Add init command (#33)

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/gitea/changelog/pulls/33
Reviewed-by: 6543 <6543@noreply.gitea.io>
Reviewed-by: Guillermo Prandi <guillep2k@noreply.gitea.io>
This commit is contained in:
John Olheiser 2020-01-27 02:42:24 +00:00 committed by techknowlogick
parent 601bf3f04c
commit 3c1b200883
6 changed files with 53 additions and 5 deletions

View File

@ -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)

View File

@ -22,7 +22,7 @@ const (
package config
func init() {
defaultConfig = []byte(` + "`" + `%s` + "`" + `)
DefaultConfig = []byte(` + "`" + `%s` + "`" + `)
}
`
)

46
cmd/init.go Normal file
View File

@ -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
}

View File

@ -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 {

View File

@ -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)

View File

@ -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,
},
}