Skip to content

Commit

Permalink
Merge pull request #3 from psampaz/invalid_updates
Browse files Browse the repository at this point in the history
Check for invalid updates + internal packages
  • Loading branch information
psampaz authored Apr 22, 2019
2 parents d1a92e6 + 5742433 commit 890bd22
Show file tree
Hide file tree
Showing 13 changed files with 712 additions and 391 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [0.2.0] - 2019-04-22
### Added
- Extra column 'VALID TIMESTAMPS' which indicates if the timestamp of the new version is
actually newer that the current one
### Changed
- Packages are now internal

## [0.1.0] - 2019-04-22
### Added
- Initial release
397 changes: 213 additions & 184 deletions README.md

Large diffs are not rendered by default.

105 changes: 105 additions & 0 deletions internal/mod/mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package mod

import (
"time"
)

// Module holds information about a specifc module listed by go list
type Module struct {
Path string `json:",omitempty"` // module path
Version string `json:",omitempty"` // module version
Versions []string `json:",omitempty"` // available module versions
Replace *Module `json:",omitempty"` // replaced by this module
Time *time.Time `json:",omitempty"` // time version was created
Update *Module `json:",omitempty"` // available update (with -u)
Main bool `json:",omitempty"` // is this the main module?
Indirect bool `json:",omitempty"` // module is only indirectly needed by main module
Dir string `json:",omitempty"` // directory holding local copy of files, if any
GoMod string `json:",omitempty"` // path to go.mod file describing module, if any
Error *ModuleError `json:",omitempty"` // error loading module
GoVersion string `json:",omitempty"` // go version used in module
}

type ModuleError struct {
Err string // error text
}

// InvalidTimestamp checks if the version reported as update by the go list command is actually newer that current version
func (m *Module) InvalidTimestamp() bool {
var mod Module
if m.Replace != nil {
mod = *m.Replace
} else {
mod = *m
}

if mod.Time != nil && mod.Update != nil {
return mod.Time.After(*mod.Update.Time)
}

return false
}

// CurrentVersion returns the current version of the module taking into consideration the any Replace settings
func (m *Module) CurrentVersion() string {
var mod Module
if m.Replace != nil {
mod = *m.Replace

} else {
mod = *m
}

return mod.Version
}

// HasUpdate checks if the module has a new version
func (m *Module) HasUpdate() bool {
var mod Module
if m.Replace != nil {
mod = *m.Replace
} else {
mod = *m
}

return mod.Update != nil
}

// New Version returns the version of the update taking into consideration the any Replace settings
func (m *Module) NewVersion() string {
var mod Module
if m.Replace != nil {
mod = *m.Replace
} else {
mod = *m
}

if mod.Update == nil {
return ""
}

return mod.Update.Version
}

// FilterModules filters the list of modules provided by the go list command
func FilterModules(modules []Module, hasUpdate, isDirect bool) []Module {
out := make([]Module, 0)
for k := range modules {

if modules[k].Main {
continue
}

if hasUpdate && modules[k].Update == nil {
continue
}

if isDirect && modules[k].Indirect {
continue
}

out = append(out, modules[k])
}

return out
}
Loading

0 comments on commit 890bd22

Please sign in to comment.