git-project-manager/cmd/project_show.go
Ryan McGuire b944af140a Add multi-remote support for GitLab (#1)
Co-authored-by: Ryan D McGuire <ryand_mcguire@sweetwater.com>
Reviewed-on: 50W/git-project-manager#1
2024-01-14 15:33:15 +00:00

84 lines
2.1 KiB
Go

package cmd
import (
"fmt"
"os"
"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{
Use: "show [fuzzy alias search]",
Short: "Show detail for a GitLab project",
Aliases: []string{"cat", "s"},
Args: cobra.ArbitraryArgs,
Long: projShowCmdLong,
Run: projectShowCmdRun,
}
func projectShowCmdRun(cmd *cobra.Command, args []string) {
var project *gitlab.Project
var inCwd bool
gitlabs := viper.GetStringSlice("project.gitlabs")
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
Search: searchStringFromArgs(args),
Gitlabs: gitlabs,
}
// 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")
} else {
inCwd = true
}
}
// Otherwise find from the given search string
if project == nil {
project = fzfFindProject(fzfOpts)
}
// Do a full fuzzy find if all else fails
if project == nil {
var err error
project, err = fzfProject(fzfOpts)
if err != nil || project == nil {
plog.Fatal("Failed to find project, nothing to show", plog.Args(
"error", err,
))
}
}
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()
if inCwd {
project.SetRepo(cache.OpenProject(cmd.Context(), project))
fmt.Fprintln(os.Stderr, project.GetGitInfo()+"\n")
}
}
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"))
}