Skip to content

Commit

Permalink
Make changes in command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ivancorrales committed Mar 1, 2022
1 parent 790040b commit ab8b54a
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 70 deletions.
59 changes: 59 additions & 0 deletions cmd/templatizer/help/help.go
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)
}
}
79 changes: 9 additions & 70 deletions cmd/templatizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,23 @@ package main
import (
"os"

"github.com/wesovilabs/templatizer/internal/action"
"github.com/wesovilabs/templatizer/cmd/templatizer/help"
"github.com/wesovilabs/templatizer/cmd/templatizer/run"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var debug bool
var repoPath, username, password, templateMode, targetDir, inputPath string

func command() *cobra.Command {
cmd := &cobra.Command{
Use: "templatizer",
Short: `Generate a json file with empty variables to be filled.`,
Long: `
`,
Example: "init --from 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: runTemplatizer,
}

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 main() {
rootCmd := command()
rootCmd := &cobra.Command{
Use: "templatizer [cmd]",
}
runCmd := run.New()
helpCmd := help.New()
rootCmd.SetHelpCommand(helpCmd)
rootCmd.AddCommand(helpCmd, runCmd)
if err := rootCmd.Execute(); err != nil {
log.Errorf("unexpected error: %s", err)
os.Exit(1)
}
}

func runTemplatizer(cmd *cobra.Command, args []string) {
log.Info("running templatizer...")
if repoPath == "" {
log.Error("missing required flag 'from'.\n Example: 'templatizer --from github.com/organization/repository.git'")
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)
}
}
82 changes: 82 additions & 0 deletions cmd/templatizer/run/run.go
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)
}
}

0 comments on commit ab8b54a

Please sign in to comment.