Skip to content

Commit

Permalink
✨ implement list command
Browse files Browse the repository at this point in the history
  • Loading branch information
Shieldine committed Jul 26, 2024
1 parent 118ff99 commit dcb62a5
Showing 1 changed file with 55 additions and 7 deletions.
62 changes: 55 additions & 7 deletions cmd/ls.go
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()
}

0 comments on commit dcb62a5

Please sign in to comment.