@@ -2,6 +2,7 @@ package cmd
22
33import (
44 "fmt"
5+ "sort"
56 "strings"
67
78 "github.com/codemonauts/shared-2fa/config"
@@ -17,20 +18,37 @@ var listCmd = &cobra.Command{
1718 Short : "List all available entries" ,
1819 Run : func (cmd * cobra.Command , args []string ) {
1920 sess , err := session .NewSessionWithOptions (session.Options {})
20- svc := ssm .New (sess , aws .NewConfig ().WithRegion (config .AWS_REGION ))
21- input := & ssm.DescribeParametersInput {}
22- data , err := svc .DescribeParameters (input )
2321 if err != nil {
24- fmt .Errorf (err .Error ())
22+ fmt .Println (err .Error ())
2523 return
2624 }
25+ svc := ssm .New (sess , aws .NewConfig ().WithRegion (config .AWS_REGION ))
26+ input := & ssm.DescribeParametersInput {}
27+
28+ var entries []string
29+
30+ svc .DescribeParametersPages (input ,
31+ func (page * ssm.DescribeParametersOutput , lastPage bool ) bool {
32+ for _ , entry := range page .Parameters {
33+ // Only parameters with our defined prefix are part of this tool
34+ if strings .HasPrefix (* entry .Name , config .NAME_PREFIX ) {
35+ cleanName := strings .Replace (* entry .Name , config .NAME_PREFIX , "" , 1 )
36+ entries = append (entries , cleanName )
37+ }
38+ }
39+ return ! lastPage
40+ })
41+
42+ if len (entries ) > 0 {
43+ // Sort entries alphabetically
44+ sort .Strings (entries )
2745
28- fmt .Println ("Found the following 2FA entries:" )
29- for _ , entry := range data .Parameters {
30- if strings .HasPrefix (* entry .Name , config .NAME_PREFIX ) {
31- cleanName := strings .Replace (* entry .Name , config .NAME_PREFIX , "" , 1 )
32- fmt .Printf ("- %s\n " , cleanName )
46+ fmt .Println ("Found the following entries:" )
47+ for _ , entry := range entries {
48+ fmt .Printf ("- %s\n " , entry )
3349 }
50+ } else {
51+ fmt .Println ("Found no entries." )
3452 }
3553 },
3654}
0 commit comments