From a89b33f0db9114d601adb05ab780d7d7380b8f73 Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Wed, 9 Apr 2025 13:07:34 +0200 Subject: [PATCH] refactor(cmd): reformat the command to match other commands --- src/cmd/gh.go | 92 +++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/src/cmd/gh.go b/src/cmd/gh.go index 46995c0..b29de2c 100644 --- a/src/cmd/gh.go +++ b/src/cmd/gh.go @@ -4,56 +4,70 @@ Copyright © 2025 NAME HERE package cmd import ( + "fmt" + "github.com/Slug-Boi/cocommit/src/cmd/tui" "github.com/Slug-Boi/cocommit/src/cmd/utils" + //"github.com/charmbracelet/lipgloss" "github.com/spf13/cobra" ) // ghProfileCmd represents the ghProfile command -var ghCmd = &cobra.Command{ - Use: "gh ", - Short: "This command will create add a github profile to your author list for use in commits", - Long: `This command will create add a github profile to your author list. - You just have to run the command with a username of the github profile you want to add. - The email will be added manually by following the TUI or adding the email flag to the command.`, - Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - email, _ := cmd.Flags().GetString("email") - shortname, _ := cmd.Flags().GetString("shortname") - longname, _ := cmd.Flags().GetString("longname") - username, _ := cmd.Flags().GetString("username") - groups, _ := cmd.Flags().GetStringSlice("groups") - exclude, _ := cmd.Flags().GetBool("exclude") +func GHCmd () *cobra.Command { + return &cobra.Command{ + Use: "gh ", + Short: "This command will create add a github profile to your author list for use in commits", + Long: `This command will create add a github profile to your author list. + You just have to run the command with a username of the github profile you want to add. + The email will be added manually by following the TUI or adding the email flag to the command.`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + email, _ := cmd.Flags().GetString("email") + shortname, _ := cmd.Flags().GetString("shortname") + longname, _ := cmd.Flags().GetString("longname") + username, _ := cmd.Flags().GetString("username") + groups, _ := cmd.Flags().GetStringSlice("groups") + exclude, _ := cmd.Flags().GetBool("exclude") - user := utils.FetchGithubProfile(args[0]) + user := utils.FetchGithubProfile(args[0]) - // Update values if flags are set - if shortname != "" { - user.Shortname = shortname - } - if longname != "" { - user.Longname = longname - } - if username != "" { - user.Username = username - } - if len(groups) > 0 { - user.Groups = groups - } - if exclude { - user.Ex = true - } - - if email != "" { - user.Email = email - } else { - // run the TUI to get the email - tui.EntryGHAuthorModel(user) - } - }, + // Update values if flags are set + if shortname != "" { + user.Shortname = shortname + } + if longname != "" { + user.Longname = longname + } + if username != "" { + user.Username = username + } + if len(groups) > 0 { + user.Groups = groups + } + if exclude { + user.Ex = true + } + + if email != "" { + user.Email = email + if utils.CheckUserFields(user) { + utils.CreateAuthor(user) + // print sucess message + //fmt.Print(lipgloss.NewStyle().Foreground(lipgloss.Color("170")).Render("Author added successfully")) + fmt.Print("Author added successfully\n") + } else { + panic("Invalid author data") + } + } else { + // run the TUI to get the email + tui.EntryGHAuthorModel(user) + } + }, + } } func init() { + ghCmd := GHCmd() rootCmd.AddCommand(ghCmd) ghCmd.Flags().StringP("email", "@", "", "Email to be used for the author") ghCmd.Flags().StringP("longname", "n", "", "Name to be used for the author")