github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/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  	var root = &initCmd{}
    19  	var 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  			f, err := os.OpenFile(root.config, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_EXCL, 0644)
    28  			if err != nil {
    29  				return err
    30  			}
    31  			defer f.Close()
    32  
    33  			log.Infof(color.New(color.Bold).Sprintf("Generating %s file", root.config))
    34  			if _, err := f.WriteString(static.ExampleConfig); err != nil {
    35  				return err
    36  			}
    37  
    38  			log.WithField("file", root.config).Info("config created; please edit accordingly to your needs")
    39  			return nil
    40  		},
    41  	}
    42  
    43  	cmd.Flags().StringVarP(&root.config, "config", "f", ".goreleaser.yml", "Load configuration from file")
    44  
    45  	root.cmd = cmd
    46  	return root
    47  }