git-project-manager/cmd/project_show.go

85 lines
2.2 KiB
Go
Raw Normal View History

2023-12-08 21:52:26 +00:00
package cmd
import (
"fmt"
"os"
2023-12-08 21:52:26 +00:00
"github.com/pterm/pterm"
2023-12-08 21:52:26 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2024-10-01 18:29:14 +00:00
2024-01-15 20:39:35 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
2023-12-08 21:52:26 +00:00
)
var projectShowCmd = &cobra.Command{
2023-12-10 16:15:52 +00:00
Use: "show [fuzzy alias search]",
Short: "Show detail for a GitLab project",
Aliases: []string{"cat", "s"},
Args: cobra.ArbitraryArgs,
Long: projShowCmdLong,
Run: projectShowCmdRun,
2023-12-08 21:52:26 +00:00
}
func projectShowCmdRun(cmd *cobra.Command, args []string) {
2024-01-15 20:39:35 +00:00
var project *projects.Project
var inCwd bool
2023-12-08 21:52:26 +00:00
2024-10-01 18:29:14 +00:00
remotes := viper.GetStringSlice(FlagRemote)
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
Search: searchStringFromArgs(args),
Remotes: remotes,
}
// Try to find project from current directory
2024-10-01 18:29:14 +00:00
if viper.GetBool(ViperProjectShowCurrent) {
var err error
2024-01-15 21:02:15 +00:00
project, err = projectCache.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
2023-12-08 21:52:26 +00:00
if project == nil {
var err error
project, err = fzfProject(fzfOpts)
2023-12-08 21:52:26 +00:00
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"))).
2024-01-15 21:02:15 +00:00
Println(projectCache.ProjectString(project))
fmt.Println()
if inCwd {
2024-01-15 21:02:15 +00:00
project.SetRepo(projectCache.OpenProject(cmd.Context(), project))
fmt.Fprintln(os.Stderr, project.GetGitInfo()+"\n")
}
2023-12-08 21:52:26 +00:00
}
func init() {
projectCmd.AddCommand(projectShowCmd)
2024-10-01 18:29:14 +00:00
projectShowCmd.PersistentFlags().Bool(FlagCurrent, false, "Use project in CWD rather than fuzzy find")
viper.BindPFlag(ViperProjectShowCurrent, projectShowCmd.Flag(FlagCurrent))
2023-12-08 21:52:26 +00:00
}