2020-04-27 21:42:44 -03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-06-21 21:11:15 -03:00
|
|
|
"fmt"
|
2020-04-27 21:42:44 -03:00
|
|
|
"os"
|
2023-07-18 12:20:09 +00:00
|
|
|
"regexp"
|
2020-04-27 21:42:44 -03:00
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2024-05-26 15:02:57 -03:00
|
|
|
"github.com/goreleaser/goreleaser/v2/internal/static"
|
2022-05-06 20:38:50 -03:00
|
|
|
"github.com/spf13/cobra"
|
2020-04-27 21:42:44 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
type initCmd struct {
|
2022-05-06 20:38:50 -03:00
|
|
|
cmd *cobra.Command
|
2020-04-27 21:42:44 -03:00
|
|
|
config string
|
2024-11-28 22:05:50 -03:00
|
|
|
lang string
|
2020-04-27 21:42:44 -03:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:20:09 +00:00
|
|
|
const gitignorePath = ".gitignore"
|
|
|
|
|
2020-04-27 21:42:44 -03:00
|
|
|
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,
|
2024-11-28 22:58:13 -03:00
|
|
|
PreRun: func(cmd *cobra.Command, _ []string) {
|
2024-11-28 22:05:50 -03:00
|
|
|
if cmd.Flags().Lookup("language").Changed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to figure out which kind of project is this...
|
|
|
|
if _, err := os.Stat("build.zig"); err == nil {
|
|
|
|
root.lang = "zig"
|
|
|
|
log.Info("project contains a 'build.zig', using default zig configuration")
|
|
|
|
return
|
|
|
|
}
|
2024-12-01 15:06:42 -03:00
|
|
|
if _, err := os.Stat("Cargo.toml"); err == nil {
|
|
|
|
root.lang = "rust"
|
|
|
|
log.Info("project contains a 'Cargo.toml', using default rust configuration")
|
|
|
|
return
|
|
|
|
}
|
2024-11-28 22:05:50 -03:00
|
|
|
},
|
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)
|
2020-04-27 21:42:44 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-05 16:49:44 -03:00
|
|
|
defer conf.Close()
|
2020-04-27 21:42:44 -03:00
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
log.Infof(boldStyle.Render(fmt.Sprintf("Generating %s file", root.config)))
|
2024-11-28 22:05:50 -03:00
|
|
|
|
|
|
|
var example []byte
|
|
|
|
switch root.lang {
|
|
|
|
case "zig":
|
|
|
|
example = static.ZigExampleConfig
|
2024-12-01 15:06:42 -03:00
|
|
|
case "rust":
|
|
|
|
example = static.RustExampleConfig
|
2024-11-28 22:05:50 -03:00
|
|
|
case "go":
|
|
|
|
example = static.GoExampleConfig
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid language: %s", root.lang)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := conf.Write(example); 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
|
|
|
|
}
|
2020-04-27 21:42:44 -03:00
|
|
|
}
|
|
|
|
log.WithField("file", root.config).Info("config created; please edit accordingly to your needs")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-11-28 22:05:50 -03:00
|
|
|
cmd.Flags().StringVarP(&root.lang, "language", "l", "go", "Which language will be used")
|
2021-12-04 14:07:55 -03:00
|
|
|
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")
|
2020-04-27 21:42:44 -03:00
|
|
|
|
2024-11-28 22:58:13 -03:00
|
|
|
_ = cmd.RegisterFlagCompletionFunc(
|
|
|
|
"language",
|
|
|
|
cobra.FixedCompletions(
|
2024-12-01 15:06:42 -03:00
|
|
|
[]string{"go", "rust", "zig"},
|
2024-11-28 22:58:13 -03:00
|
|
|
cobra.ShellCompDirectiveDefault,
|
|
|
|
),
|
|
|
|
)
|
2024-11-28 22:05:50 -03:00
|
|
|
|
2020-04-27 21:42:44 -03:00
|
|
|
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)
|
|
|
|
}
|