git-project-manager/cmd/config_generate.go

156 lines
4.1 KiB
Go
Raw Normal View History

2023-12-10 15:10:46 +00:00
package cmd
import (
2023-12-29 14:22:19 +00:00
"bytes"
2023-12-10 15:10:46 +00:00
"fmt"
2023-12-11 15:39:48 +00:00
"os"
2023-12-10 15:10:46 +00:00
2023-12-11 15:39:48 +00:00
"github.com/pterm/pterm"
2023-12-10 15:10:46 +00:00
"github.com/spf13/cobra"
2023-12-11 15:39:48 +00:00
"github.com/spf13/viper"
2024-10-01 18:29:14 +00:00
"gopkg.in/yaml.v3"
2023-12-10 15:10:46 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
2023-12-10 15:10:46 +00:00
)
var configGenerateCmd = &cobra.Command{
Use: "generate",
Short: "Generate a default configuration",
Aliases: []string{"gen", "new", "default"},
Long: configGenCmdLong,
Run: runConfigGenerateCmd,
}
func runConfigGenerateCmd(cmd *cobra.Command, args []string) {
2023-12-11 15:39:48 +00:00
var configFile string
if viper.ConfigFileUsed() != "" {
configFile = viper.ConfigFileUsed()
} else {
configFile = defConfigPath
}
plog.Debug("Using config file " + configFile)
configFile, _ = resolvePath(configFile)
newConf := promptConfigSettings(&conf)
2024-10-01 18:29:14 +00:00
if write, _ := cmd.Flags().GetBool(FlagWrite); write {
2023-12-11 15:39:48 +00:00
write, _ := pterm.DefaultInteractiveContinue.
WithDefaultText("Really write config file?").
WithOptions([]string{"yes", "no"}).
Show()
if write != "yes" {
plog.Fatal("Aborting config file write")
}
writeConfigFile(newConf, configFile)
plog.Info("Wrote config to file",
plog.Args("file", configFile))
} else {
2023-12-29 14:22:19 +00:00
var c bytes.Buffer
enc := yaml.NewEncoder(&c)
enc.SetIndent(2)
enc.Encode(newConf)
2023-12-11 15:39:48 +00:00
plog.Info("Suggest running with --write or redirect (> " + configFile + ")")
2023-12-29 14:22:19 +00:00
fmt.Print(c.String())
2023-12-11 15:39:48 +00:00
}
}
func writeConfigFile(c *config.Config, path string) {
2024-10-01 18:29:14 +00:00
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o640)
2023-12-11 15:39:48 +00:00
if err != nil {
plog.Error("Failed to prepare config for writing", plog.Args("error", err))
}
enc := yaml.NewEncoder(file)
if err := enc.Encode(c); err != nil {
plog.Fatal("Failed writing config file", plog.Args("file", path))
}
}
func promptConfigSettings(c *config.Config) *config.Config {
2024-01-17 21:56:41 +00:00
var gitRemote *info.RemoteInfo
// Just pick the first if we have one, considering this
// is meant to be a first-time use tool
2024-01-17 21:56:41 +00:00
if len(c.Remotes) > 0 {
gitRemote = &c.Remotes[0]
} else {
2024-01-17 21:56:41 +00:00
gitRemote = &config.DefaultConfig.Remotes[0]
c.Remotes = append(c.Remotes, *gitRemote)
}
2023-12-11 15:39:48 +00:00
if host, err := pterm.DefaultInteractiveTextInput.
2024-01-17 21:56:41 +00:00
WithDefaultValue(gitRemote.Host).
WithDefaultText("Enter remote URL").
2023-12-11 15:39:48 +00:00
Show(); err == nil {
2024-01-17 21:56:41 +00:00
gitRemote.Host = host
}
if name, err := pterm.DefaultInteractiveTextInput.
2024-01-17 21:56:41 +00:00
WithDefaultValue(gitRemote.Name).
WithDefaultText("Enter remote name (e.g. My Private remote)").
Show(); err == nil {
2024-01-17 21:56:41 +00:00
gitRemote.Name = name
}
if token, err := pterm.DefaultInteractiveTextInput.
WithMask("*").
2024-01-17 21:56:41 +00:00
WithDefaultValue(gitRemote.Token).
WithDefaultText("Enter remote Token").
Show(); err == nil {
2024-01-17 21:56:41 +00:00
gitRemote.Token = token
}
if remoteType, err := pterm.DefaultInteractiveSelect.
WithOptions(info.RemoteTypesAll.Strings()).
WithDefaultText("Git Clone Protocol").
Show(); err == nil {
gitRemote.Type = info.GetRemoteTypeFromString(remoteType)
2023-12-11 15:39:48 +00:00
}
2024-01-16 17:48:42 +00:00
if proto, err := pterm.DefaultInteractiveSelect.
WithOptions([]string{string(info.CloneProtoHTTP), string(info.CloneProtoSSH)}).
2024-01-16 17:48:42 +00:00
WithDefaultText("Git Clone Protocol").
Show(); err == nil {
if proto == "ssh" {
2024-01-17 21:56:41 +00:00
gitRemote.CloneProto = info.CloneProtoSSH
2024-01-16 17:48:42 +00:00
} else {
2024-01-17 21:56:41 +00:00
gitRemote.CloneProto = info.CloneProtoHTTP
2024-01-16 17:48:42 +00:00
}
}
2023-12-11 15:39:48 +00:00
if pPath, err := pterm.DefaultInteractiveTextInput.
WithDefaultValue(c.ProjectPath).
WithDefaultText("Enter path for projects and cache").
Show(); err == nil {
c.ProjectPath = pPath
}
if ownerOnly, err := pterm.DefaultInteractiveConfirm.
WithDefaultText("Only cache projects you are owner of?").
Show(); err == nil {
if ownerOnly {
c.Cache.Load.OwnerOnly = true
} else {
c.Cache.Load.OwnerOnly = false
}
}
2023-12-29 14:22:19 +00:00
if dirMode, err := pterm.DefaultInteractiveConfirm.
WithDefaultValue(true).
WithDefaultText("Open project directories instead of main files (yes for vscode)?").
Show(); err == nil {
c.Editor.OpenDirectory = dirMode
}
2023-12-11 15:39:48 +00:00
return c
2023-12-10 15:10:46 +00:00
}
func init() {
configCmd.AddCommand(configGenerateCmd)
2024-10-01 18:29:14 +00:00
configGenerateCmd.PersistentFlags().Bool(FlagPrompt, false, "Prompt for settings")
configGenerateCmd.PersistentFlags().Bool(FlagWrite, false, "Write config to file")
2023-12-10 15:10:46 +00:00
}