git-project-manager/cmd/project_show.go
Ryan McGuire c78c41f567
All checks were successful
Build and Publish / release (push) Successful in 1m10s
Rename project to git-project-manager
2024-12-27 17:42:44 -05:00

85 lines
2.1 KiB
Go

package cmd
import (
"fmt"
"os"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
)
var projectShowCmd = &cobra.Command{
Use: "show [fuzzy alias search]",
Short: "Show detail for a Git project",
Aliases: []string{"cat", "s"},
Args: cobra.ArbitraryArgs,
Long: projShowCmdLong,
Run: projectShowCmdRun,
}
func projectShowCmdRun(cmd *cobra.Command, args []string) {
var project *projects.Project
var inCwd bool
remotes := viper.GetStringSlice(FlagRemote)
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
Search: searchStringFromArgs(args),
Remotes: remotes,
}
// Try to find project from current directory
if viper.GetBool(ViperProjectShowCurrent) {
var err error
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
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(projectCache.ProjectString(project))
fmt.Println()
if inCwd {
project.SetRepo(projectCache.OpenProject(cmd.Context(), project))
fmt.Fprintln(os.Stderr, project.GetGitInfo()+"\n")
}
}
func init() {
projectCmd.AddCommand(projectShowCmd)
projectShowCmd.PersistentFlags().Bool(FlagCurrent, false, "Use project in CWD rather than fuzzy find")
viper.BindPFlag(ViperProjectShowCurrent, projectShowCmd.Flag(FlagCurrent))
}