Skip to content

Commit

Permalink
add version and verbose flags
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaumoron committed Oct 12, 2023
1 parent f777fa1 commit da63576
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func Init() *cobra.Command {
Short: "cornucopia run a code generation script.",
Long: `cornucopia run a code generation script, find more details at :
https://github.com/dvaumoron/cornucopia`,
Args: cobra.ExactArgs(1),
Version: "v1.1.0",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
if err != nil {
return err
Expand All @@ -50,6 +51,14 @@ https://github.com/dvaumoron/cornucopia`,
thread := &starlark.Thread{Name: common.Prefix + scriptname, Load: loader.Load}
glu.InitCornucopiaGlobals()

if conf.Verbose {
fmt.Println("Use the repository", conf.RepoPath, "as local cache")
fmt.Println("Use the url", conf.RepoUrl, "as base to download script")
if conf.ForceDownload {
fmt.Println("Skip local cache in script resolution (still write in it)")
}
}

_, err = starlark.ExecFile(thread, scriptname, nil, nil)
generated := len(glu.GeneratedFilenames) != 0
if err != nil {
Expand All @@ -72,6 +81,7 @@ https://github.com/dvaumoron/cornucopia`,
cmdFlags.StringVar(&conf.RepoPath, "repo-path", defaultRepoPath, "Local path of the script cache repository")
cmdFlags.StringVar(&conf.RepoUrl, "repo-addr", defaultRepoUrl, "Address of the shared script repository")
cmdFlags.BoolVarP(&conf.ForceDownload, "force-download", "f", false, "Force download in module resolution")
cmdFlags.BoolVarP(&conf.Verbose, "verbose", "v", false, "Verbose output")

return cmd
}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
RepoPath string
RepoUrl string
ForceDownload bool
Verbose bool
}

func InitDefault(envRepoPathName string, envRepoUrlName string) (string, string, error) {
Expand Down
10 changes: 10 additions & 0 deletions module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package module
import (
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -85,11 +86,20 @@ func (ml ModuleLoader) read(modulename string) ([]byte, error) {
return data, err
}

verbose := ml.conf.Verbose
if verbose {
fmt.Println("Failed to resolve", modulename, "from current directory :", err)
}

modulePath := path.Join(ml.conf.RepoPath, modulename)
if !ml.conf.ForceDownload {
if data, err = os.ReadFile(modulePath); err == nil {
return data, nil
}

if verbose {
fmt.Println("Failed to resolve", modulename, "from", modulePath, ":", err)
}
}

moduleUrl, err := url.JoinPath(ml.conf.RepoUrl, modulename)
Expand Down

0 comments on commit da63576

Please sign in to comment.