merge ucind cluster configuration to the user’s uncloud config

This commit is contained in:
Pavel Sviderski
2024-11-29 13:30:15 +10:00
parent faf3378659
commit cf5818d963
4 changed files with 106 additions and 4 deletions
+18 -1
View File
@@ -5,11 +5,14 @@ import (
"fmt"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
"os"
"strings"
"uncloud/cmd/ucind/cluster"
"uncloud/internal/ucind"
)
func main() {
var configPath string
cmd := &cobra.Command{
Use: "ucind",
Short: "A CLI tool for running Uncloud test clusters using Docker.",
@@ -24,13 +27,27 @@ func main() {
return fmt.Errorf("create Docker client: %w", err)
}
p := ucind.NewProvisioner(cli)
if strings.HasPrefix(configPath, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("get user home directory to resolve %q: %w", configPath, err)
}
configPath = strings.Replace(configPath, "~", home, 1)
}
configUpdater := ucind.NewConfigUpdater(configPath)
p := ucind.NewProvisioner(cli, configUpdater)
// Persist the provisioner in the context so it can be used by subcommands.
cmd.SetContext(context.WithValue(cmd.Context(), "provisioner", p))
return nil
},
}
// TODO: allow to override using UNCLOUD_CONFIG env var.
cmd.PersistentFlags().StringVar(&configPath, "uncloud-config", "~/.config/uncloud/config.toml",
"path to the Uncloud configuration file.")
_ = cmd.MarkPersistentFlagFilename("uncloud-config", "toml")
cmd.AddCommand(
cluster.NewRootCommand(),
)