2024-12-30 20:50:31 +00:00
|
|
|
package project
|
2023-12-08 21:52:26 +00:00
|
|
|
|
|
|
|
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-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
|
2023-12-11 20:54:22 +00:00
|
|
|
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{
|
2024-01-14 15:33:15 +00:00
|
|
|
Ctx: cmd.Context(),
|
2024-12-30 20:50:31 +00:00
|
|
|
Search: utils.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
|
2024-12-30 20:50:31 +00:00
|
|
|
if viper.GetBool(util.ViperProjectShowCurrent) {
|
2023-12-11 20:42:50 +00:00
|
|
|
var err error
|
2024-12-30 20:50:31 +00:00
|
|
|
project, err = utils.Cache().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
|
2024-12-30 20:50:31 +00:00
|
|
|
utils.Logger().Warn("Failed to get project from current directory", utils.Logger().Args(
|
2023-12-11 20:42:50 +00:00
|
|
|
"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")
|
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-12-30 20:50:31 +00:00
|
|
|
project = utils.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-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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-30 20:50:31 +00:00
|
|
|
Println(utils.Cache().ProjectString(project))
|
2023-12-11 20:42:50 +00:00
|
|
|
fmt.Println()
|
2023-12-11 20:54:22 +00:00
|
|
|
|
|
|
|
if inCwd {
|
2024-12-30 20:50:31 +00:00
|
|
|
project.SetRepo(utils.Cache().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() {
|
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
|
|
|
}
|