-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathreset.go
More file actions
executable file
·135 lines (117 loc) · 4.07 KB
/
reset.go
File metadata and controls
executable file
·135 lines (117 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package cmd
import (
"fmt"
"os"
"strconv"
"time"
"github.com/konstructio/kubefirst-api/pkg/progressPrinter"
utils "github.com/konstructio/kubefirst-api/pkg/utils"
"github.com/konstructio/kubefirst/internal/progress"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// resetCmd represents the reset command
var resetCmd = &cobra.Command{
Use: "reset",
Short: "removes local kubefirst content to provision a new platform",
Long: "removes local kubefirst content to provision a new platform",
RunE: func(_ *cobra.Command, _ []string) error {
gitProvider := viper.GetString("kubefirst.git-provider")
cloudProvider := viper.GetString("kubefirst.cloud-provider")
checksMap := viper.Get("kubefirst-checks")
switch v := checksMap.(type) {
case nil:
// Handle the nil case explicitly
message := `# Successfully reset`
progress.Success(message)
return nil
case string:
if v == "" {
log.Info().Msg("checks map is empty, continuing")
} else {
return fmt.Errorf("unable to determine contents of kubefirst-checks: unexpected type %T", v)
}
case map[string]interface{}:
checks, err := parseConfigEntryKubefirstChecks(v)
if err != nil {
log.Error().Msgf("error occurred during check parsing: %s - resetting directory without checks", err)
}
// If destroy hasn't been run yet, reset should fail to avoid orphaned resources
switch {
case checks[fmt.Sprintf("terraform-apply-%s", gitProvider)]:
return fmt.Errorf(
"it looks like there's an active %s resource deployment - please run `%s destroy` before continuing",
gitProvider, cloudProvider,
)
case checks[fmt.Sprintf("terraform-apply-%s", cloudProvider)]:
return fmt.Errorf(
"it looks like there's an active %s installation - please run `%s destroy` before continuing",
cloudProvider, cloudProvider,
)
}
default:
return fmt.Errorf("unable to determine contents of kubefirst-checks: unexpected type %T", v)
}
if err := runReset(); err != nil {
return fmt.Errorf("error during reset operation: %w", err)
}
return nil
},
}
// parseConfigEntryKubefirstChecks gathers the kubefirst-checks section of the Viper
// config file and parses as a map[string]bool
func parseConfigEntryKubefirstChecks(checks map[string]interface{}) (map[string]bool, error) {
if checks == nil {
return map[string]bool{}, fmt.Errorf("checks configuration is nil")
}
checksMap := make(map[string]bool, 0)
for key, value := range checks {
strKey := fmt.Sprintf("%v", key)
boolValue := fmt.Sprintf("%v", value)
boolValueP, _ := strconv.ParseBool(boolValue)
checksMap[strKey] = boolValueP
}
return checksMap, nil
}
// runReset carries out the reset function
func runReset() 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)
if err := utils.ResetK1Dir(k1Dir); err != nil {
return fmt.Errorf("error resetting k1 directory: %w", err)
}
log.Info().Msg("previous platform content removed")
progressPrinter.IncrementTracker("removing-platform-content")
log.Info().Msg("resetting $HOME/.kubefirst config")
viper.Set("argocd", "")
viper.Set("github", "")
viper.Set("gitlab", "")
viper.Set("components", "")
viper.Set("kbot", "")
viper.Set("kubefirst-checks", "")
viper.Set("kubefirst", "")
viper.Set("secrets", "")
if err := viper.WriteConfig(); err != nil {
return fmt.Errorf("error writing viper config: %w", err)
}
if err := os.RemoveAll(k1Dir); err != nil {
return fmt.Errorf("unable to delete %q folder, error: %w", k1Dir, err)
}
progressPrinter.IncrementTracker("removing-platform-content")
time.Sleep(time.Second * 2)
progress.Progress.Quit()
return nil
}