1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00
goreleaser/cmd/init.go
Carlos Alexandro Becker ec2db4a727
feat!: rename module to /v2 (#4894)
<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Also, add tests and the respective documentation changes as well.

-->


<!-- If applied, this commit will... -->

...

<!-- Why is this change being made? -->

...

<!-- # Provide links to any relevant tickets, URLs or other resources
-->

...

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2024-05-26 15:02:57 -03:00

75 lines
1.8 KiB
Go

package cmd
import (
"fmt"
"os"
"regexp"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/v2/internal/static"
"github.com/spf13/cobra"
)
type initCmd struct {
cmd *cobra.Command
config string
}
const gitignorePath = ".gitignore"
func newInitCmd() *initCmd {
root := &initCmd{}
cmd := &cobra.Command{
Use: "init",
Aliases: []string{"i"},
Short: "Generates a .goreleaser.yaml file",
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
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)
}
conf, err := os.OpenFile(root.config, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_EXCL, 0o644)
if err != nil {
return err
}
defer conf.Close()
log.Infof(boldStyle.Render(fmt.Sprintf("Generating %s file", root.config)))
if _, err := conf.Write(static.ExampleConfig); err != nil {
return err
}
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")
_ = cmd.MarkFlagFilename("config", "yaml", "yml")
root.cmd = cmd
return root
}
func hasDistIgnored(path string) bool {
bts, err := os.ReadFile(path)
if err != nil {
return false
}
exp := regexp.MustCompile("(?m)^dist/$")
return exp.Match(bts)
}