Begin caching, implement commands

This commit is contained in:
2023-12-05 16:56:47 -05:00
parent 47300dbf89
commit a8aa8af3d3
14 changed files with 432 additions and 198 deletions

View File

@@ -1,43 +1,69 @@
package cmd
import (
"context"
"os"
"os/signal"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
"golang.org/x/exp/slog"
)
const (
defGitlabHost = "gitlab.sweetwater.com"
defGitlabHost = "gitlab.sweetwater.com"
defProjectsPath = "~/work/projects"
defLogLevel = "info"
)
// rootCmd represents the base command when called without any subcommands
var conf config.Config
var rootCmd = &cobra.Command{
Use: "gitlab-project-manager",
Short: "Find and use GitLab projects locally",
Long: `Finds GitLab projects using fuzzy-find, remembering
your chosen term for the project as an alias, and offers helpful
shortcuts for moving around in projects and opening your code`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
PersistentPreRun: initRootCmd,
}
// Hook traversal is enabled, so this will be run for all
// sub-commands regardless of their registered pre-hooks
func initRootCmd(cmd *cobra.Command, args []string) {
initProjectPath(cmd, args)
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
ctx, cncl := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
defer cncl()
err := rootCmd.ExecuteContext(ctx)
if err != nil {
slog.Error("Failed to execute command", "err", err)
os.Exit(1)
}
}
func init() {
cobra.EnableTraverseRunHooks = true
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.gitlab-project-manager.yaml)")
rootCmd.PersistentFlags().String("gitlabHost", defGitlabHost, "GitLab Hostname (e.g. gitlab.com)")
rootCmd.PersistentFlags().String("gitlabToken", "", "GitLab Tokenname (e.g. gitlab.com)")
rootCmd.PersistentFlags().String("config", "",
"config file (default is $HOME/.gitlab-project-manager.yaml)")
rootCmd.PersistentFlags().String("gitlabHost", defGitlabHost,
"GitLab Hostname (e.g. gitlab.com)")
rootCmd.PersistentFlags().String("gitlabToken", "",
"GitLab Tokenname (e.g. gitlab.com)")
rootCmd.PersistentFlags().String("projectPath", defProjectsPath,
"Sets a path for local clones of projects")
rootCmd.PersistentFlags().String("logLevel", defLogLevel,
"Default log level -- info, warn, error, debug")
viper.BindPFlags(rootCmd.PersistentFlags())
}
@@ -58,12 +84,52 @@ func initConfig() {
viper.SetConfigName(".gitlab-project-manager")
}
viper.AutomaticEnv() // read in environment variables that match
viper.AutomaticEnv()
viper.ReadInConfig()
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
slog.Info("Using config file:", viper.ConfigFileUsed())
} else {
slog.Error("Error reading viper config", err)
checkConfigPerms(viper.ConfigFileUsed()) // Abort on world-readable config
// Configure default logger
logger := slog.New(slog.NewTextHandler(os.Stdout,
&slog.HandlerOptions{Level: getSlogLevel(viper.GetString("logLevel"))}))
slog.SetDefault(logger)
// Load into struct to not be so darn pythonic, retrieving
// settings by untyped string "name"
if err := viper.Unmarshal(&conf); err != nil {
slog.Error("Failed loading config", "err", err)
}
slog.Debug("Configuration loaded", "conf", conf)
}
func getSlogLevel(level string) slog.Level {
var slogLevel slog.Level
switch strings.ToLower(level) {
case "error":
slogLevel = slog.LevelError
case "warn":
slogLevel = slog.LevelWarn
case "info":
slogLevel = slog.LevelInfo
case "debug":
slogLevel = slog.LevelDebug
default:
slogLevel = slog.LevelInfo
}
return slogLevel
}
// Don't allow world-readable configuration
func checkConfigPerms(file string) {
stat, err := os.Stat(file)
if err != nil {
slog.Error("Failure reading configuration", "err", err)
return
}
if stat.Mode().Perm()&0004 == 0004 {
slog.Error("Configuration is world-readable. Recomment 0400.",
"mode", stat.Mode().String())
os.Exit(1)
}
}