changelog/cmd/init.go
jolheiser 8156d742f5
All checks were successful
goreleaser / goreleaser (push) Successful in 3m41s
Update to urfave/cli/v3 (#77)
This allows for persistent flags, such that `changelog -m v0.4.0 generate` and `changelog generate -m v0.4.0` are equivalent.

Reviewed-on: #77
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2023-04-03 11:51:25 +08:00

47 lines
951 B
Go

// 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/v3"
)
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(_ *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
}