Package subcommand code

This commit is contained in:
2024-12-30 15:50:31 -05:00
parent 96378d047e
commit b9d7d5a4f2
51 changed files with 357 additions and 295 deletions

26
cmd/config/config.go Normal file
View File

@ -0,0 +1,26 @@
package cmd
import (
"github.com/spf13/cobra"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
)
var ConfigCmd = &cobra.Command{
Use: "config",
Short: "Git Project Manager Configuration",
Aliases: []string{"conf"},
Long: util.ConfigCmdLong,
PersistentPreRun: configCmdPreRun,
}
var utils *util.Utils
func configCmdPreRun(cmd *cobra.Command, args []string) {
utils = util.MustFromCtx(cmd.Context())
}
func init() {
ConfigCmd.AddCommand(configGenerateCmd)
ConfigCmd.AddCommand(configShowCmd)
}

View File

@ -0,0 +1,155 @@
package cmd
import (
"bytes"
"fmt"
"os"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/config"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/info"
)
var configGenerateCmd = &cobra.Command{
Use: "generate",
Short: "Generate a default configuration",
Aliases: []string{"gen", "new", "default"},
Long: util.ConfigGenCmdLong,
Run: runConfigGenerateCmd,
}
func runConfigGenerateCmd(cmd *cobra.Command, args []string) {
var configFile string
if viper.ConfigFileUsed() != "" {
configFile = viper.ConfigFileUsed()
} else {
configFile = util.DefConfigPath
}
utils.Logger().Debug("Using config file " + configFile)
configFile, _ = util.ResolvePath(configFile)
newConf := promptConfigSettings(utils.Config())
if write, _ := cmd.Flags().GetBool(util.FlagWrite); write {
write, _ := pterm.DefaultInteractiveContinue.
WithDefaultText("Really write config file?").
WithOptions([]string{"yes", "no"}).
Show()
if write != "yes" {
utils.Logger().Fatal("Aborting config file write")
}
writeConfigFile(newConf, configFile)
utils.Logger().Info("Wrote config to file",
utils.Logger().Args("file", configFile))
} else {
var c bytes.Buffer
enc := yaml.NewEncoder(&c)
enc.SetIndent(2)
enc.Encode(newConf)
utils.Logger().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, 0o640)
if err != nil {
utils.Logger().Error("Failed to prepare config for writing", utils.Logger().Args("error", err))
}
enc := yaml.NewEncoder(file)
if err := enc.Encode(c); err != nil {
utils.Logger().Fatal("Failed writing config file", utils.Logger().Args("file", path))
}
}
func promptConfigSettings(c *config.Config) *config.Config {
var gitRemote *info.RemoteInfo
// Just pick the first if we have one, considering this
// is meant to be a first-time use tool
if len(c.Remotes) > 0 {
gitRemote = &c.Remotes[0]
} else {
gitRemote = &config.DefaultConfig.Remotes[0]
c.Remotes = append(c.Remotes, *gitRemote)
}
if host, err := pterm.DefaultInteractiveTextInput.
WithDefaultValue(gitRemote.Host).
WithDefaultText("Enter remote URL").
Show(); err == nil {
gitRemote.Host = host
}
if name, err := pterm.DefaultInteractiveTextInput.
WithDefaultValue(gitRemote.Name).
WithDefaultText("Enter remote name (e.g. My Private remote)").
Show(); err == nil {
gitRemote.Name = name
}
if token, err := pterm.DefaultInteractiveTextInput.
WithMask("*").
WithDefaultValue(gitRemote.Token).
WithDefaultText("Enter remote Token").
Show(); err == nil {
gitRemote.Token = token
}
if remoteType, err := pterm.DefaultInteractiveSelect.
WithOptions(info.RemoteTypesAll.Strings()).
WithDefaultText("Git Clone Protocol").
Show(); err == nil {
gitRemote.Type = info.GetRemoteTypeFromString(remoteType)
}
if proto, err := pterm.DefaultInteractiveSelect.
WithOptions([]string{string(info.CloneProtoHTTP), string(info.CloneProtoSSH)}).
WithDefaultText("Git Clone Protocol").
Show(); err == nil {
if proto == "ssh" {
gitRemote.CloneProto = info.CloneProtoSSH
} else {
gitRemote.CloneProto = info.CloneProtoHTTP
}
}
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() {
configGenerateCmd.PersistentFlags().Bool(util.FlagPrompt, false, "Prompt for settings")
configGenerateCmd.PersistentFlags().Bool(util.FlagWrite, false, "Write config to file")
}

40
cmd/config/config_show.go Normal file
View File

@ -0,0 +1,40 @@
package cmd
import (
"os"
"strings"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
)
var configShowCmd = &cobra.Command{
Use: "show",
Short: "Show Git Project Manager Configuration",
Aliases: []string{"s", "dump", "cat", "ls"},
Run: runConfigShowCmd,
}
func runConfigShowCmd(cmd *cobra.Command, args []string) {
c := *utils.Config()
showSensitive, _ := cmd.Flags().GetBool(util.FlagSensitive)
if !showSensitive {
utils.Logger().Info("Sensitive fields hidden, do not use unreviewed as config")
for _, r := range c.Remotes {
r.Token = strings.Repeat("*", len(r.Token))
}
} else {
utils.Logger().Warn("Displaying sensitive fields!")
}
enc := yaml.NewEncoder(os.Stdout)
enc.SetIndent(2)
enc.Encode(c)
}
func init() {
configShowCmd.Flags().BoolP(util.FlagSensitive, "s", false, "Set to show sensitive fields")
}