Skip to content

Commit

Permalink
fix: use passed --context in talosctl config cmd
Browse files Browse the repository at this point in the history
Use context from command line flags. Also some minor fixes.

Closes #6846

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Feb 21, 2023
1 parent 5ac9f43 commit 8711eea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
46 changes: 37 additions & 9 deletions cmd/talosctl/cmd/talos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/talosctl/cmd/talos/diskusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8711eea

Please sign in to comment.