diff --git a/README.md b/README.md index 0566c14..8a90dec 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ cache: - [ ] Update README for shell completion, aliases, usage - [ ] Make a Makefile - [ ] Add git repo status to project go (up-to-date, pending commits, etc..) -- [ ] Update `gpm project show` with pterm box like `gpm project list` +- [x] Update `gpm project show` with pterm box like `gpm project list` - [ ] Build pipeline, and link to gitlab registry for a release binary - [ ] Brew package and GitHub - [x] Merge aliases together for same project when selecting diff --git a/cmd/project_show.go b/cmd/project_show.go index a434239..43a8c71 100644 --- a/cmd/project_show.go +++ b/cmd/project_show.go @@ -3,7 +3,10 @@ package cmd import ( "fmt" + "github.com/pterm/pterm" "github.com/spf13/cobra" + "github.com/spf13/viper" + "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab" ) var projectShowCmd = &cobra.Command{ @@ -21,8 +24,28 @@ func projectShowCmdRun(cmd *cobra.Command, args []string) { searchString = args[0] } - project := fzfFindProject(searchString) + var project *gitlab.Project + // Try to find project from current directory + if viper.GetBool("project.show.current") { + var err error + project, err = cache.GetProjectFromCwd() + if err != nil { + // Not an error because we're still going to try to find a project + plog.Warn("Failed to get project from current directory", plog.Args( + "error", err, + )) + } else if project == nil { + plog.Warn("Failed to use --current flag, project not found in current path") + } + } + + // Otherwise find from the given search string + if project == nil { + project = fzfFindProject(searchString) + } + + // Do a full fuzzy find if all else fails if project == nil { var err error project, err = fzfProject(cmd.Context()) @@ -33,9 +56,17 @@ func projectShowCmdRun(cmd *cobra.Command, args []string) { } } - fmt.Println(cache.ProjectString(project)) + fmt.Println() + pterm.DefaultBox. + WithLeftPadding(5).WithRightPadding(5). + WithBoxStyle(&pterm.Style{pterm.FgLightBlue}). + WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Project Information"))). + Println(cache.ProjectString(project)) + fmt.Println() } func init() { projectCmd.AddCommand(projectShowCmd) + projectShowCmd.PersistentFlags().Bool("current", false, "Use project in CWD rather than fuzzy find") + viper.BindPFlag("project.show.current", projectShowCmd.Flag("current")) } diff --git a/contrib/gpm_func_omz.zsh b/contrib/gpm_func_omz.zsh index 4920f6a..bdcc18b 100644 --- a/contrib/gpm_func_omz.zsh +++ b/contrib/gpm_func_omz.zsh @@ -20,3 +20,7 @@ padd () { plist () { gitlab-project-manager alias list } + +pshow () { + gitlab-project-manager project show --current +} diff --git a/internal/projects/projects.go b/internal/projects/projects.go index 18781e6..440c67a 100644 --- a/internal/projects/projects.go +++ b/internal/projects/projects.go @@ -90,6 +90,15 @@ func (c *Cache) GetAliasByName(name string) *ProjectAlias { return nil } +func (c *Cache) GetProjectByPath(path string) *gitlab.Project { + for _, p := range c.Projects { + if p.PathWithNamespace == path { + return p + } + } + return nil +} + func (c *Cache) GetProjectByID(id int) *gitlab.Project { for _, p := range c.Projects { if p.ID == id { diff --git a/internal/projects/projects_fs.go b/internal/projects/projects_fs.go index d0832cc..4e597a9 100644 --- a/internal/projects/projects_fs.go +++ b/internal/projects/projects_fs.go @@ -1,8 +1,10 @@ package projects import ( + "errors" "os" "path/filepath" + "strings" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab" ) @@ -23,6 +25,27 @@ func (c *Cache) GoTo(project *gitlab.Project) { os.Chdir(filepath.Dir(pPath)) } +func (c *Cache) GetProjectFromCwd() (*gitlab.Project, error) { + var project *gitlab.Project + + cwd, err := os.Getwd() + if err != nil { + return project, err + } else if !strings.HasPrefix(cwd, c.path) { + return project, errors.New("Not in any project path") + } + + // Strip projects dir from path + pathWithNs := cwd[len(c.path)+1:] + c.log.Debug("Fetching project from current path", c.log.Args( + "cwd", cwd, "pathWithNamespace", pathWithNs, + )) + + project = c.GetProjectByPath(pathWithNs) + + return project, nil +} + func (c *Cache) IsProjectCloned(p *gitlab.Project) bool { _, err := os.Stat(c.GetProjectPath(p) + "/.git") if err == nil {