Skip to content

Commit

Permalink
[WIP] New option with optional survey
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lhussiez committed Apr 15, 2019
1 parent 56530a2 commit 6801759
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
31 changes: 25 additions & 6 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
77 changes: 33 additions & 44 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,55 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"gopkg.in/AlecAivazis/survey.v1"

_ "github.com/Depado/quokka/conf" // Import conf to ensure package init for survey
"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)
}
15 changes: 14 additions & 1 deletion cmd/qk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,27 @@ var newc = &cobra.Command{
Use: "new [output] <options>",
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
Expand Down

0 comments on commit 6801759

Please sign in to comment.