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"
|
|
|
|
|
2024-12-30 20:50:31 +00:00
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
|
2024-12-19 19:55:49 +00:00
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/config"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-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"},
|
2024-12-30 20:50:31 +00:00
|
|
|
Long: util.ConfigGenCmdLong,
|
2023-12-10 15:10:46 +00:00
|
|
|
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 {
|
2024-12-30 20:50:31 +00:00
|
|
|
configFile = util.DefConfigPath
|
2023-12-11 15:39:48 +00:00
|
|
|
}
|
|
|
|
|
2024-12-30 20:50:31 +00:00
|
|
|
utils.Logger().Debug("Using config file " + configFile)
|
2023-12-11 15:39:48 +00:00
|
|
|
|
2024-12-30 20:50:31 +00:00
|
|
|
configFile, _ = util.ResolvePath(configFile)
|
2023-12-11 15:39:48 +00:00
|
|
|
|
2024-12-30 20:50:31 +00:00
|
|
|
newConf := promptConfigSettings(utils.Config())
|
2023-12-11 15:39:48 +00:00
|
|
|
|
2024-12-30 20:50:31 +00:00
|
|
|
if write, _ := cmd.Flags().GetBool(util.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" {
|
2024-12-30 20:50:31 +00:00
|
|
|
utils.Logger().Fatal("Aborting config file write")
|
2023-12-11 15:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeConfigFile(newConf, configFile)
|
2024-12-30 20:50:31 +00:00
|
|
|
utils.Logger().Info("Wrote config to file",
|
|
|
|
utils.Logger().Args("file", configFile))
|
2023-12-11 15:39:48 +00:00
|
|
|
|
|
|
|
} else {
|
2023-12-29 14:22:19 +00:00
|
|
|
var c bytes.Buffer
|
|
|
|
enc := yaml.NewEncoder(&c)
|
|
|
|
enc.SetIndent(2)
|
|
|
|
enc.Encode(newConf)
|
2024-12-30 20:50:31 +00:00
|
|
|
utils.Logger().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 {
|
2024-12-30 20:50:31 +00:00
|
|
|
utils.Logger().Error("Failed to prepare config for writing", utils.Logger().Args("error", err))
|
2023-12-11 15:39:48 +00:00
|
|
|
}
|
|
|
|
enc := yaml.NewEncoder(file)
|
|
|
|
if err := enc.Encode(c); err != nil {
|
2024-12-30 20:50:31 +00:00
|
|
|
utils.Logger().Fatal("Failed writing config file", utils.Logger().Args("file", path))
|
2023-12-11 15:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func promptConfigSettings(c *config.Config) *config.Config {
|
2024-01-17 21:56:41 +00:00
|
|
|
var gitRemote *info.RemoteInfo
|
2024-01-14 15:33:15 +00:00
|
|
|
// 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]
|
2024-01-14 15:33:15 +00:00
|
|
|
} else {
|
2024-01-17 21:56:41 +00:00
|
|
|
gitRemote = &config.DefaultConfig.Remotes[0]
|
|
|
|
c.Remotes = append(c.Remotes, *gitRemote)
|
2024-01-14 15:33:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
2024-01-14 15:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)").
|
2024-01-14 15:33:15 +00:00
|
|
|
Show(); err == nil {
|
2024-01-17 21:56:41 +00:00
|
|
|
gitRemote.Name = name
|
2024-01-14 15:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if token, err := pterm.DefaultInteractiveTextInput.
|
|
|
|
WithMask("*").
|
2024-01-17 21:56:41 +00:00
|
|
|
WithDefaultValue(gitRemote.Token).
|
|
|
|
WithDefaultText("Enter remote Token").
|
2024-01-14 15:33:15 +00:00
|
|
|
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.
|
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 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() {
|
2024-12-30 20:50:31 +00:00
|
|
|
configGenerateCmd.PersistentFlags().Bool(util.FlagPrompt, false, "Prompt for settings")
|
|
|
|
configGenerateCmd.PersistentFlags().Bool(util.FlagWrite, false, "Write config to file")
|
2023-12-10 15:10:46 +00:00
|
|
|
}
|