-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreset.go
77 lines (70 loc) · 2.03 KB
/
reset.go
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
package cmd
import (
"errors"
"fmt"
"os"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/Mirantis/launchpad/pkg/analytics"
"github.com/Mirantis/launchpad/pkg/config"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v2"
event "gopkg.in/segmentio/analytics-go.v3"
)
// NewResetCommand creates new reset command to be called from cli.
func NewResetCommand() *cli.Command {
return &cli.Command{
Name: "reset",
Usage: "Reset (uninstall) a cluster",
Flags: append(GlobalFlags, []cli.Flag{
configFlag,
confirmFlag,
redactFlag,
&cli.BoolFlag{
Name: "force",
Usage: "Don't ask for confirmation",
Aliases: []string{"f"},
},
}...),
Before: actions(initLogger, initAnalytics, checkLicense, initExec, requireForce, startUpgradeCheck),
After: actions(closeAnalytics, upgradeCheckResult),
Action: func(ctx *cli.Context) error {
start := time.Now()
analytics.TrackEvent("Cluster Reset Started", nil)
product, err := config.ProductFromFile(ctx.String("config"))
if err != nil {
return fmt.Errorf("failed to load product config: %w", err)
}
err = product.Reset()
if err != nil {
analytics.TrackEvent("Cluster Reset Failed", nil)
return fmt.Errorf("failed to reset cluster: %w", err)
}
duration := time.Since(start)
props := event.Properties{
"duration": duration.Seconds(),
}
analytics.TrackEvent("Cluster Reset Completed", props)
return nil
},
}
}
var errForceRequired = errors.New("confirmation or --force required")
func requireForce(ctx *cli.Context) error {
if !ctx.Bool("force") {
if !isatty.IsTerminal(os.Stdout.Fd()) {
return fmt.Errorf("%w: reset requires --force", errForceRequired)
}
confirmed := false
prompt := &survey.Confirm{
Message: "Going to reset all of the hosts, which will destroy all configuration and data, Are you sure?",
}
if err := survey.AskOne(prompt, &confirmed); err != nil {
return fmt.Errorf("failed to ask for confirmation: %w", err)
}
if !confirmed {
return errForceRequired
}
}
return nil
}