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"
|
2023-12-10 15:10:46 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
|
2024-01-17 13:13:58 +00:00
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
|
2023-12-10 15:10:46 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if write, _ := cmd.Flags().GetBool("write"); write {
|
|
|
|
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) {
|
|
|
|
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
|
|
|
|
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-14 15:33:15 +00:00
|
|
|
var gitlabConfig *config.GitlabConfig
|
|
|
|
// Just pick the first if we have one, considering this
|
|
|
|
// is meant to be a first-time use tool
|
|
|
|
if len(c.Gitlabs) > 0 {
|
|
|
|
gitlabConfig = &c.Gitlabs[0]
|
|
|
|
} else {
|
|
|
|
gitlabConfig = &config.DefaultConfig.Gitlabs[0]
|
|
|
|
c.Gitlabs = append(c.Gitlabs, *gitlabConfig)
|
|
|
|
}
|
|
|
|
|
2023-12-11 15:39:48 +00:00
|
|
|
if host, err := pterm.DefaultInteractiveTextInput.
|
2024-01-14 15:33:15 +00:00
|
|
|
WithDefaultValue(gitlabConfig.Host).
|
2023-12-11 15:39:48 +00:00
|
|
|
WithDefaultText("Enter gitlab URL").
|
|
|
|
Show(); err == nil {
|
2024-01-14 15:33:15 +00:00
|
|
|
gitlabConfig.Host = host
|
|
|
|
}
|
|
|
|
|
|
|
|
if name, err := pterm.DefaultInteractiveTextInput.
|
|
|
|
WithDefaultValue(gitlabConfig.Name).
|
|
|
|
WithDefaultText("Enter gitlab name (e.g. My Private GitLab)").
|
|
|
|
Show(); err == nil {
|
|
|
|
gitlabConfig.Name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
if token, err := pterm.DefaultInteractiveTextInput.
|
|
|
|
WithMask("*").
|
|
|
|
WithDefaultValue(gitlabConfig.Token).
|
|
|
|
WithDefaultText("Enter gitlab Token").
|
|
|
|
Show(); err == nil {
|
|
|
|
gitlabConfig.Token = token
|
2023-12-11 15:39:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 17:48:42 +00:00
|
|
|
if proto, err := pterm.DefaultInteractiveSelect.
|
2024-01-17 13:13:58 +00:00
|
|
|
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 13:13:58 +00:00
|
|
|
gitlabConfig.CloneProto = info.CloneProtoSSH
|
2024-01-16 17:48:42 +00:00
|
|
|
} else {
|
2024-01-17 13:13:58 +00:00
|
|
|
gitlabConfig.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)
|
2023-12-11 15:39:48 +00:00
|
|
|
configGenerateCmd.PersistentFlags().Bool("prompt", false, "Prompt for settings")
|
|
|
|
configGenerateCmd.PersistentFlags().Bool("write", false, "Write config to file")
|
2023-12-10 15:10:46 +00:00
|
|
|
}
|