git-project-manager/cmd/project/project.go

67 lines
2.0 KiB
Go
Raw Permalink Normal View History

2024-12-30 20:50:31 +00:00
package project
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/alias"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/cache"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
)
var ProjectCmd = &cobra.Command{
Use: "project [fuzzy alias search]",
Short: "Use a Git project",
Aliases: []string{"proj", "projects", "p"},
Args: cobra.MaximumNArgs(1),
ArgAliases: []string{"alias"},
Long: util.ProjCmdLong,
PersistentPreRun: projectCmdPreRun,
PersistentPostRun: util.PostProjectCmd,
// Run: projectGoCmdRun,
}
var utils *util.Utils
func projectCmdPreRun(cmd *cobra.Command, args []string) {
utils = util.MustFromCtx(cmd.Context())
util.InitProjects(cmd, args)
}
func getProject(cmd *cobra.Command, args []string) *projects.Project {
remotes := viper.GetStringSlice(util.FlagRemote)
fzfOpts := &util.FzfProjectOpts{
Ctx: cmd.Context(),
Search: utils.SearchStringFromArgs(args),
Remotes: remotes,
}
project := utils.FzfFindProject(fzfOpts)
if project == nil {
utils.Logger().Fatal("Failed to find a project, nothing to do")
} else {
utils.Logger().Debug("Houston, we have a project", utils.Logger().Args(
"project", project.String(),
"aliases", cache.ProjectAliasesString(
utils.Cache().GetProjectAliases(project)),
))
}
if len(utils.Cache().GetProjectAliases(project)) == 0 {
utils.Logger().Info("New project, set aliases or press enter for default")
alias.AddNewAliases(cmd, project.GetID())
2024-12-30 20:50:31 +00:00
}
return project
}
func init() {
ProjectCmd.AddCommand(projectAddCmd)
ProjectCmd.AddCommand(projectGoCmd)
ProjectCmd.AddCommand(projectListCmd)
ProjectCmd.AddCommand(projectOpenCmd)
ProjectCmd.AddCommand(projectRunCmd)
ProjectCmd.AddCommand(projectShowCmd)
}