From 6801759b5505d5aa9085f61b9ac99b0ce567f867 Mon Sep 17 00:00:00 2001 From: Paul Lhussiez Date: Mon, 15 Apr 2019 11:18:46 +0200 Subject: [PATCH] [WIP] New option with optional survey --- .drone.yml | 2 +- cmd/flags.go | 31 ++++++++++++++++---- cmd/new.go | 77 ++++++++++++++++++++++---------------------------- cmd/qk/main.go | 15 +++++++++- 4 files changed, 73 insertions(+), 52 deletions(-) diff --git a/.drone.yml b/.drone.yml index 6dff3ad..b65528c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -36,7 +36,7 @@ steps: token: from_secret: telegram_token message: > - *{{repo.owner}}/{{repo.name}}* + *{{repo.name}}* [Build {{build.number}}]({{build.link}}) by {{commit.author}} {{#success build.status}}succeeded{{else}}failed{{/success}} in {{buildtime build.started}} diff --git a/cmd/flags.go b/cmd/flags.go index 7b55f2a..9c40660 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -13,22 +13,41 @@ func AddRendererFlags(c *cobra.Command) { // c.PersistentFlags().BoolP("commands", "c", false, "execute the after commands (make sure you know what it does)") // General options - c.PersistentFlags().StringP("input", "i", "", "specify an input values file to automate template rendering") - c.PersistentFlags().BoolP("keep", "k", false, "do not delete the template when operation is complete") - c.PersistentFlags().StringP("path", "p", "", "specify if the template is actually stored in a sub-directory of the downloaded file") - c.PersistentFlags().StringP("output", "o", "", "specify the directory where the template should be downloaded or cloned") - c.PersistentFlags().BoolP("yes", "y", false, "Automatically accept") + c.Flags().StringP("input", "i", "", "specify an input values file to automate template rendering") + c.Flags().BoolP("keep", "k", false, "do not delete the template when operation is complete") + c.Flags().StringP("path", "p", "", "specify if the template is actually stored in a sub-directory of the downloaded file") + c.Flags().StringP("output", "o", "", "specify the directory where the template should be downloaded or cloned") // Git options - c.PersistentFlags().Int("git.depth", 1, "depth of git clone in case of git provider") + c.Flags().Int("git.depth", 1, "depth of git clone in case of git provider") // TODO: Handle auth for HTTP Provider // c.PersistentFlags().String("user", "", "user for auth if needed") // c.PersistentFlags().String("password", "", "password for auth if needed") + if err := viper.BindPFlags(c.Flags()); err != nil { + log.Fatal("Could not bind flags") + } +} + +// AddGlobalFlags adds the persistent flags that will be added to all the +// commands +func AddGlobalFlags(c *cobra.Command) { + c.PersistentFlags().BoolP("yes", "y", false, "Automatically accept") + c.PersistentFlags().Bool("debug", false, "Enable or disable debug mode") if err := viper.BindPFlags(c.PersistentFlags()); err != nil { log.Fatal("Could not bind flags") } } +// AddNewFlags will apply the flags to the +func AddNewFlags(c *cobra.Command) { + c.Flags().StringP("name", "n", "", "name of the new template") + c.Flags().StringP("description", "d", "", "description of the new template") + c.Flags().StringP("version", "v", "", "version of the new template") + if err := viper.BindPFlags(c.Flags()); err != nil { + log.Fatal("Could not bind flags") + } +} + // Initialize will be run when cobra finishes its initialization func Initialize() { log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime)) diff --git a/cmd/new.go b/cmd/new.go index 306358e..01b6ef7 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "os" - "path/filepath" "gopkg.in/AlecAivazis/survey.v1" @@ -11,58 +10,48 @@ import ( "github.com/Depado/quokka/utils" ) +func askIfNotString(in *string, name, message, def string, debug bool) { + var err error + if *in == "" { + if err = survey.AskOne(&survey.Input{ + Message: message, + Default: def, + }, in, nil); err != nil { + utils.ErrPrintln("Canceled operation") + os.Exit(0) + } + } else if debug { + utils.OkPrintln(utils.Green.Sprint(name), "already filled:", *in) + } +} + // NewQuokkaTemplate will create a new Quokka template with default params -func NewQuokkaTemplate(path string) { +func NewQuokkaTemplate(path, name, description, version string, yes, debug bool) { var err error if _, err = os.Stat(path); !os.IsNotExist(err) { - var confirmed bool - prompt := &survey.Confirm{ - Message: "The output destination already exists. Continue ?", - } - survey.AskOne(prompt, &confirmed, nil) // nolint: errcheck - if !confirmed { - utils.ErrPrintln("Canceled operation") - os.Exit(0) + if yes { + utils.OkPrintln("Output destination already exists but 'yes' option was used") + } else { + var confirmed bool + prompt := &survey.Confirm{ + Message: "The output destination already exists. Continue ?", + } + survey.AskOne(prompt, &confirmed, nil) // nolint: errcheck + if !confirmed { + utils.ErrPrintln("Canceled operation") + os.Exit(0) + } } } else { - if err = os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { + if err = os.MkdirAll(path, os.ModePerm); err != nil { utils.FatalPrintln("Unable to create directory") } } - var qs = []*survey.Question{ - { - Name: "name", - Prompt: &survey.Input{ - Message: "Name of the template?", - Default: "Quokka Template", - }, - }, - { - Name: "description", - Prompt: &survey.Input{ - Message: "Description of the template?", - Default: "New Quokka Template", - }, - }, - { - Name: "version", - Prompt: &survey.Input{ - Message: "Version of the template?", - Default: "0.1.0", - }, - }, - } - answers := struct { - Name string // survey will match the question and field names - Description string `survey:"color"` // or you can tag fields to match a specific name - Version int // if the types don't match exactly, survey will try to convert for you - }{} - // perform the questions - if err = survey.Ask(qs, &answers); err != nil { - utils.FatalPrintln("Unable to ask questions") - } + askIfNotString(&name, "name", "Template name?", "Quokka Template", debug) + askIfNotString(&description, "description", "Template description?", "New Quokka Template", debug) + askIfNotString(&version, "version", "Template version?", "0.1.0", debug) - fmt.Println(answers) + fmt.Printf("Name: %s\nDescription: %s\nVersion: %s\n", name, description, version) } diff --git a/cmd/qk/main.go b/cmd/qk/main.go index 0414310..cc8aa7d 100644 --- a/cmd/qk/main.go +++ b/cmd/qk/main.go @@ -56,14 +56,27 @@ var newc = &cobra.Command{ Use: "new [output] ", Short: "Create a new quokka template", Args: cobra.MinimumNArgs(1), - Run: func(c *cobra.Command, args []string) { cmd.NewQuokkaTemplate(args[0]) }, + Run: func(c *cobra.Command, args []string) { + cmd.NewQuokkaTemplate( + args[0], + viper.GetString("name"), + viper.GetString("description"), + viper.GetString("version"), + viper.GetBool("yes"), + viper.GetBool("debug"), + ) + }, } func main() { // Initialize Cobra and Viper cobra.OnInitialize(cmd.Initialize) cmd.AddRendererFlags(rootc) + cmd.AddGlobalFlags(rootc) + + // Add extra flags rootc.AddCommand(versionc) + cmd.AddNewFlags(newc) rootc.AddCommand(newc) // Run the command