fix: use passed --context in talosctl config cmd

Use context from command line flags. Also some minor fixes.

Closes #6846

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
This commit is contained in:
Dmitriy Matrenichev 2023-02-21 01:32:37 +03:00
parent 5ac9f43e45
commit 8711eea962
No known key found for this signature in database
GPG Key ID: D3363CF894E68892
2 changed files with 38 additions and 10 deletions

View File

@ -77,7 +77,12 @@ var configEndpointCmd = &cobra.Command{
args[i] = strings.TrimSpace(args[i])
}
c.Contexts[c.Context].Endpoints = args
ctxData, err := getContextData(c)
if err != nil {
return err
}
ctxData.Endpoints = args
if err := c.Save(GlobalArgs.Talosconfig); err != nil {
return fmt.Errorf("error writing config: %w", err)
}
@ -103,7 +108,12 @@ var configNodeCmd = &cobra.Command{
args[i] = strings.TrimSpace(args[i])
}
c.Contexts[c.Context].Nodes = args
ctxData, err := getContextData(c)
if err != nil {
return err
}
ctxData.Nodes = args
if err := c.Save(GlobalArgs.Talosconfig); err != nil {
return fmt.Errorf("error writing config: %w", err)
}
@ -446,9 +456,12 @@ Certificate expires: {{ .CertTTL }} ({{ .CertNotAfter }})
//
//nolint:goconst
func configInfoCommand(config *clientconfig.Config, now time.Time) (string, error) {
context := config.Contexts[config.Context]
cfgContext, err := getContextData(config)
if err != nil {
return "", err
}
b, err := base64.StdEncoding.DecodeString(context.Crt)
b, err := base64.StdEncoding.DecodeString(cfgContext.Crt)
if err != nil {
return "", err
}
@ -466,13 +479,13 @@ func configInfoCommand(config *clientconfig.Config, now time.Time) (string, erro
roles, _ := role.Parse(crt.Subject.Organization)
nodesS := "not defined"
if len(context.Nodes) > 0 {
nodesS = strings.Join(context.Nodes, ", ")
if len(cfgContext.Nodes) > 0 {
nodesS = strings.Join(cfgContext.Nodes, ", ")
}
endpointsS := "not defined"
if len(context.Endpoints) > 0 {
endpointsS = strings.Join(context.Endpoints, ", ")
if len(cfgContext.Endpoints) > 0 {
endpointsS = strings.Join(cfgContext.Endpoints, ", ")
}
rolesS := "not defined"
@ -517,7 +530,7 @@ var configInfoCmd = &cobra.Command{
// CompleteConfigContext represents tab completion for `--context`
// argument and `config [context|remove]` command.
func CompleteConfigContext(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
func CompleteConfigContext(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
c, err := clientconfig.Open(GlobalArgs.Talosconfig)
if err != nil {
return nil, cobra.ShellCompDirectiveError
@ -559,3 +572,18 @@ func init() {
addCommand(configCmd)
}
func getContextData(c *clientconfig.Config) (*clientconfig.Context, error) {
contextName := c.Context
if GlobalArgs.CmdContext != "" {
contextName = GlobalArgs.CmdContext
}
ctxData, ok := c.Contexts[contextName]
if !ok {
return nil, fmt.Errorf("context %q is not defined", contextName)
}
return ctxData, nil
}

View File

@ -10,7 +10,7 @@ import (
"os"
"text/tabwriter"
humanize "github.com/dustin/go-humanize"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"