git-project-manager/cmd/config_generate.go
Ryan McGuire b944af140a Add multi-remote support for GitLab (#1)
Co-authored-by: Ryan D McGuire <ryand_mcguire@sweetwater.com>
Reviewed-on: 50W/git-project-manager#1
2024-01-14 15:33:15 +00:00

136 lines
3.5 KiB
Go

package cmd
import (
"bytes"
"fmt"
"os"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"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) {
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 {
var c bytes.Buffer
enc := yaml.NewEncoder(&c)
enc.SetIndent(2)
enc.Encode(newConf)
plog.Info("Suggest running with --write or redirect (> " + configFile + ")")
fmt.Print(c.String())
}
}
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 {
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)
}
if host, err := pterm.DefaultInteractiveTextInput.
WithDefaultValue(gitlabConfig.Host).
WithDefaultText("Enter gitlab URL").
Show(); err == nil {
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
}
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
}
}
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
}
return c
}
func init() {
configCmd.AddCommand(configGenerateCmd)
configGenerateCmd.PersistentFlags().Bool("prompt", false, "Prompt for settings")
configGenerateCmd.PersistentFlags().Bool("write", false, "Write config to file")
}