Improve project show and add pshow func with cwd support
This commit is contained in:
parent
d4ad16fe16
commit
ea909b87cc
@ -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
|
||||
|
@ -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"))
|
||||
}
|
||||
|
@ -20,3 +20,7 @@ padd () {
|
||||
plist () {
|
||||
gitlab-project-manager alias list
|
||||
}
|
||||
|
||||
pshow () {
|
||||
gitlab-project-manager project show --current
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user