Improve project show and add pshow func with cwd support

This commit is contained in:
Ryan McGuire 2023-12-11 15:42:50 -05:00
parent d4ad16fe16
commit ea909b87cc
5 changed files with 70 additions and 3 deletions

View File

@ -66,7 +66,7 @@ cache:
- [ ] Update README for shell completion, aliases, usage - [ ] Update README for shell completion, aliases, usage
- [ ] Make a Makefile - [ ] Make a Makefile
- [ ] Add git repo status to project go (up-to-date, pending commits, etc..) - [ ] 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 - [ ] Build pipeline, and link to gitlab registry for a release binary
- [ ] Brew package and GitHub - [ ] Brew package and GitHub
- [x] Merge aliases together for same project when selecting - [x] Merge aliases together for same project when selecting

View File

@ -3,7 +3,10 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/pterm/pterm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
) )
var projectShowCmd = &cobra.Command{ var projectShowCmd = &cobra.Command{
@ -21,8 +24,28 @@ func projectShowCmdRun(cmd *cobra.Command, args []string) {
searchString = args[0] 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 { if project == nil {
var err error var err error
project, err = fzfProject(cmd.Context()) 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() { func init() {
projectCmd.AddCommand(projectShowCmd) projectCmd.AddCommand(projectShowCmd)
projectShowCmd.PersistentFlags().Bool("current", false, "Use project in CWD rather than fuzzy find")
viper.BindPFlag("project.show.current", projectShowCmd.Flag("current"))
} }

View File

@ -20,3 +20,7 @@ padd () {
plist () { plist () {
gitlab-project-manager alias list gitlab-project-manager alias list
} }
pshow () {
gitlab-project-manager project show --current
}

View File

@ -90,6 +90,15 @@ func (c *Cache) GetAliasByName(name string) *ProjectAlias {
return nil 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 { func (c *Cache) GetProjectByID(id int) *gitlab.Project {
for _, p := range c.Projects { for _, p := range c.Projects {
if p.ID == id { if p.ID == id {

View File

@ -1,8 +1,10 @@
package projects package projects
import ( import (
"errors"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab" "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)) 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 { func (c *Cache) IsProjectCloned(p *gitlab.Project) bool {
_, err := os.Stat(c.GetProjectPath(p) + "/.git") _, err := os.Stat(c.GetProjectPath(p) + "/.git")
if err == nil { if err == nil {