Skip to content

Commit 3bc6428

Browse files
committed
improve usage info
1 parent 9a0ce09 commit 3bc6428

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

cli/init.go

+43-24
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,20 @@ import (
1616
)
1717

1818
const (
19-
githubTarballURL = "https://github.com/dispatchrun/%s/tarball/main"
20-
githubAPIURL = "https://api.github.com/repos/dispatchrun/%s/branches/main"
21-
repo = "dispatch-examples"
19+
githubTarballURL = "https://github.com/%s/tarball/main"
20+
githubAPIURL = "https://api.github.com/repos/%s/branches/main"
21+
repo = "dispatchrun/dispatch-examples"
2222
dispatchUserDir = "dispatch"
2323
)
2424

25-
// TODO: versioning for different SDKs?
26-
2725
func directoryExists(path string) (bool, error) {
2826
info, err := os.Stat(path)
2927
if os.IsNotExist(err) {
30-
// The directory does not exist
3128
return false, nil
3229
}
3330
if err != nil {
34-
// Some other error occurred
3531
return false, err
3632
}
37-
// Check if the path is a directory
3833
return info.IsDir(), nil
3934
}
4035

@@ -96,6 +91,8 @@ func extractTarball(r io.Reader, destDir string) error {
9691
continue
9792
}
9893

94+
// We need to strip the top-level directory from the file paths
95+
// It contains the repository name and the commit SHA which we don't need
9996
// Get the top-level directory name
10097
if topLevelDir == "" {
10198
parts := strings.Split(header.Name, "/")
@@ -286,18 +283,15 @@ func initCommand() *cobra.Command {
286283
cmd := &cobra.Command{
287284
Use: "init <template> [path]",
288285
Short: "Initialize a new Dispatch project",
289-
Long: "Initialize a new Dispatch project",
290-
Args: cobra.MinimumNArgs(1),
286+
// Args: cobra.MinimumNArgs(1),
291287
RunE: func(cmd *cobra.Command, args []string) error {
292-
var directory string
293-
var exists = true
294-
288+
// get or create the Dispatch templates directory
295289
dispatchUserDirPath, err := getAppDataDir(dispatchUserDir)
296290
if err != nil {
297-
cmd.SilenceUsage = true
298-
return fmt.Errorf("failed to get Dispatch templates directory: %w", err)
291+
fmt.Printf("failed to get Dispatch templates directory: %s", err)
299292
}
300293

294+
// well-known paths for Dispatch templates
301295
dispatchTemplatesDirPath := filepath.Join(dispatchUserDirPath, "templates")
302296
dispatchTemplatesHashPath := filepath.Join(dispatchUserDirPath, "templates.sha")
303297

@@ -306,15 +300,17 @@ func initCommand() *cobra.Command {
306300
if err != nil {
307301
if !os.IsNotExist(err) {
308302
cmd.SilenceUsage = true
309-
return fmt.Errorf("failed to read templates SHA: %w", err)
303+
cmd.PrintErrf("failed to read templates SHA: %s", err)
310304
}
311305
}
312306

307+
// get the latest commit SHA from the templates repository
313308
remoteSHA, err := getLatestCommitSHA(repo)
314309
if err != nil {
315310
cmd.Printf("failed to get latest commit SHA: %v", err)
316311
}
317312

313+
// update the templates if the latest commit SHA is different
318314
if remoteSHA != "" && string(sha) != remoteSHA {
319315
cmd.Println("Templates update available. Do you want to download the latest templates? [y/N]")
320316

@@ -327,7 +323,7 @@ func initCommand() *cobra.Command {
327323
if err != nil {
328324
cmd.Printf("failed to download and extract templates: %v", err)
329325
} else {
330-
cmd.Print("Templates have been updated\n")
326+
cmd.Print("Templates have been updated\n\n")
331327
// TODO: possible improvement:
332328
// find which templates have been added/removed/modified
333329
// and/or
@@ -342,18 +338,42 @@ func initCommand() *cobra.Command {
342338
}
343339
}
344340

341+
// read the available templates
345342
templates, err := readDirectories(dispatchTemplatesDirPath)
343+
346344
if err != nil {
347345
cmd.SilenceUsage = true
348346
if os.IsNotExist(err) {
349-
return fmt.Errorf("templates directory does not exist in %s. Please run `dispatch init` to download the templates", dispatchTemplatesDirPath)
347+
cmd.PrintErrf("templates directory does not exist in %s. Please run `dispatch init` to download the templates", dispatchTemplatesDirPath)
350348
}
351-
return fmt.Errorf("failed to read templates directory. : %w", err)
349+
cmd.PrintErrf("failed to read templates directory. : %s", err)
352350
}
353351

352+
if len(templates) == 0 {
353+
cmd.SilenceUsage = true
354+
return fmt.Errorf("templates directory %s is corrupted. Please clean it and try again", dispatchTemplatesDirPath)
355+
}
356+
357+
var templatesList string = ""
358+
359+
for _, template := range templates {
360+
templatesList += " " + template + "\n"
361+
}
362+
cmd.SetUsageTemplate(cmd.UsageTemplate() + "\nAvailable templates:\n" + templatesList)
363+
364+
// if no arguments are provided (user wants to download/update templates only), print the usage
365+
if len(args) == 0 {
366+
cmd.Print(cmd.UsageString())
367+
return nil
368+
}
369+
370+
var directory string
371+
var exists = true
372+
354373
wantedTemplate := args[0]
355374
isTemplateFound := false
356375

376+
// find template in the available templates
357377
for _, template := range templates {
358378
if template == wantedTemplate {
359379
isTemplateFound = true
@@ -363,14 +383,11 @@ func initCommand() *cobra.Command {
363383

364384
if !isTemplateFound {
365385
cmd.SilenceUsage = true
366-
cmd.Printf("Template %v is not supported.\nAvailable templates:\n", wantedTemplate)
367-
for _, template := range templates {
368-
cmd.Println(" " + template)
369-
}
370-
cmd.Println()
386+
cmd.Printf("Template %s is not supported.\n\nAvailable templates:\n %s", wantedTemplate, templatesList)
371387
return nil
372388
}
373389

390+
// check if a directory is provided
374391
if len(args) > 1 {
375392
directory = args[1]
376393
flag, err := directoryExists(directory)
@@ -394,6 +411,7 @@ func initCommand() *cobra.Command {
394411
directory = "."
395412
}
396413

414+
// check if the if directory exists and is empty
397415
if exists {
398416
isEmpty, err := isDirectoryEmpty(directory)
399417
if err != nil {
@@ -435,5 +453,6 @@ func initCommand() *cobra.Command {
435453
return nil
436454
},
437455
}
456+
438457
return cmd
439458
}

0 commit comments

Comments
 (0)