Package subcommand code
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
package alias
|
||||
|
||||
import (
|
||||
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
|
||||
)
|
||||
|
||||
var AliasCmd = &cobra.Command{
|
||||
@ -23,7 +24,7 @@ func aliasCmdPreRun(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
func mustHaveAliases(cmd *cobra.Command, args []string) {
|
||||
utils := util.MustFromCtx(cmd.Context())
|
||||
utils = util.MustFromCtx(cmd.Context())
|
||||
|
||||
if len(utils.Cache().Aliases) == 0 {
|
||||
utils.Logger().Fatal("No aliases set, nothing to " + cmd.Name())
|
||||
|
@ -45,18 +45,19 @@ func runAddAliasCmd(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
addNewAliases(project.ID)
|
||||
AddNewAliases(cmd, project.ID)
|
||||
}
|
||||
|
||||
func addNewAliases(projectID int) {
|
||||
project := utils.Cache().GetProjectByID(projectID)
|
||||
func AddNewAliases(cmd *cobra.Command, projectID int) {
|
||||
u := util.MustFromCtx(cmd.Context())
|
||||
project := u.Cache().GetProjectByID(projectID)
|
||||
if project == nil {
|
||||
utils.Logger().Error("Failed to find project to alias", utils.Logger().Args("projectID", projectID))
|
||||
u.Logger().Error("Failed to find project to alias", u.Logger().Args("projectID", projectID))
|
||||
return
|
||||
}
|
||||
|
||||
// Collect the aliases
|
||||
aliases := promptAliasesForProject(project)
|
||||
aliases := PromptAliasesForProject(cmd, project)
|
||||
|
||||
// Add aliases
|
||||
for _, a := range aliases {
|
||||
@ -64,13 +65,13 @@ func addNewAliases(projectID int) {
|
||||
if a == "" {
|
||||
continue
|
||||
}
|
||||
if err := utils.Cache().AddAlias(a, project.ID, project.Remote); err != nil {
|
||||
utils.Logger().Debug("Skipping alias add", utils.Logger().Args(
|
||||
if err := u.Cache().AddAlias(a, project.ID, project.Remote); err != nil {
|
||||
u.Logger().Debug("Skipping alias add", u.Logger().Args(
|
||||
"error", err,
|
||||
"alias", a,
|
||||
))
|
||||
} else {
|
||||
utils.Logger().Info("Successfully added alias to project", utils.Logger().Args(
|
||||
u.Logger().Info("Successfully added alias to project", u.Logger().Args(
|
||||
"project", project.String(),
|
||||
"alias", a,
|
||||
))
|
||||
@ -78,10 +79,11 @@ func addNewAliases(projectID int) {
|
||||
}
|
||||
}
|
||||
|
||||
func promptAliasesForProject(p *projects.Project) []string {
|
||||
aliases := utils.Cache().GetProjectAliases(p)
|
||||
func PromptAliasesForProject(cmd *cobra.Command, p *projects.Project) []string {
|
||||
u := util.MustFromCtx(cmd.Context())
|
||||
aliases := u.Cache().GetProjectAliases(p)
|
||||
if len(aliases) > 0 {
|
||||
utils.Logger().Info("Adding aliases to project", utils.Logger().Args(
|
||||
u.Logger().Info("Adding aliases to project", u.Logger().Args(
|
||||
"project", p.String(),
|
||||
"existingAliases", cache.ProjectAliasesString(aliases),
|
||||
))
|
||||
|
@ -20,15 +20,11 @@ var aliasListCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func runListAliasCmd(cmd *cobra.Command, args []string) {
|
||||
remotes := viper.GetStringSlice(FlagRemote)
|
||||
remotes := viper.GetStringSlice(util.FlagRemote)
|
||||
pterm.DefaultBox.
|
||||
WithLeftPadding(5).WithRightPadding(5).
|
||||
WithBoxStyle(&pterm.Style{pterm.FgLightBlue}).
|
||||
WithTitle(pterm.Bold.Sprint(pterm.LightGreen("Aliases by Project"))).
|
||||
Print("\n" + projectCache.AliasesByProjectString(remotes...))
|
||||
Print("\n" + utils.Cache().AliasesByProjectString(remotes...))
|
||||
fmt.Print("\n\n")
|
||||
}
|
||||
|
||||
func init() {
|
||||
aliasCmd.AddCommand(aliasListCmd)
|
||||
}
|
||||
|
Reference in New Issue
Block a user