Skip to content

Commit

Permalink
Change --plan by --hosts and fixes an error on the help poster
Browse files Browse the repository at this point in the history
Signed-off-by: Gu1llaum-3 <[email protected]>
  • Loading branch information
Gu1llaum-3 committed Jul 14, 2024
1 parent 8861d70 commit c634106
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ type Node struct {
}

func MakeReset() *cobra.Command {
var user, ip, sshKey, plan string
var user, ip, sshKey, hosts string

cmd := &cobra.Command{
Use: "reset",
Short: "Uninstall k3s on specified nodes",
RunE: func(cmd *cobra.Command, args []string) error {
if user == "" || (ip == "" && plan == "") {
return fmt.Errorf("Usage: %s", cmd.UsageString())
if user == "" || (ip == "" && hosts == "") {
cmd.Help()
return nil // Change this line to return nil instead of an error
}

if plan != "" {
return uninstallK3sFromPlan(user, sshKey, plan)
if hosts != "" {
return uninstallK3sFromHosts(user, sshKey, hosts)
}
return uninstallK3s(user, sshKey, ip)
},
Expand All @@ -37,7 +38,7 @@ func MakeReset() *cobra.Command {
cmd.Flags().StringVarP(&user, "user", "u", "", "Username for SSH connection")
cmd.Flags().StringVarP(&ip, "ip", "i", "", "IP address of the host")
cmd.Flags().StringVar(&sshKey, "ssh-key", os.Getenv("HOME")+"/.ssh/id_rsa", "Path to the private SSH key")
cmd.Flags().StringVar(&plan, "plan", "", "JSON file containing the list of nodes")
cmd.Flags().StringVar(&hosts, "hosts", "", "JSON file containing the list of nodes")

return cmd
}
Expand Down Expand Up @@ -65,15 +66,15 @@ fi
return nil
}

func uninstallK3sFromPlan(user, sshKey, plan string) error {
data, err := ioutil.ReadFile(plan)
func uninstallK3sFromHosts(user, sshKey, hosts string) error {
data, err := ioutil.ReadFile(hosts)
if err != nil {
return fmt.Errorf("unable to read JSON file %s: %v", plan, err)
return fmt.Errorf("unable to read JSON file %s: %v", hosts, err)
}

var nodes []Node
if err := json.Unmarshal(data, &nodes); err != nil {
return fmt.Errorf("error parsing JSON file %s: %v", plan, err)
return fmt.Errorf("error parsing JSON file %s: %v", hosts, err)
}

var successNodes []Node
Expand Down

0 comments on commit c634106

Please sign in to comment.