diff --git a/CHANGELOG.md b/CHANGELOG.md index 466e9f1..5137a6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [0.1.0](https://gitea.com/gitea/changelog/pulls?q=&type=all&state=closed&milestone=1231) - 2020-01-25 * FEATURES + * Add init command (#33) * Add subcommand to display contributors of milestone (#10) * BUGFIXES * Fix README about import path (#22) diff --git a/changelog.example.go b/changelog.example.go index 2db8d32..b9baf98 100644 --- a/changelog.example.go +++ b/changelog.example.go @@ -22,7 +22,7 @@ const ( package config func init() { - defaultConfig = []byte(` + "`" + `%s` + "`" + `) + DefaultConfig = []byte(` + "`" + `%s` + "`" + `) } ` ) diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000..0666cfc --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,46 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "fmt" + "io/ioutil" + "os" + + "code.gitea.io/changelog/config" + + "github.com/urfave/cli/v2" +) + +var ( + Init = &cli.Command{ + Name: "init", + Usage: "Initialize a default .changelog.yml", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "name", + Aliases: []string{"n"}, + Usage: "Name of the changelog config", + Value: ".changelog.yml", + Destination: &nameFlag, + }, + }, + Action: runInit, + } + nameFlag string +) + +func runInit(cmd *cli.Context) error { + if _, err := os.Stat(nameFlag); err == nil { + return fmt.Errorf("file '%s' already exists", nameFlag) + } + + if err := ioutil.WriteFile(nameFlag, config.DefaultConfig, os.ModePerm); err != nil { + return err + } + + fmt.Printf("Config initialized at '%s'\n", nameFlag) + return nil +} diff --git a/config/config.go b/config/config.go index e265b03..67e41f6 100644 --- a/config/config.go +++ b/config/config.go @@ -11,7 +11,7 @@ import ( "gopkg.in/yaml.v2" ) -var defaultConfig []byte +var DefaultConfig []byte // Group is a grouping of PRs type Group struct { @@ -35,7 +35,7 @@ func New(configPath string) (*Config, error) { var err error var configContent []byte if len(configPath) == 0 { - configContent = defaultConfig + configContent = DefaultConfig } else { configContent, err = ioutil.ReadFile(configPath) if err != nil { diff --git a/config/config_default.go b/config/config_default.go index 7113f98..b224d64 100644 --- a/config/config_default.go +++ b/config/config_default.go @@ -5,7 +5,7 @@ package config func init() { - defaultConfig = []byte(`# The full repository name + DefaultConfig = []byte(`# The full repository name repo: go-gitea/gitea # Service type (gitea or github) diff --git a/main.go b/main.go index e828239..db092bf 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "os" "code.gitea.io/changelog/cmd" + "github.com/urfave/cli/v2" ) @@ -30,7 +31,6 @@ func main() { Name: "milestone", Aliases: []string{"m"}, Usage: "Targeted milestone", - Required: true, Destination: &cmd.MilestoneFlag, }, &cli.StringFlag{ @@ -61,6 +61,7 @@ func main() { Commands: []*cli.Command{ cmd.Generate, cmd.Contributors, + cmd.Init, }, }