Package subcommand code

This commit is contained in:
2024-12-30 14:54:32 -05:00
parent 888ee5ea4a
commit 96378d047e
19 changed files with 564 additions and 412 deletions

34
cmd/cache/cache.go vendored Normal file
View File

@ -0,0 +1,34 @@
package cmd
import (
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cacheCmd = &cobra.Command{
Use: "cache",
Aliases: []string{"a", "ln"},
Short: "Manage Git project cache",
Long: cacheCmdLong,
}
func runCacheCmd(cmd *cobra.Command, args []string) {
initProjectCache(cmd, args)
projectCache.LockCache()
}
func postCacheCmd(cmd *cobra.Command, args []string) {
postProjectCache(cmd, args)
projectCache.UnlockCache()
}
func init() {
rootCmd.AddCommand(cacheCmd)
cacheCmd.PersistentFlags().Duration("ttl", 48*time.Hour,
"Duration before cache is re-built in go time.Duration format")
viper.BindPFlags(cacheCmd.Flags())
}

31
cmd/cache/cache_clear.go vendored Normal file
View File

@ -0,0 +1,31 @@
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 Git Project Cache",
PreRun: runCacheCmd,
PostRun: postCacheCmd,
Long: longDesc,
Run: clearCache,
}
func clearCache(cmd *cobra.Command, args []string) {
slog.Debug("Preparing to clear local cache")
projectCache.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"))
}

32
cmd/cache/cache_dump.go vendored Normal file
View File

@ -0,0 +1,32 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Dump Git project cache",
Long: `Dumps cache to display`,
PreRun: runCacheCmd,
PostRun: postCacheCmd,
Run: runCacheDunpCmd,
}
func runCacheDunpCmd(cmd *cobra.Command, args []string) {
remotes := viper.GetStringSlice(FlagRemote)
if conf.Dump.Full {
fmt.Println(projectCache.DumpString(true, searchStringFromArgs(args), remotes...))
} else {
plog.Info(projectCache.String())
}
}
func init() {
cacheCmd.AddCommand(dumpCmd)
dumpCmd.PersistentFlags().BoolP("full", "f", false, "Dumps entire cache")
viper.BindPFlag("dump.full", dumpCmd.LocalFlags().Lookup("full"))
}

31
cmd/cache/cache_load.go vendored Normal file
View File

@ -0,0 +1,31 @@
package cmd
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var loadCmd = &cobra.Command{
Use: "load",
Short: "Load Git Project Cache",
Long: `Used to initialize or update a new Git 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.`,
PreRun: runCacheCmd,
PostRun: postCacheCmd,
Run: loadCache,
}
func loadCache(cmd *cobra.Command, args []string) {
remotes := viper.GetStringSlice(FlagRemote)
projectCache.Refresh(remotes...)
}
func init() {
cacheCmd.AddCommand(loadCmd)
loadCmd.PersistentFlags().Bool(FlagOwnerOnly, true,
"Only load projects that you are owner of")
viper.BindPFlag(ViperCacheLoadOwnerOnly, loadCmd.Flag(FlagOwnerOnly))
}

31
cmd/cache/cache_unlock.go vendored Normal file
View File

@ -0,0 +1,31 @@
package cmd
import (
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var unlockCmd = &cobra.Command{
Use: "unlock",
Short: "unlock Git project cache",
Long: `unlocks cache to display`,
Run: func(cmd *cobra.Command, args []string) {
initProjectCache(cmd, args)
if viper.GetBool(ViperCacheUnlockForce) {
projectCache.UnlockCache()
} else if yes, _ := pterm.DefaultInteractiveConfirm.
WithDefaultValue(false).
Show("Are you sure you want to manually unlock?"); yes {
projectCache.UnlockCache()
} else {
plog.Error("You failed to confirm cache unlock")
}
},
}
func init() {
cacheCmd.AddCommand(unlockCmd)
unlockCmd.PersistentFlags().BoolP(FlagCacheForce, "f", false, "force unlocks cache (don't ask)")
viper.BindPFlag(ViperCacheUnlockForce, unlockCmd.LocalFlags().Lookup(FlagCacheForce))
}