fix: better error handling & proper context deletion for server rm
continuous-integration/drone/push Build is passing Details

This commit is contained in:
decentral1se 2022-02-24 15:57:52 +01:00
parent 823f869f1d
commit 3381b8936d
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
2 changed files with 36 additions and 25 deletions

View File

@ -185,12 +185,12 @@ func ValidateSubCmdFlags(c *cli.Context) bool {
} }
// ValidateServer ensures the server name arg is valid. // ValidateServer ensures the server name arg is valid.
func ValidateServer(c *cli.Context) (string, error) { func ValidateServer(c *cli.Context) string {
serverName := c.Args().First() serverName := c.Args().First()
serverNames, err := config.ReadServerNames() serverNames, err := config.ReadServerNames()
if err != nil { if err != nil {
return serverName, err logrus.Fatal(err)
} }
if serverName == "" && !NoInput { if serverName == "" && !NoInput {
@ -199,17 +199,28 @@ func ValidateServer(c *cli.Context) (string, error) {
Options: serverNames, Options: serverNames,
} }
if err := survey.AskOne(prompt, &serverName); err != nil { if err := survey.AskOne(prompt, &serverName); err != nil {
return serverName, err logrus.Fatal(err)
} }
} }
matched := false
for _, name := range serverNames {
if name == serverName {
matched = true
}
}
if !matched {
ShowSubcommandHelpAndError(c, errors.New("server doesn't exist?"))
}
if serverName == "" { if serverName == "" {
ShowSubcommandHelpAndError(c, errors.New("no server provided")) ShowSubcommandHelpAndError(c, errors.New("no server provided"))
} }
logrus.Debugf("validated %s as server argument", serverName) logrus.Debugf("validated %s as server argument", serverName)
return serverName, nil return serverName
} }
// EnsureDNSProvider ensures a DNS provider is chosen. // EnsureDNSProvider ensures a DNS provider is chosen.

View File

@ -126,21 +126,24 @@ like tears in rain.
}, },
Before: internal.SubCommandBefore, Before: internal.SubCommandBefore,
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
serverName := c.Args().Get(1) serverName := internal.ValidateServer(c)
if serverName != "" {
var err error warnMsg := `Did not pass -s/--server for actual server deletion, prompting!
serverName, err = internal.ValidateServer(c)
if err != nil { Abra doesn't currently know if it helped you create this server with one of the
logrus.Fatal(err) 3rd party integrations (e.g. Capsul). You have a choice here to actually,
} really and finally destroy this server using those integrations. If you want to
} do this, choose Yes.
If you just want to remove the server config files & context, choose No.
`
if !rmServer { if !rmServer {
logrus.Warn("did not pass -s/--server for actual server deletion, prompting") logrus.Warn(fmt.Sprintf(warnMsg))
response := false response := false
prompt := &survey.Confirm{ prompt := &survey.Confirm{
Message: "prompt to actual server deletion?", Message: "delete actual live server?",
} }
if err := survey.AskOne(prompt, &response); err != nil { if err := survey.AskOne(prompt, &response); err != nil {
logrus.Fatal(err) logrus.Fatal(err)
@ -164,21 +167,18 @@ like tears in rain.
logrus.Fatal(err) logrus.Fatal(err)
} }
} }
} }
if serverName != "" { if err := client.DeleteContext(serverName); err != nil {
if err := client.DeleteContext(serverName); err != nil { logrus.Fatal(err)
logrus.Fatal(err)
}
if err := os.RemoveAll(filepath.Join(config.SERVERS_DIR, serverName)); err != nil {
logrus.Fatal(err)
}
logrus.Infof("server at %s has been lost in time, like tears in rain", serverName)
} }
if err := os.RemoveAll(filepath.Join(config.SERVERS_DIR, serverName)); err != nil {
logrus.Fatal(err)
}
logrus.Infof("server at %s has been lost in time, like tears in rain", serverName)
return nil return nil
}, },
} }