wip
This commit is contained in:
29
cmd/clear.go
Normal file
29
cmd/clear.go
Normal file
@ -0,0 +1,29 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
const longDesc = `Used to reset a project cache, forcing it to be rebuilt.
|
||||
|
||||
If --clearAliases is provided, will also reset aliases. Use with caution.`
|
||||
|
||||
var clearCmd = &cobra.Command{
|
||||
Use: "clear",
|
||||
Short: "Clear GitLab Project Cache",
|
||||
Long: longDesc,
|
||||
Run: clearCache,
|
||||
}
|
||||
|
||||
func clearCache(cmd *cobra.Command, args []string) {
|
||||
slog.Debug("Preparing to clear local cache")
|
||||
cache.Clear(conf.Cache.Clear.ClearAliases)
|
||||
}
|
||||
|
||||
func init() {
|
||||
cacheCmd.AddCommand(clearCmd)
|
||||
clearCmd.Flags().Bool("clearAliases", false, "Will also clear aliases from the cache, use with caution")
|
||||
viper.BindPFlag("cache.clear.clearAliases", clearCmd.LocalFlags().Lookup("clearAliases"))
|
||||
}
|
43
cmd/dump.go
43
cmd/dump.go
@ -1,24 +1,3 @@
|
||||
/*
|
||||
Copyright © 2023 Ryan McGuire <ryand_mcguire@sweetwater.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
@ -27,31 +6,15 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// dumpCmd represents the dump command
|
||||
var dumpCmd = &cobra.Command{
|
||||
Use: "dump",
|
||||
Short: "A brief description of your command",
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Short: "Dump GitLab project cache",
|
||||
Long: `Dumps cache to display`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("dump called")
|
||||
fmt.Println(cache.String())
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
cacheCmd.AddCommand(dumpCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// dumpCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// dumpCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
32
cmd/init.go
32
cmd/init.go
@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"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/exp/slog"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
@ -18,22 +18,28 @@ import (
|
||||
// func from their PersistentPreRun commands
|
||||
|
||||
func initProjectCache(cmd *cobra.Command, args []string) {
|
||||
slog.Debug("Running persistent pre-run for cacheCmd")
|
||||
plog.Debug("Running persistent pre-run for cacheCmd")
|
||||
conf.Cache.File = conf.ProjectPath + "/.cache.json"
|
||||
|
||||
var err error
|
||||
gitlabClient, err := gitlab.NewGitlabClient(cmd.Context(), conf.GitlabHost, conf.GitlabToken)
|
||||
if err != nil {
|
||||
plog.Error("Failed to create GitLab client", plog.Args("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cacheOpts := &projects.CacheOpts{
|
||||
Path: conf.Cache.File,
|
||||
TTL: conf.Cache.Ttl,
|
||||
Logger: slog.Default(),
|
||||
Logger: plog,
|
||||
Gitlab: gitlabClient,
|
||||
}
|
||||
if cache, err = projects.NewProjectCache(cacheOpts); err != nil {
|
||||
slog.Error("Failed to prepare project cache", "error", err)
|
||||
plog.Error("Failed to prepare project cache", plog.Args("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := cache.Load(); err != nil {
|
||||
slog.Error("Cache load failed", "error", err)
|
||||
plog.Error("Cache load failed", plog.Args("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@ -43,25 +49,25 @@ func postProjectCache(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
func initProjectPath(cmd *cobra.Command, args []string) {
|
||||
slog.Debug("Running persistent pre-run for rootCmd")
|
||||
plog.Debug("Running persistent pre-run for rootCmd")
|
||||
var err error
|
||||
if conf.ProjectPath, err = resolvePath(conf.ProjectPath); err != nil {
|
||||
slog.Error("Failed to determine project path", "error", err)
|
||||
plog.Error("Failed to determine project path", plog.Args("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = os.Stat(conf.ProjectPath)
|
||||
if err != nil {
|
||||
slog.Error("Failed to stat project path, trying to create", "error", err)
|
||||
plog.Error("Failed to stat project path, trying to create", plog.Args("error", err))
|
||||
if err := os.Mkdir(conf.ProjectPath, 0750); err != nil {
|
||||
slog.Error("Failed to create project path", "error", err)
|
||||
plog.Error("Failed to create project path", plog.Args("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
slog.Info("Project path created", "path", conf.ProjectPath)
|
||||
plog.Info("Project path created", plog.Args("path", conf.ProjectPath))
|
||||
} else {
|
||||
if err = unix.Access(conf.ProjectPath, unix.W_OK); err != nil {
|
||||
slog.Error("Unable to write to project path",
|
||||
plog.Error("Unable to write to project path", plog.Args(
|
||||
"path", conf.ProjectPath,
|
||||
"error", err)
|
||||
"error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
20
cmd/load.go
20
cmd/load.go
@ -1,32 +1,22 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// loadCmd represents the load command
|
||||
var loadCmd = &cobra.Command{
|
||||
Use: "load",
|
||||
Short: "Load GitLab Project Cache",
|
||||
Long: `Used to initialize or update a new GitLab cache. With thousands
|
||||
of projects, it would be too much work to hit the API every time a user
|
||||
wants to find a new project.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("load called")
|
||||
},
|
||||
Run: loadCache,
|
||||
}
|
||||
|
||||
func loadCache(cmd *cobra.Command, args []string) {
|
||||
cache.Refresh()
|
||||
}
|
||||
|
||||
func init() {
|
||||
cacheCmd.AddCommand(loadCmd)
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// loadCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// loadCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
40
cmd/root.go
40
cmd/root.go
@ -6,10 +6,10 @@ import (
|
||||
"os/signal"
|
||||
"strings"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"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 (
|
||||
@ -19,6 +19,7 @@ const (
|
||||
)
|
||||
|
||||
var conf config.Config
|
||||
var plog *pterm.Logger
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "gitlab-project-manager",
|
||||
@ -44,7 +45,7 @@ func Execute() {
|
||||
err := rootCmd.ExecuteContext(ctx)
|
||||
|
||||
if err != nil {
|
||||
slog.Error("Failed to execute command", "err", err)
|
||||
plog.Error("Failed to execute command", plog.Args("err", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@ -89,47 +90,48 @@ func initConfig() {
|
||||
|
||||
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)
|
||||
// Configure pretty logger
|
||||
plog = pterm.DefaultLogger.WithLevel(getPtermLogLevel(viper.GetString("logLevel")))
|
||||
if plog.Level == pterm.LogLevelDebug {
|
||||
pterm.EnableDebugMessages()
|
||||
}
|
||||
|
||||
// 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)
|
||||
plog.Error("Failed loading config", plog.Args("err", err))
|
||||
}
|
||||
|
||||
slog.Debug("Configuration loaded", "conf", conf)
|
||||
plog.Debug("Configuration loaded", plog.Args("conf", conf))
|
||||
}
|
||||
|
||||
func getSlogLevel(level string) slog.Level {
|
||||
var slogLevel slog.Level
|
||||
func getPtermLogLevel(level string) pterm.LogLevel {
|
||||
var pLevel pterm.LogLevel
|
||||
switch strings.ToLower(level) {
|
||||
case "error":
|
||||
slogLevel = slog.LevelError
|
||||
pLevel = pterm.LogLevelError
|
||||
case "warn":
|
||||
slogLevel = slog.LevelWarn
|
||||
pLevel = pterm.LogLevelWarn
|
||||
case "info":
|
||||
slogLevel = slog.LevelInfo
|
||||
pLevel = pterm.LogLevelInfo
|
||||
case "debug":
|
||||
slogLevel = slog.LevelDebug
|
||||
pLevel = pterm.LogLevelDebug
|
||||
default:
|
||||
slogLevel = slog.LevelInfo
|
||||
pLevel = pterm.LogLevelInfo
|
||||
}
|
||||
return slogLevel
|
||||
return pLevel
|
||||
}
|
||||
|
||||
// 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)
|
||||
plog.Error("Failure reading configuration", plog.Args("err", err))
|
||||
return
|
||||
}
|
||||
if stat.Mode().Perm()&0004 == 0004 {
|
||||
slog.Error("Configuration is world-readable. Recomment 0400.",
|
||||
"mode", stat.Mode().String())
|
||||
plog.Error("Configuration is world-readable. Recomment 0400.",
|
||||
plog.Args("mode", stat.Mode().String()))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user