-
Notifications
You must be signed in to change notification settings - Fork 58
/
info.go
153 lines (125 loc) · 3.34 KB
/
info.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2021 The GoRE Authors. All rights reserved.
// Use of this source code is governed by the license that
// can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cheynewallace/tabby"
gore "github.com/goretk/gore"
"github.com/spf13/cobra"
)
func init() {
infoCMD := &cobra.Command{
Use: "info path/to/go/file",
Aliases: []string{"metadata", "i"},
Short: "Print summary information.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
listInfo(args[0])
},
}
gomodCMD := &cobra.Command{
Use: "gomod /path/to/go/file",
Aliases: []string{"gosum", "gm"},
Short: "Display go mod information.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
listModInfo(args[0])
},
}
rootCmd.AddCommand(gomodCMD)
rootCmd.AddCommand(infoCMD)
}
func listInfo(fileStr string) {
fp, err := filepath.Abs(fileStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse the filepath: %s.\n", err)
os.Exit(1)
}
f, err := gore.Open(fp)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when opening the file: %s.\n", err)
os.Exit(1)
}
defer f.Close()
t := tabby.New()
t.AddLine("OS", f.FileInfo.OS)
t.AddLine("Arch", f.FileInfo.Arch)
if comp, err := f.GetCompilerVersion(); err == nil {
t.AddLine("Compiler", fmt.Sprintf("%s (%s)", strings.TrimPrefix(comp.Name, "go"), strings.Split(comp.Timestamp, "T")[0]))
}
if f.BuildID != "" {
t.AddLine("Build ID", f.BuildID)
}
if root, err := f.GetGoRoot(); err == nil {
t.AddLine("GoRoot", root)
}
if pkg, err := f.GetPackages(); err == nil {
for _, p := range pkg {
if p.Name == "main" {
t.AddLine("Main root", p.Filepath)
break
}
}
t.AddLine("# main", len(pkg))
std, _ := f.GetSTDLib()
t.AddLine("# std", len(std))
ven, _ := f.GetVendors()
t.AddLine("# vendor", len(ven))
unk, _ := f.GetUnknown()
if len(unk) != 0 {
t.AddLine("# unknown", len(unk))
}
}
// Display extracted build information if it's available.
if f.BuildInfo != nil && f.BuildInfo.ModInfo != nil && len(f.BuildInfo.ModInfo.Settings) != 0 {
for _, set := range f.BuildInfo.ModInfo.Settings {
t.AddLine(set.Key, set.Value)
}
}
t.Print()
}
func listModInfo(fileStr string) {
fp, err := filepath.Abs(fileStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse the filepath: %s.\n", err)
os.Exit(1)
}
f, err := gore.Open(fp)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when opening the file: %s.\n", err)
os.Exit(1)
}
defer f.Close()
if f.BuildInfo == nil {
fmt.Fprintf(os.Stderr, "No build info found in the file.\n")
return
}
if f.BuildInfo.ModInfo == nil {
fmt.Fprintf(os.Stderr, "No mod info found in the file.\n")
return
}
mod := f.BuildInfo.ModInfo
t := tabby.New()
t.AddHeader("Type", "Name", "Version", "Replaced by", "Hash")
// If the main path is empty, we fallback to the path in the
// build info.
mainPath := mod.Main.Path
if mainPath == "" {
mainPath = mod.Path
}
t.AddLine("main", mainPath, mod.Main.Version, "", mod.Main.Sum)
for _, m := range mod.Deps {
if m.Replace != nil {
t.AddLine("dep", m.Path, m.Version, m.Replace.Path, m.Sum)
t.AddLine("replacement", m.Replace.Path, m.Replace.Version, "", m.Sum)
} else {
t.AddLine("dep", m.Path, m.Version, "", m.Sum)
}
}
t.Print()
fmt.Printf("\n")
}