github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/cmd/init.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/apex/log"
     7  	"github.com/fatih/color"
     8  	"github.com/goreleaser/goreleaser/internal/static"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  type initCmd struct {
    13  	cmd    *cobra.Command
    14  	config string
    15  }
    16  
    17  func newInitCmd() *initCmd {
    18  	root := &initCmd{}
    19  	cmd := &cobra.Command{
    20  		Use:           "init",
    21  		Aliases:       []string{"i"},
    22  		Short:         "Generates a .goreleaser.yml file",
    23  		SilenceUsage:  true,
    24  		SilenceErrors: true,
    25  		Args:          cobra.NoArgs,
    26  		RunE: func(cmd *cobra.Command, args []string) error {
    27  			conf, err := os.OpenFile(root.config, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_EXCL, 0o644)
    28  			if err != nil {
    29  				return err
    30  			}
    31  			defer conf.Close()
    32  
    33  			log.Infof(color.New(color.Bold).Sprintf("Generating %s file", root.config))
    34  			if _, err := conf.WriteString(static.ExampleConfig); err != nil {
    35  				return err
    36  			}
    37  
    38  			gitignore, err := os.OpenFile(".gitignore", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
    39  			if err != nil {
    40  				return err
    41  			}
    42  			defer gitignore.Close()
    43  			if _, err := gitignore.WriteString("\ndist/\n"); err != nil {
    44  				return err
    45  			}
    46  
    47  			log.WithField("file", root.config).Info("config created; please edit accordingly to your needs")
    48  			return nil
    49  		},
    50  	}
    51  
    52  	cmd.Flags().StringVarP(&root.config, "config", "f", ".goreleaser.yml", "Load configuration from file")
    53  
    54  	root.cmd = cmd
    55  	return root
    56  }