feat: Introduces a new help error
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
p4u1 2025-03-13 09:02:59 +01:00
parent e58a716fe1
commit 62ff8eab44
3 changed files with 36 additions and 14 deletions

18
cli/internal/error.go Normal file
View File

@ -0,0 +1,18 @@
package internal
import "fmt"
type HelpError struct {
err string
help string
}
func (e HelpError) Error() string {
return fmt.Sprintf(`%s
Help: %s `, e.err, e.help)
}
func Error(msg, help string) HelpError {
return HelpError{err: msg, help: help}
}

View File

@ -66,6 +66,8 @@ func Run(version, commit string) {
} }
rootCmd.CompletionOptions.DisableDefaultCmd = true rootCmd.CompletionOptions.DisableDefaultCmd = true
// This prevents displaying usage for errors returned from RunE
rootCmd.SilenceUsage = true
manCommand := &cobra.Command{ manCommand := &cobra.Command{
Use: "man [flags]", Use: "man [flags]",

View File

@ -1,6 +1,8 @@
package server package server
import ( import (
"errors"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -41,19 +43,20 @@ developer machine. The domain is then set to "default".`,
ValidArgsFunction: func( ValidArgsFunction: func(
cmd *cobra.Command, cmd *cobra.Command,
args []string, args []string,
toComplete string) ([]string, cobra.ShellCompDirective) { toComplete string,
) ([]string, cobra.ShellCompDirective) {
if !local { if !local {
return autocomplete.ServerNameComplete() return autocomplete.ServerNameComplete()
} }
return nil, cobra.ShellCompDirectiveDefault return nil, cobra.ShellCompDirectiveDefault
}, },
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 && local { if len(args) > 0 && local {
log.Fatal("cannot use [server] and --local together") return errors.New("cannot use [server] and --local together")
} }
if len(args) == 0 && !local { if len(args) == 0 && !local {
log.Fatal("missing argument or --local/-l flag") return errors.New("missing argument or --local/-l flag")
} }
name := "default" name := "default"
@ -69,14 +72,14 @@ developer machine. The domain is then set to "default".`,
if local { if local {
created, err := createServerDir(name) created, err := createServerDir(name)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
log.Debugf("attempting to create client for %s", name) log.Debugf("attempting to create client for %s", name)
if _, err := client.New(name, timeout); err != nil { if _, err := client.New(name, timeout); err != nil {
cleanUp(name) cleanUp(name)
log.Fatal(err) return err
} }
if created { if created {
@ -85,18 +88,18 @@ developer machine. The domain is then set to "default".`,
log.Warn("local server already exists") log.Warn("local server already exists")
} }
return return nil
} }
_, err := createServerDir(name) _, err := createServerDir(name)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
created, err := newContext(name) created, err := newContext(name)
if err != nil { if err != nil {
cleanUp(name) cleanUp(name)
log.Fatalf("unable to create local context: %s", err) return fmt.Errorf("unable to create local context: %s", err)
} }
log.Debugf("attempting to create client for %s", name) log.Debugf("attempting to create client for %s", name)
@ -104,7 +107,7 @@ developer machine. The domain is then set to "default".`,
if _, err := client.New(name, timeout); err != nil { if _, err := client.New(name, timeout); err != nil {
cleanUp(name) cleanUp(name)
log.Debugf("ssh %s error: %s", name, sshPkg.Fatal(name, err)) log.Debugf("ssh %s error: %s", name, sshPkg.Fatal(name, err))
log.Fatalf("can't ssh to %s, make sure \"ssh %s\" works", name, name) return internal.Error(fmt.Sprintf("can't ssh to %s", name), fmt.Sprintf("make sure \"ssh %s\" works", name))
} }
if created { if created {
@ -114,10 +117,11 @@ developer machine. The domain is then set to "default".`,
log.Warnf("unable to resolve IPv4 for %s", name) log.Warnf("unable to resolve IPv4 for %s", name)
} }
return return nil
} }
log.Warnf("%s already exists", name) log.Warnf("%s already exists", name)
return nil
}, },
} }
@ -189,9 +193,7 @@ func createServerDir(name string) (bool, error) {
return true, nil return true, nil
} }
var ( var local bool
local bool
)
func init() { func init() {
ServerAddCommand.Flags().BoolVarP( ServerAddCommand.Flags().BoolVarP(