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 588d8dd
Showing 1 changed file with 83 additions and 69 deletions.
152 changes: 83 additions & 69 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 @@ -283,73 +280,27 @@ func copyFile(srcFile string, dstFile string) error {
}

func initCommand() *cobra.Command {
dispatchUserDirPath, err := getAppDataDir(dispatchUserDir)
if err != nil {
fmt.Printf("failed to get Dispatch templates directory: %s", err)
}

dispatchTemplatesDirPath := filepath.Join(dispatchUserDirPath, "templates")
dispatchTemplatesHashPath := filepath.Join(dispatchUserDirPath, "templates.sha")
templates := []string{}

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)
}

dispatchTemplatesDirPath := filepath.Join(dispatchUserDirPath, "templates")
dispatchTemplatesHashPath := filepath.Join(dispatchUserDirPath, "templates.sha")

// read the latest commit SHA
sha, err := os.ReadFile(dispatchTemplatesHashPath)
if err != nil {
if !os.IsNotExist(err) {
cmd.SilenceUsage = true
return fmt.Errorf("failed to read templates SHA: %w", err)
}
}

remoteSHA, err := getLatestCommitSHA(repo)
if err != nil {
cmd.Printf("failed to get latest commit SHA: %v", err)
}

if remoteSHA != "" && string(sha) != remoteSHA {
cmd.Println("Templates update available. Do you want to download the latest templates? [y/N]")

var response string
fmt.Scanln(&response)

if strings.ToLower(response) == "y" {
cmd.Printf("Downloading templates\n")
err = downloadAndExtractTemplates(dispatchTemplatesDirPath)
if err != nil {
cmd.Printf("failed to download and extract templates: %v", err)
} else {
cmd.Print("Templates have been updated\n")
// TODO: possible improvement:
// find which templates have been added/removed/modified
// and/or
// show last n commit messages as changes
}

// save the latest commit SHA
err = os.WriteFile(dispatchTemplatesHashPath, []byte(remoteSHA), 0644)
if err != nil {
cmd.Printf("failed to save templates SHA: %v", err)
}
}
if len(args) == 0 {
cmd.Print(cmd.UsageString())
return nil
}

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)
}
return fmt.Errorf("failed to read templates directory. : %w", err)
}
var directory string
var exists = true

wantedTemplate := args[0]
isTemplateFound := false
Expand Down Expand Up @@ -435,5 +386,68 @@ func initCommand() *cobra.Command {
return nil
},
}

// read the latest commit SHA
sha, err := os.ReadFile(dispatchTemplatesHashPath)
if err != nil {
if !os.IsNotExist(err) {
cmd.SilenceUsage = true
cmd.PrintErrf("failed to read templates SHA: %s", err)
}
}

remoteSHA, err := getLatestCommitSHA(repo)
if err != nil {
cmd.Printf("failed to get latest commit SHA: %v", err)
}

if remoteSHA != "" && string(sha) != remoteSHA {
cmd.Println("Templates update available. Do you want to download the latest templates? [y/N]")

var response string
fmt.Scanln(&response)

if strings.ToLower(response) == "y" {
cmd.Printf("Downloading templates\n")
err = downloadAndExtractTemplates(dispatchTemplatesDirPath)
if err != nil {
cmd.Printf("failed to download and extract templates: %v", err)
} else {
cmd.Print("Templates have been updated\n\n")
// TODO: possible improvement:
// find which templates have been added/removed/modified
// and/or
// show last n commit messages as changes
}

// save the latest commit SHA
err = os.WriteFile(dispatchTemplatesHashPath, []byte(remoteSHA), 0644)
if err != nil {
cmd.Printf("failed to save templates SHA: %v", err)
}
}
}

templateDirs, err := readDirectories(dispatchTemplatesDirPath)
templates = templateDirs

if err != nil {
cmd.SilenceUsage = true
if os.IsNotExist(err) {
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"
}
cmd.SetUsageTemplate(cmd.UsageTemplate() + "\nAvailable templates:\n" + templatesList)
}

return cmd
}

0 comments on commit 588d8dd

Please sign in to comment.