...
1
2
3
4
5 package modinfo
6
7 import (
8 "cmd/go/internal/modfetch/codehost"
9 "encoding/json"
10 "time"
11 )
12
13
14
15
16 type ModulePublic struct {
17 Path string `json:",omitempty"`
18 Version string `json:",omitempty"`
19 Query string `json:",omitempty"`
20 Versions []string `json:",omitempty"`
21 Replace *ModulePublic `json:",omitempty"`
22 Time *time.Time `json:",omitempty"`
23 Update *ModulePublic `json:",omitempty"`
24 Main bool `json:",omitempty"`
25 Indirect bool `json:",omitempty"`
26 Dir string `json:",omitempty"`
27 GoMod string `json:",omitempty"`
28 GoVersion string `json:",omitempty"`
29 Retracted []string `json:",omitempty"`
30 Deprecated string `json:",omitempty"`
31 Error *ModuleError `json:",omitempty"`
32
33 Origin *codehost.Origin `json:",omitempty"`
34 Reuse bool `json:",omitempty"`
35 }
36
37 type ModuleError struct {
38 Err string
39 }
40
41 type moduleErrorNoMethods ModuleError
42
43
44
45
46 func (e *ModuleError) UnmarshalJSON(data []byte) error {
47 if len(data) > 0 && data[0] == '"' {
48 return json.Unmarshal(data, &e.Err)
49 }
50 return json.Unmarshal(data, (*moduleErrorNoMethods)(e))
51 }
52
53 func (m *ModulePublic) String() string {
54 s := m.Path
55 versionString := func(mm *ModulePublic) string {
56 v := mm.Version
57 if len(mm.Retracted) == 0 {
58 return v
59 }
60 return v + " (retracted)"
61 }
62
63 if m.Version != "" {
64 s += " " + versionString(m)
65 if m.Update != nil {
66 s += " [" + versionString(m.Update) + "]"
67 }
68 }
69 if m.Deprecated != "" {
70 s += " (deprecated)"
71 }
72 if m.Replace != nil {
73 s += " => " + m.Replace.Path
74 if m.Replace.Version != "" {
75 s += " " + versionString(m.Replace)
76 if m.Replace.Update != nil {
77 s += " [" + versionString(m.Replace.Update) + "]"
78 }
79 }
80 if m.Replace.Deprecated != "" {
81 s += " (deprecated)"
82 }
83 }
84 return s
85 }
86
View as plain text