|
| 1 | +package mod |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | +) |
| 6 | + |
| 7 | +// Module holds information about a specifc module listed by go list |
| 8 | +type Module struct { |
| 9 | + Path string `json:",omitempty"` // module path |
| 10 | + Version string `json:",omitempty"` // module version |
| 11 | + Versions []string `json:",omitempty"` // available module versions |
| 12 | + Replace *Module `json:",omitempty"` // replaced by this module |
| 13 | + Time *time.Time `json:",omitempty"` // time version was created |
| 14 | + Update *Module `json:",omitempty"` // available update (with -u) |
| 15 | + Main bool `json:",omitempty"` // is this the main module? |
| 16 | + Indirect bool `json:",omitempty"` // module is only indirectly needed by main module |
| 17 | + Dir string `json:",omitempty"` // directory holding local copy of files, if any |
| 18 | + GoMod string `json:",omitempty"` // path to go.mod file describing module, if any |
| 19 | + Error *ModuleError `json:",omitempty"` // error loading module |
| 20 | + GoVersion string `json:",omitempty"` // go version used in module |
| 21 | +} |
| 22 | + |
| 23 | +type ModuleError struct { |
| 24 | + Err string // error text |
| 25 | +} |
| 26 | + |
| 27 | +// InvalidTimestamp checks if the version reported as update by the go list command is actually newer that current version |
| 28 | +func (m *Module) InvalidTimestamp() bool { |
| 29 | + var mod Module |
| 30 | + if m.Replace != nil { |
| 31 | + mod = *m.Replace |
| 32 | + } else { |
| 33 | + mod = *m |
| 34 | + } |
| 35 | + |
| 36 | + if mod.Time != nil && mod.Update != nil { |
| 37 | + return mod.Time.After(*mod.Update.Time) |
| 38 | + } |
| 39 | + |
| 40 | + return false |
| 41 | +} |
| 42 | + |
| 43 | +// CurrentVersion returns the current version of the module taking into consideration the any Replace settings |
| 44 | +func (m *Module) CurrentVersion() string { |
| 45 | + var mod Module |
| 46 | + if m.Replace != nil { |
| 47 | + mod = *m.Replace |
| 48 | + |
| 49 | + } else { |
| 50 | + mod = *m |
| 51 | + } |
| 52 | + |
| 53 | + return mod.Version |
| 54 | +} |
| 55 | + |
| 56 | +// HasUpdate checks if the module has a new version |
| 57 | +func (m *Module) HasUpdate() bool { |
| 58 | + var mod Module |
| 59 | + if m.Replace != nil { |
| 60 | + mod = *m.Replace |
| 61 | + } else { |
| 62 | + mod = *m |
| 63 | + } |
| 64 | + |
| 65 | + return mod.Update != nil |
| 66 | +} |
| 67 | + |
| 68 | +// New Version returns the version of the update taking into consideration the any Replace settings |
| 69 | +func (m *Module) NewVersion() string { |
| 70 | + var mod Module |
| 71 | + if m.Replace != nil { |
| 72 | + mod = *m.Replace |
| 73 | + } else { |
| 74 | + mod = *m |
| 75 | + } |
| 76 | + |
| 77 | + if mod.Update == nil { |
| 78 | + return "" |
| 79 | + } |
| 80 | + |
| 81 | + return mod.Update.Version |
| 82 | +} |
| 83 | + |
| 84 | +// FilterModules filters the list of modules provided by the go list command |
| 85 | +func FilterModules(modules []Module, hasUpdate, isDirect bool) []Module { |
| 86 | + out := make([]Module, 0) |
| 87 | + for k := range modules { |
| 88 | + |
| 89 | + if modules[k].Main { |
| 90 | + continue |
| 91 | + } |
| 92 | + |
| 93 | + if hasUpdate && modules[k].Update == nil { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + if isDirect && modules[k].Indirect { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + out = append(out, modules[k]) |
| 102 | + } |
| 103 | + |
| 104 | + return out |
| 105 | +} |
0 commit comments