Skip to content
Permalink
Browse files

fix: CLI improvements (#936)

* fix: CLI improvements

* fix: CLI improvements

* fix: CLI improvements

* chore: lint
  • Loading branch information...
caarlos0 committed Jan 22, 2019
1 parent d730f3e commit 17a894981f92fe8cf623d9d2507c42496a6b3b9a
Showing with 106 additions and 45 deletions.
  1. +1 −1 go.mod
  2. +2 −0 go.sum
  3. +33 −0 internal/static/config.go
  4. +4 −0 internal/static/doc.go
  5. +54 −0 internal/static/usage.go
  6. +12 −44 main.go
2 go.mod
@@ -2,7 +2,6 @@ module github.com/goreleaser/goreleaser

require (
github.com/Masterminds/semver v1.4.2
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/apex/log v1.1.0
github.com/aws/aws-sdk-go v1.15.64
github.com/caarlos0/ctrlc v1.0.0
@@ -25,5 +24,6 @@ require (
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f
golang.org/x/sys v0.0.0-20181030150119-7e31e0c00fa0 // indirect
google.golang.org/appengine v1.2.0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/yaml.v2 v2.2.1
)
2 go.sum
@@ -65,6 +65,8 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.2.0 h1:S0iUepdCWODXRvtE+gcRDd15L+k+k1AiHlMiMjefH24=
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
@@ -0,0 +1,33 @@
package static

// ExampleConfig is the config used within goreleaser init.
// nolint: gochecknoglobals
const ExampleConfig = `# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
# you may remove this if you don't use vgo
- go mod download
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
archive:
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
`
@@ -0,0 +1,4 @@
// Package static contains static "text" files, just because embedding real
// static files would be kind of an overengineering right now, so it's just
// strings in go code really.
package static
@@ -0,0 +1,54 @@
package static

// UsageTemplate for goreleaser CLI.
// nolint: gochecknoglobals
const UsageTemplate = `{{define "FormatCommand"}}\
{{if .FlagSummary}} {{.FlagSummary}}{{end}}\
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\
{{end}}\
{{define "FormatCommands"}}\
{{range .FlattenedCommands}}\
{{if not .Hidden}}\
{{.FullCommand}}{{template "FormatCommand" .}}{{if .Default}} (default){{end}}
{{.Help|Wrap 4}}
{{end}}\
{{end}}\
{{end}}\
{{define "FormatUsage"}}\
{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{with .Context.SelectedCommand.Help}}\
{{. | Wrap 0}}
{{end}}\
{{else}}\
{{.App.Help | Wrap 0}}
{{end}}\
USAGE:
{{if .Context.SelectedCommand}}\
$ {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatUsage" .Context.SelectedCommand}}
{{else}}\
$ {{.App.Name}}{{template "FormatUsage" .App}}
{{end}}\
{{if .Context.Flags}}\
FLAGS:
{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.Args}}\
ARGS:
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if len .Context.SelectedCommand.Commands}}\
SUBCOMMANDS:
{{template "FormatCommands" .Context.SelectedCommand}}
{{end}}\
{{else if .App.Commands}}\
COMMANDS:
{{template "FormatCommands" .App}}
{{end}}\
`
56 main.go
@@ -7,15 +7,16 @@ import (
"strings"
"time"

"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/caarlos0/ctrlc"
"github.com/fatih/color"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/pipeline"
"github.com/goreleaser/goreleaser/internal/static"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

// nolint: gochecknoglobals
@@ -49,6 +50,7 @@ func main() {
defer fmt.Println()

var app = kingpin.New("goreleaser", "Deliver Go binaries as fast and easily as possible")
var debug = app.Flag("debug", "Enable debug mode").Bool()
var initCmd = app.Command("init", "Generates a .goreleaser.yml file").Alias("i")
var releaseCmd = app.Command("release", "Releases the current project").Alias("r").Default()
var config = releaseCmd.Flag("config", "Load configuration from file").Short('c').Short('f').PlaceHolder(".goreleaser.yml").String()
@@ -59,19 +61,23 @@ func main() {
var skipValidate = releaseCmd.Flag("skip-validate", "Skips all git sanity checks").Bool()
var rmDist = releaseCmd.Flag("rm-dist", "Remove the dist folder before building").Bool()
var parallelism = releaseCmd.Flag("parallelism", "Amount of slow tasks to do in concurrently").Short('p').Default("4").Int() // TODO: use runtime.NumCPU here?
var debug = releaseCmd.Flag("debug", "Enable debug mode").Bool()
var timeout = releaseCmd.Flag("timeout", "Timeout to the entire release process").Default("30m").Duration()

app.Version(fmt.Sprintf("%v, commit %v, built at %v", version, commit, date))
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
app.UsageTemplate(static.UsageTemplate)

switch kingpin.MustParse(app.Parse(os.Args[1:])) {
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
if *debug {
log.SetLevel(log.DebugLevel)
}
switch cmd {
case initCmd.FullCommand():
var filename = ".goreleaser.yml"
if err := initProject(filename); err != nil {
log.WithError(err).Error("failed to init project")
terminate(1)
os.Exit(1)
return
}
log.WithField("file", filename).Info("config created; please edit accordingly to your needs")
@@ -92,21 +98,14 @@ func main() {
}
if err := releaseProject(options); err != nil {
log.WithError(err).Errorf(color.New(color.Bold).Sprintf("release failed after %0.2fs", time.Since(start).Seconds()))
terminate(1)
os.Exit(1)
return
}
log.Infof(color.New(color.Bold).Sprintf("release succeeded after %0.2fs", time.Since(start).Seconds()))
}
}

func terminate(status int) {
os.Exit(status)
}

func releaseProject(options releaseOptions) error {
if options.Debug {
log.SetLevel(log.DebugLevel)
}
cfg, err := loadConfig(options.Config)
if err != nil {
return err
@@ -169,7 +168,7 @@ func initProject(filename string) error {
return fmt.Errorf("%s already exists", filename)
}
log.Infof(color.New(color.Bold).Sprintf("Generating %s file", filename))
return ioutil.WriteFile(filename, []byte(exampleConfig), 0644)
return ioutil.WriteFile(filename, []byte(static.ExampleConfig), 0644)
}

func loadConfig(path string) (config.Project, error) {
@@ -193,34 +192,3 @@ func loadConfig(path string) (config.Project, error) {
log.Warn("could not load config, using defaults")
return config.Project{}, nil
}

// nolint: gochecknoglobals
var exampleConfig = `# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
# you may remove this if you don't use vgo
- go mod download
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
archive:
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
`

0 comments on commit 17a8949

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