/
list.go
62 lines (53 loc) · 1.37 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cmd
import (
"fmt"
"strconv"
"github.com/fatih/color"
"github.com/nao1215/gup/internal/goutil"
"github.com/nao1215/gup/internal/print"
"github.com/spf13/cobra"
)
func newListCmd() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List up command name with package path and version under $GOPATH/bin or $GOBIN",
Long: `List up command name with package path and version under $GOPATH/bin or $GOBIN`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: func(cmd *cobra.Command, args []string) {
OsExit(list(cmd, args))
},
}
}
func list(cmd *cobra.Command, args []string) int {
if err := goutil.CanUseGoCmd(); err != nil {
print.Err(fmt.Errorf("%s: %w", "you didn't install golang", err))
return 1
}
pkgs, err := getPackageInfo()
if err != nil {
print.Err(err)
return 1
}
if len(pkgs) == 0 {
print.Err("unable to list up package: no package information")
return 1
}
printPackageList(pkgs)
return 0
}
// PackageList list up command package in $GOPATH/bin or $GOBIN
func printPackageList(pkgs []goutil.Package) {
max := 0
for _, v := range pkgs {
if len(v.Name) > max {
max = len(v.Name)
}
}
for _, v := range pkgs {
fmt.Fprintf(print.Stdout, "%"+strconv.Itoa(max)+"s: %s%s\n",
v.Name,
v.ImportPath,
color.GreenString("@"+v.Version.Current))
}
}