-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,79 @@ | ||
/* | ||
Copyright © 2024 Shieldine <EMAIL ADDRESS> | ||
Copyright © 2024 Shieldine <74987363+Shieldine@users.noreply.github.com> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Shieldine/git-profile/internal" | ||
"github.com/Shieldine/git-profile/models" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
profileName = "" | ||
name string | ||
email string | ||
origin string | ||
) | ||
|
||
var lsCmd = &cobra.Command{ | ||
Use: "list", | ||
Use: "list [profile-name]", | ||
Aliases: []string{"l", "ls"}, | ||
Args: cobra.MaximumNArgs(1), | ||
Short: "List profiles", | ||
Long: `Display all profiles currently present in your config.`, | ||
Long: `Display profiles currently present in your config. | ||
Provide a profile name to list the attributes of the specified profile. | ||
Use flags to filter for a specific origin, name or email. | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
Users := internal.GetAllProfiles() | ||
if len(Users) == 0 { | ||
if len(args) != 0 { | ||
profileName = args[0] | ||
|
||
Profile := internal.GetProfileByName(profileName) | ||
|
||
if (models.ProfileConfig{}) == Profile { | ||
fmt.Printf("Profile %s doesn't exist.", profileName) | ||
return | ||
} | ||
|
||
PrintProfile(Profile) | ||
return | ||
} | ||
|
||
Profiles := internal.GetAllProfiles() | ||
|
||
if len(Profiles) == 0 { | ||
fmt.Println("No profiles to display.") | ||
} else { | ||
for _, user := range Users { | ||
fmt.Println(user) | ||
for _, profile := range Profiles { | ||
if name == "" && email == "" && origin == "" { | ||
PrintProfile(profile) | ||
} else if name != "" && name != profile.Name { | ||
continue | ||
} else if email != "" && email != profile.Email { | ||
continue | ||
} else if origin != "" && origin != profile.Origin { | ||
continue | ||
} | ||
PrintProfile(profile) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(lsCmd) | ||
lsCmd.Flags().StringVarP(&name, "name", "n", "", "List profiles with matching name") | ||
lsCmd.Flags().StringVarP(&email, "email", "e", "", "List profiles with matching email") | ||
lsCmd.Flags().StringVarP(&origin, "origin", "o", "", "List profiles with matching origin") | ||
} | ||
|
||
func PrintProfile(profile models.ProfileConfig) { | ||
fmt.Printf("Profile %s:\n", profile.ProfileName) | ||
fmt.Printf(" Origin: %s\n", profile.Origin) | ||
fmt.Printf(" Name: %s\n", profile.Name) | ||
fmt.Printf(" Email: %s\n", profile.Email) | ||
fmt.Println() | ||
} |