2023-12-08 21:52:26 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-11 20:54:22 +00:00
|
|
|
"os"
|
2023-12-08 21:52:26 +00:00
|
|
|
|
2023-12-11 20:42:50 +00:00
|
|
|
"github.com/pterm/pterm"
|
2023-12-08 21:52:26 +00:00
|
|
|
"github.com/spf13/cobra"
|
2023-12-11 20:42:50 +00:00
|
|
|
"github.com/spf13/viper"
|
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
|
2023-12-11 20:54:22 +00:00
|
|
|
var inCwd bool
|
2023-12-08 21:52:26 +00:00
|
|
|
|
2024-01-17 15:06:20 +00:00
|
|
|
remotes := viper.GetStringSlice("remote")
|
2024-01-14 15:33:15 +00:00
|
|
|
fzfOpts := &fzfProjectOpts{
|
|
|
|
Ctx: cmd.Context(),
|
|
|
|
Search: searchStringFromArgs(args),
|
2024-01-17 15:06:20 +00:00
|
|
|
Remotes: remotes,
|
2024-01-14 15:33:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 20:42:50 +00:00
|
|
|
// Try to find project from current directory
|
|
|
|
if viper.GetBool("project.show.current") {
|
|
|
|
var err error
|
2024-01-15 21:02:15 +00:00
|
|
|
project, err = projectCache.GetProjectFromCwd()
|
2023-12-11 20:42:50 +00:00
|
|
|
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")
|
2023-12-11 20:54:22 +00:00
|
|
|
} else {
|
|
|
|
inCwd = true
|
2023-12-11 20:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise find from the given search string
|
|
|
|
if project == nil {
|
2024-01-14 15:33:15 +00:00
|
|
|
project = fzfFindProject(fzfOpts)
|
2023-12-11 20:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do a full fuzzy find if all else fails
|
2023-12-08 21:52:26 +00:00
|
|
|
if project == nil {
|
|
|
|
var err error
|
2024-01-14 15:33:15 +00:00
|
|
|
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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:42:50 +00:00
|
|
|
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))
|
2023-12-11 20:42:50 +00:00
|
|
|
fmt.Println()
|
2023-12-11 20:54:22 +00:00
|
|
|
|
|
|
|
if inCwd {
|
2024-01-15 21:02:15 +00:00
|
|
|
project.SetRepo(projectCache.OpenProject(cmd.Context(), project))
|
2023-12-11 20:54:22 +00:00
|
|
|
fmt.Fprintln(os.Stderr, project.GetGitInfo()+"\n")
|
|
|
|
}
|
2023-12-08 21:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
projectCmd.AddCommand(projectShowCmd)
|
2023-12-11 20:42:50 +00:00
|
|
|
projectShowCmd.PersistentFlags().Bool("current", false, "Use project in CWD rather than fuzzy find")
|
|
|
|
viper.BindPFlag("project.show.current", projectShowCmd.Flag("current"))
|
2023-12-08 21:52:26 +00:00
|
|
|
}
|