1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2026-05-02 20:22:23 +02:00
Files
goreleaser/cmd/init.go
T

75 lines
1.8 KiB
Go
Raw Normal View History

package cmd
import (
2022-06-21 21:11:15 -03:00
"fmt"
"os"
2023-07-18 12:20:09 +00:00
"regexp"
2022-06-21 21:11:15 -03:00
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/static"
2022-05-06 20:38:50 -03:00
"github.com/spf13/cobra"
)
type initCmd struct {
2022-05-06 20:38:50 -03:00
cmd *cobra.Command
config string
}
2023-07-18 12:20:09 +00:00
const gitignorePath = ".gitignore"
func newInitCmd() *initCmd {
2021-03-05 16:49:44 -03:00
root := &initCmd{}
2022-05-06 20:38:50 -03:00
cmd := &cobra.Command{
2023-06-06 06:21:17 +03:00
Use: "init",
Aliases: []string{"i"},
Short: "Generates a .goreleaser.yaml file",
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
2023-07-18 12:20:09 +00:00
RunE: func(_ *cobra.Command, _ []string) error {
if _, err := os.Stat(root.config); err == nil {
return fmt.Errorf("%s already exists, delete it and run the command again", root.config)
}
2021-03-05 16:49:44 -03:00
conf, err := os.OpenFile(root.config, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_EXCL, 0o644)
if err != nil {
return err
}
2021-03-05 16:49:44 -03:00
defer conf.Close()
2022-06-21 21:11:15 -03:00
log.Infof(boldStyle.Render(fmt.Sprintf("Generating %s file", root.config)))
2022-11-25 15:26:14 -03:00
if _, err := conf.Write(static.ExampleConfig); err != nil {
2021-03-05 16:49:44 -03:00
return err
}
2023-07-18 12:20:09 +00:00
if !hasDistIgnored(gitignorePath) {
gitignore, err := os.OpenFile(gitignorePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return err
}
defer gitignore.Close()
if _, err := gitignore.WriteString("\ndist/\n"); err != nil {
return err
}
}
log.WithField("file", root.config).Info("config created; please edit accordingly to your needs")
return nil
},
}
cmd.Flags().StringVarP(&root.config, "config", "f", ".goreleaser.yaml", "Load configuration from file")
2023-06-06 06:21:17 +03:00
_ = cmd.MarkFlagFilename("config", "yaml", "yml")
root.cmd = cmd
return root
}
2023-07-18 12:20:09 +00:00
func hasDistIgnored(path string) bool {
bts, err := os.ReadFile(path)
if err != nil {
return false
}
exp := regexp.MustCompile("(?m)^dist/$")
return exp.Match(bts)
}