Skip to content

Commit

Permalink
chore: simplify with RemoveAll
Browse files Browse the repository at this point in the history
Signed-off-by: nathan-nicholson <[email protected]>
  • Loading branch information
nathan-nicholson committed Dec 10, 2024
1 parent 9c93181 commit 33b2ae0
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ See the LICENSE file for more details.
package cmd

import (
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -65,7 +64,12 @@ var resetCmd = &cobra.Command{
return fmt.Errorf("unable to determine contents of kubefirst-checks: unexpected type %T", v)
}

if err := runReset(); err != nil {
homePath, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("unable to get user home directory: %w", err)
}

if err := runReset(homePath); err != nil {
return fmt.Errorf("error during reset operation: %w", err)
}
return nil
Expand Down Expand Up @@ -95,18 +99,14 @@ func parseConfigEntryKubefirstChecks(checks map[string]interface{}) (map[string]
}

// runReset carries out the reset function
func runReset() error {
func runReset(homePath string) error {
utils.DisplayLogHints()

progressPrinter.AddTracker("removing-platform-content", "Removing local platform content", 2)
progressPrinter.SetupProgress(progressPrinter.TotalOfTrackers(), false)

log.Info().Msg("removing previous platform content")

homePath, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("unable to get user home directory: %w", err)
}
k1Dir := fmt.Sprintf("%s/.k1", homePath)
kubefirstConfig := fmt.Sprintf("%s/.kubefirst", homePath)

Expand All @@ -133,13 +133,7 @@ func runReset() error {
return fmt.Errorf("unable to delete %q folder, error: %w", k1Dir, err)
}

if _, err := os.Stat(kubefirstConfig); err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Info().Msgf("%q does not exist, continuing", kubefirstConfig)
} else {
return fmt.Errorf("error checking %q: %w", kubefirstConfig, err)
}
} else if err := os.Remove(kubefirstConfig); err != nil {
if err := os.RemoveAll(kubefirstConfig); err != nil {
return fmt.Errorf("unable to remove %q, error: %w", kubefirstConfig, err)
}

Expand Down

0 comments on commit 33b2ae0

Please sign in to comment.