Skip to content

Commit

Permalink
[WIP] Yes option and 'new' command
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lhussiez committed Apr 15, 2019
1 parent aa70272 commit 56530a2
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 30 deletions.
15 changes: 14 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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.
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
68 changes: 68 additions & 0 deletions cmd/new.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 3 additions & 2 deletions cmd/qk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var rootc = &cobra.Command{
viper.GetString("input"),
viper.GetBool("keep"),
viper.GetInt("git.depth"),
viper.GetBool("yes"),
)
},
}
Expand All @@ -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() {
Expand Down
27 changes: 15 additions & 12 deletions renderer/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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))
Expand Down

0 comments on commit 56530a2

Please sign in to comment.