git-project-manager/cmd/project/project_show.go

86 lines
2.3 KiB
Go
Raw Normal View History

2024-12-30 20:50:31 +00:00
package project
2023-12-08 21:52:26 +00:00
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-12-30 20:50:31 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
2024-12-19 19:55:49 +00:00
"gitea.libretechconsulting.com/rmcguire/git-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]",
2024-12-27 22:42:44 +00:00
Short: "Show detail for a Git project",
2023-12-10 16:15:52 +00:00
Aliases: []string{"cat", "s"},
Args: cobra.ArbitraryArgs,
2024-12-30 20:50:31 +00:00
Long: util.ProjShowCmdLong,
2023-12-10 16:15:52 +00:00
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-12-30 20:50:31 +00:00
remotes := viper.GetStringSlice(util.FlagRemote)
fzfOpts := &util.FzfProjectOpts{
Ctx: cmd.Context(),
2024-12-30 20:50:31 +00:00
Search: utils.SearchStringFromArgs(args),
Remotes: remotes,
}
// Try to find project from current directory
2024-12-30 20:50:31 +00:00
if viper.GetBool(util.ViperProjectShowCurrent) {
var err error
2024-12-30 20:50:31 +00:00
project, err = utils.Cache().GetProjectFromCwd()
if err != nil {
// Not an error because we're still going to try to find a project
2024-12-30 20:50:31 +00:00
utils.Logger().Warn("Failed to get project from current directory", utils.Logger().Args(
"error", err,
))
} else if project == nil {
2024-12-30 20:50:31 +00:00
utils.Logger().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 {
2024-12-30 20:50:31 +00:00
project = utils.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
2024-12-30 20:50:31 +00:00
project, err = utils.FzfProject(fzfOpts)
2023-12-08 21:52:26 +00:00
if err != nil || project == nil {
2024-12-30 20:50:31 +00:00
utils.Logger().Fatal("Failed to find project, nothing to show", utils.Logger().Args(
2023-12-08 21:52:26 +00:00
"error", err,
))
}
}
fmt.Println()
pterm.DefaultBox.
WithLeftPadding(5).WithRightPadding(5).
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Project Information"))).
2024-12-30 20:50:31 +00:00
Println(utils.Cache().ProjectString(project))
fmt.Println()
if inCwd {
2024-12-30 20:50:31 +00:00
project.SetRepo(utils.Cache().OpenProject(cmd.Context(), project))
fmt.Fprintln(os.Stderr, project.GetGitInfo()+"\n")
}
2023-12-08 21:52:26 +00:00
}
func init() {
2024-12-30 20:50:31 +00:00
ProjectCmd.AddCommand(projectShowCmd)
projectShowCmd.PersistentFlags().Bool(util.FlagCurrent, false, "Use project in CWD rather than fuzzy find")
viper.BindPFlag(util.ViperProjectShowCurrent, projectShowCmd.Flag(util.FlagCurrent))
2023-12-08 21:52:26 +00:00
}