feat(deploy): load compose project

This commit is contained in:
Pavel Sviderski
2025-03-14 21:41:06 +10:00
parent 7fc07ce4bd
commit e1530bb6f3
4 changed files with 85 additions and 9 deletions
+60 -8
View File
@@ -3,17 +3,16 @@ package main
import (
"context"
"fmt"
composecli "github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/graph"
"github.com/compose-spec/compose-go/v2/types"
"github.com/spf13/cobra"
"uncloud/internal/cli"
)
type deployOptions struct {
//configPath string
files []string
services []string
//machines []string
//env []string
//envFile string
//projectDir string
cluster string
}
@@ -36,12 +35,65 @@ func NewDeployCommand() *cobra.Command {
},
}
cmd.Flags().StringVarP(&opts.cluster, "cluster", "c", "", "Name of the cluster to deploy to (default is the current cluster)")
cmd.Flags().StringSliceVarP(&opts.files, "file", "f", nil,
"One or more Compose files to deploy services from. (default compose.yaml)")
cmd.Flags().StringVarP(&opts.cluster, "cluster", "c", "",
"Name of the cluster to deploy to (default is the current cluster)")
return cmd
}
// deploy parses the compose file and deploys the services to the Uncloud cluster.
// deploy parses the Compose file(s) and deploys the services.
func deploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
return fmt.Errorf("not implemented")
project, err := loadComposeProject(ctx, opts)
if err != nil {
return err
}
projectYAML, err := project.MarshalYAML()
if err != nil {
return err
}
fmt.Println(string(projectYAML))
// TODO: move to ComposeDeployment.
err = graph.InDependencyOrder(ctx, project,
// TODO: properly handle dependency conditions.
func(ctx context.Context, name string, service types.ServiceConfig) error {
service, err := project.GetService(name)
if err != nil {
return err
}
fmt.Println(service.Name)
return nil
})
return nil
}
func loadComposeProject(ctx context.Context, opts deployOptions) (*types.Project, error) {
options, err := composecli.NewProjectOptions(
opts.files,
// First apply os.Environment, always wins.
composecli.WithOsEnv,
// Read dot env file to populate project environment.
composecli.WithDotEnv,
// Get compose file path set by COMPOSE_FILE.
composecli.WithConfigFileEnv,
// If none was selected, get default compose.yaml file from current dir or parent folders.
composecli.WithDefaultConfigPath,
)
if err != nil {
return nil, fmt.Errorf("create compose parser options: %w", err)
}
project, err := options.LoadProject(ctx)
if err != nil {
return nil, fmt.Errorf("load compose file(s): %w", err)
}
return project, nil
}