Skip to content

Commit

Permalink
fix: Better error messages for kitsch init.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Sep 20, 2022
1 parent 4bad6ed commit 986ec5c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
12 changes: 10 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/jwalton/kitsch/internal/kitsch/initscripts"
"github.com/jwalton/kitsch/internal/kitsch/log"
"github.com/spf13/cobra"
)

var initCmd = &cobra.Command{
Use: "init",
Use: "init [shell]",
Short: "Returns a script which can be used to initialize " + programName + ".",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
validShells := strings.Join(initscripts.ValidShells(), ", ")
log.Error("init command requires the name of a shells. Use one of: " + validShells + ".")
os.Exit(1)
}

shell := args[0]

printFullInit, err := cmd.Flags().GetBool("print-full-init")
Expand Down
4 changes: 3 additions & 1 deletion internal/kitsch/initscripts/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"embed"
"fmt"
"os"
"strings"
"text/template"
)

Expand Down Expand Up @@ -46,7 +47,8 @@ func getInitScript(filename string, shell string, configFile string) (string, er

initTemplate, err := initTemplates.ReadFile("templates/" + shell + "-" + filename + "." + shellExt)
if err != nil {
return "", fmt.Errorf("invalid shell %s", shell)
validShells := strings.Join(ValidShells(), ", ")
return "", fmt.Errorf("invalid shell %s. Use one of: %s", shell, validShells)
}

return execTemplate(string(initTemplate), data)
Expand Down
9 changes: 9 additions & 0 deletions internal/kitsch/initscripts/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ var shellConfigFiles = map[string]string{
"powershell": "Microsoft.PowerShell_profile.ps1 (you can find the location of this file by running `echo $PROFILE`)",
}

// ValidShells returns a list of valid shell types.
func ValidShells() []string {
result := []string{}
for supportedShell := range shellConfigFiles {
result = append(result, supportedShell)
}
return result
}

// ShowSetupInstructions prints setup instructions for the given shell.
func ShowSetupInstructions(
programName string,
Expand Down

0 comments on commit 986ec5c

Please sign in to comment.