From 7bc69b9a9e1ccbc973ae5f91a940fc79a3b53466 Mon Sep 17 00:00:00 2001 From: Connor Edwards <38229097+cedws@users.noreply.github.com> Date: Thu, 1 May 2025 06:12:18 +0900 Subject: [PATCH] feat: add support for Compose profiles --- cmd/uncloud/deploy.go | 19 ++++++++++++++++++- pkg/client/compose/project.go | 10 +++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cmd/uncloud/deploy.go b/cmd/uncloud/deploy.go index 02b61e7a..141e0e4c 100644 --- a/cmd/uncloud/deploy.go +++ b/cmd/uncloud/deploy.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + composecli "github.com/compose-spec/compose-go/v2/cli" "github.com/docker/compose/v2/pkg/progress" "github.com/psviderski/uncloud/internal/cli" "github.com/psviderski/uncloud/pkg/api" @@ -17,6 +18,7 @@ import ( type deployOptions struct { files []string + profiles []string services []string context string @@ -41,6 +43,8 @@ func NewDeployCommand() *cobra.Command { cmd.Flags().StringSliceVarP(&opts.files, "file", "f", nil, "One or more Compose files to deploy services from. (default compose.yaml)") + cmd.Flags().StringSliceVarP(&opts.profiles, "profile", "p", nil, + "One or more Compose profiles to enable.") cmd.Flags().StringVarP(&opts.context, "context", "c", "", "Name of the cluster context to deploy to (default is the current context)") // TODO: Consider adding a filter flag to specify which machines to deploy to but keep the rest running. @@ -49,9 +53,22 @@ func NewDeployCommand() *cobra.Command { return cmd } +// projectOpts returns the project options for the Compose file(s). +func projectOpts(opts deployOptions) []composecli.ProjectOptionsFn { + projectOpts := []composecli.ProjectOptionsFn{} + + if len(opts.profiles) > 0 { + projectOpts = append(projectOpts, composecli.WithDefaultProfiles(opts.profiles...)) + } + + return projectOpts +} + // runDeploy parses the Compose file(s) and deploys the services. func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error { - project, err := compose.LoadProject(ctx, opts.files) + projectOpts := projectOpts(opts) + + project, err := compose.LoadProject(ctx, opts.files, projectOpts...) if err != nil { return fmt.Errorf("load compose file(s): %w", err) } diff --git a/pkg/client/compose/project.go b/pkg/client/compose/project.go index cad92e74..efc3bbe2 100644 --- a/pkg/client/compose/project.go +++ b/pkg/client/compose/project.go @@ -13,9 +13,8 @@ import ( // FakeProjectName is a placeholder name for the project to be able to strip it from the resource names used as prefix. const FakeProjectName = "f-a-k-e" -func LoadProject(ctx context.Context, paths []string) (*types.Project, error) { - options, err := composecli.NewProjectOptions( - paths, +func LoadProject(ctx context.Context, paths []string, opts ...composecli.ProjectOptionsFn) (*types.Project, error) { + defaultOpts := []composecli.ProjectOptionsFn{ composecli.WithName(FakeProjectName), // First apply os.Environment, always wins. composecli.WithOsEnv, @@ -26,6 +25,11 @@ func LoadProject(ctx context.Context, paths []string) (*types.Project, error) { // If none was selected, get default Compose file names from current or parent folders. composecli.WithDefaultConfigPath, composecli.WithExtension(PortsExtensionKey, PortsSource{}), + } + + options, err := composecli.NewProjectOptions( + paths, + append(defaultOpts, opts...)..., ) if err != nil { return nil, fmt.Errorf("create compose parser options: %w", err)