Update config, add manual cache unlocking

This commit is contained in:
2023-12-29 10:24:12 -05:00
parent ea0157a997
commit 156ddc2009
13 changed files with 141 additions and 50 deletions

View File

@@ -11,12 +11,10 @@ import (
var cache *projects.Cache
var cacheCmd = &cobra.Command{
Use: "cache",
Aliases: []string{"a", "ln"},
Short: "Manage GitLab project cache",
Long: cacheCmdLong,
PersistentPreRun: runCacheCmd,
PersistentPostRun: postCacheCmd,
Use: "cache",
Aliases: []string{"a", "ln"},
Short: "Manage GitLab project cache",
Long: cacheCmdLong,
}
func runCacheCmd(cmd *cobra.Command, args []string) {

View File

@@ -11,10 +11,12 @@ 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,
Use: "clear",
Short: "Clear GitLab Project Cache",
PreRun: runCacheCmd,
PostRun: postCacheCmd,
Long: longDesc,
Run: clearCache,
}
func clearCache(cmd *cobra.Command, args []string) {

View File

@@ -8,9 +8,11 @@ import (
)
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Dump GitLab project cache",
Long: `Dumps cache to display`,
Use: "dump",
Short: "Dump GitLab project cache",
Long: `Dumps cache to display`,
PreRun: runCacheCmd,
PostRun: postCacheCmd,
Run: func(cmd *cobra.Command, args []string) {
if conf.Dump.Full {
fmt.Println(cache.DumpString(true))

View File

@@ -11,7 +11,9 @@ var loadCmd = &cobra.Command{
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: loadCache,
PreRun: runCacheCmd,
PostRun: postCacheCmd,
Run: loadCache,
}
func loadCache(cmd *cobra.Command, args []string) {

31
cmd/cache_unlock.go 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 GitLab project cache",
Long: `unlocks cache to display`,
Run: func(cmd *cobra.Command, args []string) {
initProjectCache(cmd, args)
if viper.GetBool("cache.unlock.force") {
cache.UnlockCache()
} else if yes, _ := pterm.DefaultInteractiveConfirm.
WithDefaultValue(false).
Show("Are you sure you want to manually unlock?"); yes {
cache.UnlockCache()
} else {
plog.Error("You failed to confirm cache unlock")
}
},
}
func init() {
cacheCmd.AddCommand(unlockCmd)
unlockCmd.PersistentFlags().BoolP("force", "f", false, "force unlocks cache (don't ask)")
viper.BindPFlag("cache.unlock.force", unlockCmd.LocalFlags().Lookup("force"))
}

View File

@@ -19,12 +19,7 @@ var projectCmd = &cobra.Command{
}
func getProject(args []string) *gitlab.Project {
var searchString string
if len(args) > 0 {
searchString = args[0]
}
project := fzfFindProject(searchString)
project := fzfFindProject(searchStringFromArgs(args))
if project == nil {
plog.Fatal("Failed to find a project, nothing to do")

View File

@@ -20,12 +20,7 @@ var projectGoCmd = &cobra.Command{
}
func projectGoCmdRun(cmd *cobra.Command, args []string) {
var term string
if len(args) > 0 {
term = args[0]
}
project := fzfSearchProjectAliases(term)
project := fzfSearchProjectAliases(searchStringFromArgs(args))
if project == nil {
plog.Fatal("No project selected, nowhere to go")

View File

@@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
)
var projectOpenCmd = &cobra.Command{
@@ -53,23 +52,9 @@ func projectOpenCmdRun(cmd *cobra.Command, args []string) {
}
// Identify search terms
var searchTerm string
if len(args) > 0 {
searchTerm = args[0]
}
// Try to open local project
var project *gitlab.Project
if searchTerm == "." {
project, _ = cache.GetProjectFromCwd()
}
// Find a project if not local
project := fzfCwdOrSearchProjectAliases(searchStringFromArgs(args))
if project == nil {
project = fzfSearchProjectAliases(searchTerm)
if project == nil {
plog.Fatal("No project to open, nothing to do")
}
plog.Fatal("No project to open, nothing to do")
}
// Check the project

30
cmd/project_run.go Normal file
View File

@@ -0,0 +1,30 @@
package cmd
import (
"github.com/spf13/cobra"
)
var projectRunCmd = &cobra.Command{
Use: "run",
Short: "Run the project (e.g. go run .)",
Aliases: []string{"exec", "r"},
Long: projRunCmdLong,
Run: projectRunCmdRun,
}
func projectRunCmdRun(cmd *cobra.Command, args []string) {
project := fzfCwdOrSearchProjectAliases(searchStringFromArgs(args))
if project == nil {
plog.Fatal("No project selected, nothing to open")
}
plog.Info("Running Projet", plog.Args("lang", project.Language))
if project.Language == nil {
plog.Fatal("GitLab isn't sure what language this project is... can't run.")
}
}
func init() {
projectCmd.AddCommand(projectRunCmd)
}

View File

@@ -35,6 +35,9 @@ will be cloned from source control.
If conf.projects.alwaysPull, a git pull will be ran automatically`
const projRunCmdLong = `Runs the current project. Tries to detect
the language and runs accordingly (e.g. go run .)`
const projListCmdLong = `List locally cloned projects. Optionally
lists all projects in project cache`

View File

@@ -26,6 +26,19 @@ func fzfFindProject(searchString string) *gitlab.Project {
return project
}
// If . is given as a project, will open project from the
// current working directory. Otherwise, will attempt to fuzzy-find
// a project given a search term if provided
func fzfCwdOrSearchProjectAliases(searchString string) *gitlab.Project {
var project *gitlab.Project
if searchString == "." {
project, _ = cache.GetProjectFromCwd()
} else {
project = fzfSearchProjectAliases(searchString)
}
return project
}
// This will fuzzy search only aliases, preferring an exact
// match if one is given
func fzfSearchProjectAliases(searchString string) *gitlab.Project {
@@ -138,3 +151,13 @@ func fzfPreviewWindow(i, w, h int) string {
p := cache.Projects[i]
return cache.ProjectString(p)
}
// Nearly useless function that simply returns either an
// empty string, or a string from the first arg if one is provided
func searchStringFromArgs(args []string) string {
var term string
if len(args) > 0 {
term = args[0]
}
return term
}