feat: add support for Compose profiles

This commit is contained in:
Connor Edwards
2025-05-01 12:23:56 +09:00
parent 769f5e47c3
commit 7bc69b9a9e
2 changed files with 25 additions and 4 deletions
+18 -1
View File
@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"strings" "strings"
composecli "github.com/compose-spec/compose-go/v2/cli"
"github.com/docker/compose/v2/pkg/progress" "github.com/docker/compose/v2/pkg/progress"
"github.com/psviderski/uncloud/internal/cli" "github.com/psviderski/uncloud/internal/cli"
"github.com/psviderski/uncloud/pkg/api" "github.com/psviderski/uncloud/pkg/api"
@@ -17,6 +18,7 @@ import (
type deployOptions struct { type deployOptions struct {
files []string files []string
profiles []string
services []string services []string
context string context string
@@ -41,6 +43,8 @@ func NewDeployCommand() *cobra.Command {
cmd.Flags().StringSliceVarP(&opts.files, "file", "f", nil, cmd.Flags().StringSliceVarP(&opts.files, "file", "f", nil,
"One or more Compose files to deploy services from. (default compose.yaml)") "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", "", cmd.Flags().StringVarP(&opts.context, "context", "c", "",
"Name of the cluster context to deploy to (default is the current context)") "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. // 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 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. // runDeploy parses the Compose file(s) and deploys the services.
func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error { 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 { if err != nil {
return fmt.Errorf("load compose file(s): %w", err) return fmt.Errorf("load compose file(s): %w", err)
} }
+7 -3
View File
@@ -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. // 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" const FakeProjectName = "f-a-k-e"
func LoadProject(ctx context.Context, paths []string) (*types.Project, error) { func LoadProject(ctx context.Context, paths []string, opts ...composecli.ProjectOptionsFn) (*types.Project, error) {
options, err := composecli.NewProjectOptions( defaultOpts := []composecli.ProjectOptionsFn{
paths,
composecli.WithName(FakeProjectName), composecli.WithName(FakeProjectName),
// First apply os.Environment, always wins. // First apply os.Environment, always wins.
composecli.WithOsEnv, 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. // If none was selected, get default Compose file names from current or parent folders.
composecli.WithDefaultConfigPath, composecli.WithDefaultConfigPath,
composecli.WithExtension(PortsExtensionKey, PortsSource{}), composecli.WithExtension(PortsExtensionKey, PortsSource{}),
}
options, err := composecli.NewProjectOptions(
paths,
append(defaultOpts, opts...)...,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("create compose parser options: %w", err) return nil, fmt.Errorf("create compose parser options: %w", err)