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

86 lines
2.3 KiB
Go

package project
import (
"fmt"
"os"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
"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: util.ProjShowCmdLong,
Run: projectShowCmdRun,
}
func projectShowCmdRun(cmd *cobra.Command, args []string) {
var project *projects.Project
var inCwd bool
remotes := viper.GetStringSlice(util.FlagRemote)
fzfOpts := &util.FzfProjectOpts{
Ctx: cmd.Context(),
Search: utils.SearchStringFromArgs(args),
Remotes: remotes,
}
// Try to find project from current directory
if viper.GetBool(util.ViperProjectShowCurrent) {
var err error
project, err = utils.Cache().GetProjectFromCwd()
if err != nil {
// Not an error because we're still going to try to find a project
utils.Logger().Warn("Failed to get project from current directory", utils.Logger().Args(
"error", err,
))
} else if project == nil {
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 {
project = utils.FzfFindProject(fzfOpts)
}
// Do a full fuzzy find if all else fails
if project == nil {
var err error
project, err = utils.FzfProject(fzfOpts)
if err != nil || project == nil {
utils.Logger().Fatal("Failed to find project, nothing to show", utils.Logger().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(utils.Cache().ProjectString(project))
fmt.Println()
if inCwd {
project.SetRepo(utils.Cache().OpenProject(cmd.Context(), project))
fmt.Fprintln(os.Stderr, project.GetGitInfo()+"\n")
}
}
func init() {
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))
}