chore: activate the context of a newly initialised cluster

This commit is contained in:
Pavel Sviderski
2025-04-03 13:38:48 +10:00
parent 3ada89c4f8
commit b3885b0888
2 changed files with 6 additions and 8 deletions
+2 -2
View File
@@ -82,8 +82,8 @@ func NewInitCommand() *cobra.Command {
"Path to SSH private key for SSH remote login. (default ~/.ssh/id_*)", "Path to SSH private key for SSH remote login. (default ~/.ssh/id_*)",
) )
cmd.Flags().StringVarP( cmd.Flags().StringVarP(
&opts.context, "context", "c", "", &opts.context, "context", "c", "default",
"Name of the cluster context in the local config.", "Name of the created context for the initialised cluster in the Uncloud config.",
) )
return cmd return cmd
+4 -6
View File
@@ -173,7 +173,7 @@ func (cli *CLI) initRemoteMachine(
contextName = defaultContextName contextName = defaultContextName
} }
if _, ok := cli.Config.Contexts[contextName]; ok { if _, ok := cli.Config.Contexts[contextName]; ok {
return nil, fmt.Errorf("cluster %q already exists", contextName) return nil, fmt.Errorf("cluster context '%s' already exists", contextName)
} }
machineClient, err := cli.provisionRemoteMachine(ctx, remoteMachine) machineClient, err := cli.provisionRemoteMachine(ctx, remoteMachine)
@@ -220,12 +220,10 @@ func (cli *CLI) initRemoteMachine(
if err = cli.CreateContext(contextName); err != nil { if err = cli.CreateContext(contextName); err != nil {
return nil, fmt.Errorf("save cluster context to config: %w", err) return nil, fmt.Errorf("save cluster context to config: %w", err)
} }
// Set the current cluster to the just created one if it is the only cluster in the config. if err = cli.SetCurrentContext(contextName); err != nil {
if len(cli.Config.Contexts) == 1 { return nil, fmt.Errorf("set current cluster context: %w", err)
if err = cli.SetCurrentContext(contextName); err != nil {
return nil, fmt.Errorf("set current cluster context: %w", err)
}
} }
fmt.Printf("Current cluster context is now '%s'.\n", contextName)
// Save the machine's SSH connection details in the context config. // Save the machine's SSH connection details in the context config.
connCfg := config.MachineConnection{ connCfg := config.MachineConnection{