Skip to content

Commit

Permalink
feat(gencommand): add reset command
Browse files Browse the repository at this point in the history
  • Loading branch information
budimanjojo committed Feb 8, 2024
1 parent 7cd0d20 commit d624d1f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cmd/gencommand_reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"log"

"github.com/spf13/cobra"

"github.com/budimanjojo/talhelper/pkg/config"
"github.com/budimanjojo/talhelper/pkg/generate"
)

var gencommandResetCmd = &cobra.Command{
Use: "reset",
Short: "Generate talosctl reset commands.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.LoadAndValidateFromFile(gencommandCfgFile, gencommandEnvFile)
if err != nil {
log.Fatalf("failed to parse config file: %s", err)
}

err = generate.GenerateResetCommand(cfg, gencommandOutDir, gencommandNode, gencommandExtraFlags)
if err != nil {
log.Fatalf("failed to generate talosctl apply command: %s", err)
}
},
}

func init() {
gencommandCmd.AddCommand(gencommandResetCmd)
}
30 changes: 30 additions & 0 deletions pkg/generate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,33 @@ func GenerateBootstrapCommand(cfg *config.TalhelperConfig, outDir string, node s
return fmt.Errorf("node with IP or hostname %s not found", node)
}
}

// GenarateResetCommand prints out `talosctl reset` command for selected node.
// `outDir` is directory where generated talosconfig and node manifest files are located.
// If `node` is empty string, it prints commands for all nodes in `cfg.Nodes`.
// It returns error, if any.
func GenerateResetCommand(cfg *config.TalhelperConfig, outDir string, node string, extraFlags []string) error {
var result []string
for _, n := range cfg.Nodes {
isSelectedNode := ((node != "") && (node == n.IPAddress)) || ((node != "") && (node == n.Hostname))
allNodesSelected := (node == "")

if allNodesSelected || isSelectedNode {
resetFlags := []string{
"--talosconfig=" + outDir + "/talosconfig",
"--nodes=" + n.IPAddress,
}
resetFlags = append(resetFlags, extraFlags...)
result = append(result, fmt.Sprintf("talosctl reset %s;", strings.Join(resetFlags, " ")))
}
}

if len(result) > 0 {
for _, r := range result {
fmt.Printf("%s\n", r)
}
return nil
} else {
return fmt.Errorf("node with IP or hostname %s not found", node)
}
}

0 comments on commit d624d1f

Please sign in to comment.