Skip to content
Permalink
Browse files

[WIP] Yes option and 'new' command

  • Loading branch information...
Depado committed Apr 15, 2019
1 parent aa70272 commit 56530a22847001ad4735917963c7c040e84f3f88
Showing with 119 additions and 30 deletions.
  1. +14 −1 .drone.yml
  2. +18 −15 README.md
  3. +1 −0 cmd/flags.go
  4. +68 −0 cmd/new.go
  5. +3 −2 cmd/qk/main.go
  6. +15 −12 renderer/render.go
@@ -26,9 +26,22 @@ steps:
path: /go
commands:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.16.0
- ./bin/golangci-lint --version
- ./bin/golangci-lint run

- name: telegram
image: appleboy/drone-telegram
settings:
to: 790376882
format: markdown
token:
from_secret: telegram_token
message: >
*{{repo.owner}}/{{repo.name}}*
[Build {{build.number}}]({{build.link}}) by {{commit.author}} {{#success build.status}}succeeded{{else}}failed{{/success}} in {{buildtime build.started}}
`{{truncate commit.sha 8}}`: "{{commit.message}}"
volumes:
- name: deps
host:
@@ -40,27 +40,32 @@

# Introduction

quokka is a boilerplate engine. It allows you to quickly use boilerplate
Quokka is a boilerplate engine. It allows you to quickly use boilerplate
templates and avoid copy-pasting chunks of code and snippets when you start a
new project.
new project. You can create templates for literally anything you want!

## Example Usages

- Generating your CI/CD configuration file
-

## Features

- **No external dependencies**
quokka is written in Go and thus is compiled to a static binary. Download
Quokka is written in Go and thus is compiled to a static binary. Download
or build it and you're good to go.
- **Local or distant templates**
Either your template is a git repository, an archive stored on a distant
server or a local directory, quokka knows how to handle these.
server or a local directory, Quokka knows how to handle these.
- **Sweet output and prompts**
Thanks to the wonderful [survey](https://github.com/AlecAivazis/survey)
library, the prompts are unified, can display an help text and support
validation.
- **Clean configuration files**
quokka uses YAML for its configuration file formats, making them clean
Quokka uses YAML for its configuration file formats, making them clean
and easy to read.
- **Powerful templating system**
quokka uses [Go's template system](https://golang.org/pkg/text/template/)
Quokka uses [Go's template system](https://golang.org/pkg/text/template/)
to render the boilerplate.
- **Configuration override**
Need a different behavior or additional variables in a specific directory?
@@ -70,11 +75,11 @@ new project.
Each variable can have its own subset of variables which will only be
prompted to the user if the parent variable is filled or set to true.
- **Customizable templates**
quokka enables fine-grained control over what needs to be done when
Quokka enables fine-grained control over what needs to be done when
rendering the template. Just copy the file, ignore it, add conditionals based
on what the user answered, change the template delimiters…
- **[On Hold] After render commands**
quokka enables you to define commands to be run once the boilerplate has
Quokka enables you to define commands to be run once the boilerplate has
been rendered. _For security reasons, an explicit flag must be provided by the
user for the commands to be executed_
This feature is currently disabled for security reasons.
@@ -89,25 +94,23 @@ You can grab the latest release from [the release page](https://github.com/Depad

```
$ go get -u github.com/Depado/quokka
$ cd $GOPATH/src/github.com/Depado/projectpml
$ dep ensure
$ cd $GOPATH/src/github.com/Depado/quokka
$ make
```

Or directly install:

```
$ go get -u github.com/Depado/quokka
$ cd $GOPATH/src/github.com/Depado/projectpml
$ dep ensure
$ cd $GOPATH/src/github.com/Depado/quokka
$ make install
```

# Usage

quokka supports various provider to download the templates. It supports
`git`, downloading an archive (`.zip`/`.tar.gz`/`.tar.xz`/...) from internet,
or using a local directory.
Quokka supports various providers to download the templates. It supports
`git`, downloading an archive (`.zip`/`.tar.gz`/`.tar.xz`/...),
or using a local directory.

```
Quokka (qk) is a template engine that enables to render local or distant
@@ -17,6 +17,7 @@ func AddRendererFlags(c *cobra.Command) {
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")
// Git options
c.PersistentFlags().Int("git.depth", 1, "depth of git clone in case of git provider")

@@ -0,0 +1,68 @@
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"
)

// NewQuokkaTemplate will create a new Quokka template with default params
func NewQuokkaTemplate(path string) {
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)
}
} else {
if err = os.MkdirAll(filepath.Dir(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")
}

fmt.Println(answers)
}
@@ -39,6 +39,7 @@ var rootc = &cobra.Command{
viper.GetString("input"),
viper.GetBool("keep"),
viper.GetInt("git.depth"),
viper.GetBool("yes"),
)
},
}
@@ -47,15 +48,15 @@ var rootc = &cobra.Command{
var versionc = &cobra.Command{
Use: "version",
Short: "Show build and version",
Run: func(cmd *cobra.Command, args []string) { fmt.Printf("Build: %s\nVersion: %s\n", Build, Version) },
Run: func(c *cobra.Command, args []string) { fmt.Printf("Build: %s\nVersion: %s\n", Build, Version) },
}

// New command that will create a new empty quokka template
var newc = &cobra.Command{
Use: "new [output] <options>",
Short: "Create a new quokka template",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { fmt.Println("Coming soon") },
Run: func(c *cobra.Command, args []string) { cmd.NewQuokkaTemplate(args[0]) },
}

func main() {
@@ -5,25 +5,28 @@ import (

"github.com/Depado/quokka/provider"
"github.com/Depado/quokka/utils"
"github.com/spf13/viper"
"gopkg.in/AlecAivazis/survey.v1"
)

// Render is the main render function
func Render(template, output, toutput, path, input string, keep bool, depth int) {
func Render(template, output, toutput, path, input string, keep bool, depth int, yes bool) {
var err error
var tpath string

if _, err = os.Stat(output); !os.IsNotExist(err) {
var confirmed bool
prompt := &survey.Confirm{
Help: "qk will only affect already existing files that match the template you're trying to render",
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{
Help: "qk will only affect already existing files that match the template you're trying to render",
Message: "The output destination already exists. Continue ?",
}
survey.AskOne(prompt, &confirmed, nil) // nolint: errcheck
if !confirmed {
utils.ErrPrintln("Canceled operation")
os.Exit(0)
}
}
}

@@ -35,7 +38,7 @@ func Render(template, output, toutput, path, input string, keep bool, depth int)
}

// Delete the template if needed
if !viper.GetBool("keep") && p.UsesTmp() {
if !keep && p.UsesTmp() {
defer func(p string) {
os.RemoveAll(path)
utils.OkPrintln("Removed template", utils.Green.Sprint(path))

0 comments on commit 56530a2

Please sign in to comment.
You can’t perform that action at this time.