-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
790040b
commit ab8b54a
Showing
3 changed files
with
150 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2022 Iván Corrales | ||
*/ | ||
package help | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func New() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "help [command]", | ||
Short: `Help about any command.`, | ||
Long: `Help provides help for any command in the application. | ||
Simply type templatizer help [path to command] for full details.`, | ||
Example: "templatizer help run", | ||
Run: run, | ||
} | ||
} | ||
|
||
// RunHelp checks given arguments and executes command. | ||
func run(cmd *cobra.Command, args []string) { | ||
foundCmd, _, err := cmd.Root().Find(args) | ||
switch { | ||
case foundCmd == nil: | ||
cmd.Printf("Unknown help topic %#q.\n", args) | ||
if usageErr := cmd.Root().Usage(); usageErr != nil { | ||
panic(usageErr) | ||
} | ||
return | ||
case err != nil: | ||
cmd.Println(err) | ||
argsString := strings.Join(args, " ") | ||
matchedMsgIsPrinted := false | ||
for _, foundCmd := range foundCmd.Commands() { | ||
if strings.Contains(foundCmd.Short, argsString) { | ||
if !matchedMsgIsPrinted { | ||
cmd.Printf("Matchers of string '%s' in short descriptions of commands: \n", argsString) | ||
matchedMsgIsPrinted = true | ||
} | ||
cmd.Printf(" %-14s %s\n", foundCmd.Name(), foundCmd.Short) | ||
} | ||
} | ||
if !matchedMsgIsPrinted { | ||
if err := cmd.Root().Usage(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
return | ||
default: | ||
if len(args) == 0 { | ||
foundCmd = cmd | ||
} | ||
helpFunc := foundCmd.HelpFunc() | ||
helpFunc(foundCmd, args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package run | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/wesovilabs/templatizer/internal/action" | ||
) | ||
|
||
var debug bool | ||
|
||
var repoPath, username, password, templateMode, targetDir, inputPath string | ||
|
||
func New() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "run", | ||
Short: "Create a project from a given template", | ||
Long: `If flag --input is provided will create the repository from the template otherwise | ||
It will create a param file to be filled`, | ||
Example: "templatizer run --source github.com/ivancorrales/go-rest-template", | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
log.SetFormatter(&log.TextFormatter{}) | ||
if debug { | ||
log.Info("Debug logs enabled...") | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
}, | ||
Run: run, | ||
} | ||
|
||
cmd.PersistentFlags().BoolVar(&debug, "verbose", false, | ||
"verbose logging") | ||
cmd.PersistentFlags().StringVar(&repoPath, "source", "", | ||
"path to the repo i.e. github.com/ivancorrales/go-rest-template)") | ||
cmd.PersistentFlags().StringVarP(&username, "username", "u", "", | ||
"user handle") | ||
cmd.PersistentFlags().StringVarP(&password, "password", "p", "", | ||
"user secret for the provided username") | ||
cmd.PersistentFlags().StringVarP(&templateMode, "mode", "m", "goTemplate", | ||
"template mode used tod efine the variables. ") | ||
cmd.PersistentFlags().StringVarP(&inputPath, "input", "i", "", | ||
"path to the input files that contains the variables") | ||
cmd.PersistentFlags().StringVar(&targetDir, "target", "", | ||
"Path to the folder in which the repository will be created") | ||
return cmd | ||
} | ||
|
||
func run(cmd *cobra.Command, args []string) { | ||
log.Info("running templatizer...") | ||
for i := range args { | ||
println(args[i]) | ||
} | ||
println() | ||
if repoPath == "" { | ||
log.Error("missing required flag 'from'") | ||
log.Info("Please run `templatizer help") | ||
os.Exit(1) | ||
} | ||
options := []action.Option{ | ||
action.WithRepoPath(repoPath), | ||
} | ||
if inputPath != "" { | ||
options = append(options, action.WithVariables(inputPath)) | ||
} | ||
if username != "" { | ||
options = append(options, action.WithUsername(username)) | ||
} | ||
if password != "" { | ||
options = append(options, action.WithPassword(password)) | ||
} | ||
if templateMode != "" { | ||
options = append(options, action.WithTemplateMode(templateMode)) | ||
} | ||
if targetDir != "" { | ||
options = append(options, action.WithTargetDir(targetDir)) | ||
} | ||
err := action.New(options...).Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} |