Skip to content

Commit

Permalink
improve usage info
Browse files Browse the repository at this point in the history
  • Loading branch information
chicoxyzzy committed Jun 25, 2024
1 parent 9a0ce09 commit ac8e40c
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@ import (
)

const (
githubTarballURL = "https://github.com/dispatchrun/%s/tarball/main"
githubAPIURL = "https://api.github.com/repos/dispatchrun/%s/branches/main"
repo = "dispatch-examples"
githubTarballURL = "https://github.com/%s/tarball/main"
githubAPIURL = "https://api.github.com/repos/%s/branches/main"
repo = "dispatchrun/dispatch-examples"
dispatchUserDir = "dispatch"
)

// TODO: versioning for different SDKs?

func directoryExists(path string) (bool, error) {
info, err := os.Stat(path)
if os.IsNotExist(err) {
// The directory does not exist
return false, nil
}
if err != nil {
// Some other error occurred
return false, err
}
// Check if the path is a directory
return info.IsDir(), nil
}

Expand Down Expand Up @@ -96,6 +91,8 @@ func extractTarball(r io.Reader, destDir string) error {
continue
}

// We need to strip the top-level directory from the file paths
// It contains the repository name and the commit SHA which we don't need
// Get the top-level directory name
if topLevelDir == "" {
parts := strings.Split(header.Name, "/")
Expand Down Expand Up @@ -286,16 +283,11 @@ func initCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "init <template> [path]",
Short: "Initialize a new Dispatch project",
Long: "Initialize a new Dispatch project",
Args: cobra.MinimumNArgs(1),
// Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var directory string
var exists = true

dispatchUserDirPath, err := getAppDataDir(dispatchUserDir)
if err != nil {
cmd.SilenceUsage = true
return fmt.Errorf("failed to get Dispatch templates directory: %w", err)
fmt.Printf("failed to get Dispatch templates directory: %s", err)
}

dispatchTemplatesDirPath := filepath.Join(dispatchUserDirPath, "templates")
Expand All @@ -306,7 +298,7 @@ func initCommand() *cobra.Command {
if err != nil {
if !os.IsNotExist(err) {
cmd.SilenceUsage = true
return fmt.Errorf("failed to read templates SHA: %w", err)
cmd.PrintErrf("failed to read templates SHA: %s", err)
}
}

Expand All @@ -327,7 +319,7 @@ func initCommand() *cobra.Command {
if err != nil {
cmd.Printf("failed to download and extract templates: %v", err)
} else {
cmd.Print("Templates have been updated\n")
cmd.Print("Templates have been updated\n\n")
// TODO: possible improvement:
// find which templates have been added/removed/modified
// and/or
Expand All @@ -343,14 +335,33 @@ func initCommand() *cobra.Command {
}

templates, err := readDirectories(dispatchTemplatesDirPath)

if err != nil {
cmd.SilenceUsage = true
if os.IsNotExist(err) {
return fmt.Errorf("templates directory does not exist in %s. Please run `dispatch init` to download the templates", dispatchTemplatesDirPath)
cmd.PrintErrf("templates directory does not exist in %s. Please run `dispatch init` to download the templates", dispatchTemplatesDirPath)
}
cmd.PrintErrf("failed to read templates directory. : %s", err)
}

if len(templates) == 0 {
cmd.SetUsageTemplate(cmd.UsageTemplate() + "\nNo templates available. Please run `dispatch init` to download the templates")
} else {
templatesList := ""
for _, template := range templates {
templatesList += " " + template + "\n"
}
return fmt.Errorf("failed to read templates directory. : %w", err)
cmd.SetUsageTemplate(cmd.UsageTemplate() + "\nAvailable templates:\n" + templatesList)
}

if len(args) == 0 {
cmd.Print(cmd.UsageString())
return nil
}

var directory string
var exists = true

wantedTemplate := args[0]
isTemplateFound := false

Expand Down Expand Up @@ -435,5 +446,6 @@ func initCommand() *cobra.Command {
return nil
},
}

return cmd
}

0 comments on commit ac8e40c

Please sign in to comment.