@@ -19,6 +19,7 @@ import (
19
19
"log/slog"
20
20
21
21
"github.com/blinklabs-io/cardano-up/pkgmgr"
22
+ "github.com/hashicorp/go-version"
22
23
"github.com/spf13/cobra"
23
24
)
24
25
@@ -27,12 +28,14 @@ var listFlags = struct {
27
28
}{}
28
29
29
30
func listAvailableCommand () * cobra.Command {
30
- return & cobra.Command {
31
+ cmd := & cobra.Command {
31
32
Use : "list-available" ,
32
33
Short : "List available packages" ,
33
34
Run : func (cmd * cobra.Command , args []string ) {
34
35
pm := createPackageManager ()
35
36
packages := pm .AvailablePackages ()
37
+ verbose , _ := cmd .Flags ().GetBool ("verbose" )
38
+
36
39
slog .Info ("Available packages:\n " )
37
40
slog .Info (
38
41
fmt .Sprintf (
@@ -42,28 +45,35 @@ func listAvailableCommand() *cobra.Command {
42
45
"Description" ,
43
46
),
44
47
)
45
- for _ , tmpPackage := range packages {
46
- slog .Info (
47
- fmt .Sprintf (
48
- "%-20s %-12s %s" ,
49
- tmpPackage .Name ,
50
- tmpPackage .Version ,
51
- tmpPackage .Description ,
52
- ),
53
- )
54
- if len (tmpPackage .Dependencies ) > 0 {
55
- tmpOutput := " Requires: "
56
- for idx , dep := range tmpPackage .Dependencies {
57
- tmpOutput += dep
58
- if idx < len (tmpPackage .Dependencies )- 1 {
59
- tmpOutput += ` | `
48
+ if verbose {
49
+ // show all versions of packages
50
+ for _ , tmpPackage := range packages {
51
+ printPackageInfo (tmpPackage )
52
+ }
53
+ } else {
54
+ // Shows only latest version of each package
55
+ latestPackages := make (map [string ]int )
56
+ order := make ([]string , 0 )
57
+ for index , pkg := range packages {
58
+ packageName := pkg .Name
59
+ packageVersion := pkg .Version
60
+ existingIndex , exists := latestPackages [packageName ]
61
+ if ! exists || compareVersions (packageVersion , packages [existingIndex ].Version ) {
62
+ if ! exists {
63
+ order = append (order , packageName )
60
64
}
65
+ latestPackages [packageName ] = index
61
66
}
62
- slog .Info (tmpOutput )
67
+ }
68
+ for _ , name := range order {
69
+ printPackageInfo (packages [latestPackages [name ]])
63
70
}
64
71
}
65
72
},
66
73
}
74
+ // Added a verbose flag
75
+ cmd .Flags ().BoolP ("verbose" , "v" , false , "Show all versions of packages" )
76
+ return cmd
67
77
}
68
78
69
79
func listCommand () * cobra.Command {
@@ -111,3 +121,36 @@ func listCommand() *cobra.Command {
111
121
BoolVarP (& listFlags .all , "all" , "A" , false , "show packages from all contexts (defaults to only active context)" )
112
122
return listCmd
113
123
}
124
+
125
+ // Prints packge details
126
+ func printPackageInfo (pkg pkgmgr.Package ) {
127
+ slog .Info (
128
+ fmt .Sprintf (
129
+ "%-20s %-12s %s" ,
130
+ pkg .Name ,
131
+ pkg .Version ,
132
+ pkg .Description ,
133
+ ),
134
+ )
135
+ if len (pkg .Dependencies ) > 0 {
136
+ tmpOutput := " Requires: "
137
+ for idx , dep := range pkg .Dependencies {
138
+ tmpOutput += dep
139
+ if idx < len (pkg .Dependencies )- 1 {
140
+ tmpOutput += ` | `
141
+ }
142
+ }
143
+ slog .Info (tmpOutput )
144
+ }
145
+ }
146
+
147
+ // Compare semantic version of packages
148
+ func compareVersions (v1 string , v2 string ) bool {
149
+ ver1 , err1 := version .NewVersion (v1 )
150
+ ver2 , err2 := version .NewVersion (v2 )
151
+
152
+ if err1 != nil || err2 != nil {
153
+ return v1 > v2
154
+ }
155
+ return ver1 .GreaterThan (ver2 )
156
+ }
0 commit comments