Add config generator

This commit is contained in:
Ryan McGuire 2023-12-11 10:39:48 -05:00
parent cf5675d04f
commit dfb2699417
3 changed files with 105 additions and 3 deletions

View File

@ -34,6 +34,23 @@ The basic workflow looks like this:
1. **List** -- get a list of your configured projects any time with `plist`
1. **Reward** -- buy the author a beer, because this thing is a time saver
## Installing
1. Install into your go path by running `go install .`
1. Copy `contrib/gpm_func_omz.zsh` into your `~/.oh-my-zsh/custom/` path, or just source from your bashrc/zshrc
1. Generate config file: `gpm config gen --write`
1. You can run this any time to update settings
1. Run `gpm cache load` (if aliases is in-place, otherwise `gitlab-project-manager cache load`)
### Config Sample
```yaml
gitlabHost: https://gitlab.sweetwater.com
gitlabToken: <your token here>
logLevel: info
cache:
ttl: 168h
ownerOnly: false
```
## TODO
- [ ] Fix NPE when cache is reset or project for whatever reason leaves an orphaned alias
@ -49,6 +66,8 @@ The basic workflow looks like this:
- [ ] Make a Makefile
- [ ] Add git repo status to project go (up-to-date, pending commits, etc..)
- [ ] Update `gpm project show` with pterm box like `gpm project list`
- [ ] Build pipeline, and link to gitlab registry for a release binary
- [ ] Brew package and GitHub
- [x] Merge aliases together for same project when selecting
- [x] If after merging there is only one project, go there by default

View File

@ -2,8 +2,11 @@ package cmd
import (
"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"
)
@ -17,11 +20,87 @@ var configGenerateCmd = &cobra.Command{
}
func runConfigGenerateCmd(cmd *cobra.Command, args []string) {
plog.Info("Suggest running with > " + defConfigPath)
c, _ := yaml.Marshal(config.DefaultConfig)
fmt.Print(string(c))
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 {
c, _ := yaml.Marshal(newConf)
plog.Info("Suggest running with --write or redirect (> " + configFile + ")")
fmt.Print(string(c))
}
}
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 {
if host, err := pterm.DefaultInteractiveTextInput.
WithDefaultValue(c.GitlabHost).
WithDefaultText("Enter gitlab URL").
Show(); err == nil {
c.GitlabHost = host
}
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 token, err := pterm.DefaultInteractiveTextInput.
WithMask("*").
WithDefaultValue(c.GitlabToken).
WithDefaultText("Enter gitlab Token").
Show(); err == nil {
c.GitlabToken = token
}
return c
}
func init() {
configCmd.AddCommand(configGenerateCmd)
configGenerateCmd.PersistentFlags().Bool("prompt", false, "Prompt for settings")
configGenerateCmd.PersistentFlags().Bool("write", false, "Write config to file")
}

View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/spf13/cobra"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
"golang.org/x/sys/unix"
@ -53,6 +54,9 @@ func postProjectCache(cmd *cobra.Command, args []string) {
func initProjectPath(cmd *cobra.Command, args []string) {
plog.Debug("Running persistent pre-run for rootCmd")
var err error
if conf.ProjectPath == "" {
conf.ProjectPath = config.DefaultConfig.ProjectPath
}
if conf.ProjectPath, err = resolvePath(conf.ProjectPath); err != nil {
plog.Error("Failed to determine project path", plog.Args("error", err))
os.Exit(1)