Begin caching, implement commands
This commit is contained in:
67
cmd/cache.go
67
cmd/cache.go
@ -1,57 +1,40 @@
|
||||
/*
|
||||
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 (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
|
||||
)
|
||||
|
||||
// cacheCmd represents the cache command
|
||||
var cacheCmd = &cobra.Command{
|
||||
Use: "cache",
|
||||
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:
|
||||
const desc = `Contains sub-commands for managing project cache.
|
||||
The project cache keeps this speedy, without smashing against the GitLab
|
||||
API every time a new project is added / searched for`
|
||||
|
||||
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.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("cache called")
|
||||
},
|
||||
var cache *projects.Cache
|
||||
|
||||
var cacheCmd = &cobra.Command{
|
||||
Use: "cache",
|
||||
Short: "Manage GitLab project cache",
|
||||
Long: desc,
|
||||
PersistentPreRun: initCacheCmd,
|
||||
PersistentPostRun: postCacheCmd,
|
||||
}
|
||||
|
||||
func initCacheCmd(cmd *cobra.Command, args []string) {
|
||||
initProjectCache(cmd, args)
|
||||
}
|
||||
|
||||
func postCacheCmd(cmd *cobra.Command, args []string) {
|
||||
postProjectCache(cmd, args)
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(cacheCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
cacheCmd.PersistentFlags().Duration("ttl", 48*time.Hour,
|
||||
"Duration before cache is re-built in go time.Duration format")
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// cacheCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// cacheCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
viper.BindPFlags(cacheCmd.Flags())
|
||||
}
|
||||
|
@ -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 (
|
||||
@ -44,14 +23,4 @@ to quickly create a Cobra application.`,
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(configCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// configCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
76
cmd/init.go
Normal file
76
cmd/init.go
Normal file
@ -0,0 +1,76 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
|
||||
"golang.org/x/exp/slog"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// This file contains init methods that may be used by
|
||||
// multiple sub-commands. For instance, the cach and projects
|
||||
// sub-commands both depend on a cache and may both call the initProjectCache
|
||||
// func from their PersistentPreRun commands
|
||||
|
||||
func initProjectCache(cmd *cobra.Command, args []string) {
|
||||
slog.Debug("Running persistent pre-run for cacheCmd")
|
||||
conf.Cache.File = conf.ProjectPath + "/.cache.json"
|
||||
|
||||
var err error
|
||||
cacheOpts := &projects.CacheOpts{
|
||||
Path: conf.Cache.File,
|
||||
TTL: conf.Cache.Ttl,
|
||||
Logger: slog.Default(),
|
||||
}
|
||||
if cache, err = projects.NewProjectCache(cacheOpts); err != nil {
|
||||
slog.Error("Failed to prepare project cache", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := cache.Load(); err != nil {
|
||||
slog.Error("Cache load failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func postProjectCache(cmd *cobra.Command, args []string) {
|
||||
cache.Write()
|
||||
}
|
||||
|
||||
func initProjectPath(cmd *cobra.Command, args []string) {
|
||||
slog.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)
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = os.Stat(conf.ProjectPath)
|
||||
if err != nil {
|
||||
slog.Error("Failed to stat project path, trying to create", "error", err)
|
||||
if err := os.Mkdir(conf.ProjectPath, 0750); err != nil {
|
||||
slog.Error("Failed to create project path", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
slog.Info("Project path created", "path", conf.ProjectPath)
|
||||
} else {
|
||||
if err = unix.Access(conf.ProjectPath, unix.W_OK); err != nil {
|
||||
slog.Error("Unable to write to project path",
|
||||
"path", conf.ProjectPath,
|
||||
"error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resolvePath(path string) (string, error) {
|
||||
if strings.HasPrefix(path, "~/") {
|
||||
usr, _ := user.Current()
|
||||
path = filepath.Join(usr.HomeDir, path[2:])
|
||||
}
|
||||
return filepath.Abs(path)
|
||||
}
|
33
cmd/load.go
33
cmd/load.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 (
|
||||
@ -30,13 +9,10 @@ import (
|
||||
// loadCmd represents the load command
|
||||
var loadCmd = &cobra.Command{
|
||||
Use: "load",
|
||||
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: "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")
|
||||
},
|
||||
@ -44,7 +20,6 @@ to quickly create a Cobra application.`,
|
||||
|
||||
func init() {
|
||||
cacheCmd.AddCommand(loadCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
|
96
cmd/root.go
96
cmd/root.go
@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user