Skip to content

Commit

Permalink
Addedd glice overrides to generate initial overrides.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Schinkel committed Oct 20, 2022
1 parent 32d5b9e commit d41ce10
Show file tree
Hide file tree
Showing 28 changed files with 768 additions and 453 deletions.
13 changes: 2 additions & 11 deletions cmd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ var auditCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(auditCmd)
initCmd.Flags().Int("ttl", 24*60*60, "Time-to-Live for data in the cache file allowing recently audited dependencies to be skipped")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// auditCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// auditCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
addTTLFlag(auditCmd)
auditCmd.Flags().Bool("overrides", false, "Write an `overrides.yaml` file if any disallowed licenses are found.")
}
1 change: 0 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ var initCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().String("path", glice.SourceDir(""), "Directory path to your project's top-level go.mod file")
}
19 changes: 19 additions & 0 deletions cmd/overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
glice "github.com/ribice/glice/v3/pkg"
"github.com/spf13/cobra"
)

// overridesCmd represents the overrides command
var overridesCmd = &cobra.Command{
Use: "overrides",
Run: glice.RunOverrides,
Short: "Generate overrides.yaml for editing",
Long: `Generate overrides.yaml for manual copying into glice.yaml and then manual editing to by the user`,
}

func init() {
rootCmd.AddCommand(overridesCmd)
addTTLFlag(overridesCmd)
}
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ func init() {
pf.StringVar(&source, "source", glice.SourceDir(""), "Source directory where go.mod for the repo to audit is located.")
pf.StringVar(&cachefile, "cache-file", glice.CacheFilepath(), "Full filepath to the cachefile to create.")
pf.BoolVar(&nocache, "nocache", false, "Disable use of caching")

rootCmd.MarkFlagsMutuallyExclusive("nocache", "cache-file")
}

// initOptions create a glice.Options object and sets its values
// from the command line flags.
func initOptions() {
glice.SetOptions(&glice.Options{
VerbosityLevel: verbose,
Expand All @@ -78,3 +81,8 @@ func initOptions() {
CacheFilepath: cachefile,
})
}

// addTTLFlag allows multiple Commands to share the TTL Flag.
func addTTLFlag(cmd *cobra.Command) {
cmd.Flags().Int("ttl", 24*60*60, "Time-to-Live for data in the cache file allowing recently audited dependencies to be skipped")
}
59 changes: 0 additions & 59 deletions pkg/audit.go

This file was deleted.

18 changes: 10 additions & 8 deletions pkg/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package glice
import "sort"

type Changes struct {
Additions []string
Deletions []string
Additions Dependencies
Deletions Dependencies
}

func NewChanges() *Changes {
return &Changes{
Additions: make([]string, 0),
Deletions: make([]string, 0),
Additions: make(Dependencies, 0),
Deletions: make(Dependencies, 0),
}
}

Expand All @@ -27,17 +27,19 @@ func (c *Changes) Print() {
})
}

func showChanges(list []string, _type, descr string) {
func showChanges(list Dependencies, _type, descr string) {
if len(list) == 0 {
goto end
}
sort.Strings(list)
sort.Slice(list, func(i, j int) bool {
return list[i].Import < list[j].Import
})
Notef("\n%s", _type)
Notef("\n---------")
Notef("\n%s", descr)
Notef("\n")
for _, imp := range list {
Notef("\n - %s", imp)
for _, dep := range list {
Notef("\n - %s", dep.Import)
}
Notef("\n\n")
end:
Expand Down
60 changes: 55 additions & 5 deletions pkg/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"github.com/fatih/color"
"sort"
"strconv"
)

type Dependencies []*Dependency
Expand All @@ -18,6 +20,29 @@ func (deps Dependencies) ToMap() DependencyMap {
return newDeps
}

// ToEditorsAndOverrides returns a slice of *Dependency and a slice of unique *Editor
func (deps Dependencies) ToEditorsAndOverrides(ctx context.Context) (editors Editors, overrides Overrides) {
overrides = make(Overrides, len(deps))
edMap := make(EditorMap, 0)
for index, dep := range deps {
eg, err := GetEditorGetter(dep)
if err != nil {
Warnf("Unable to add dependency '%s'; %w",
dep.Import,
err)
continue
}
ed := eg.GetEditor()
overrides[index] = NewOverride(dep, ed)

if _, ok := edMap[ed.String()]; !ok {
edMap[ed.String()] = ed
}
}
editors = edMap.ToEditors()
return editors, overrides
}

// Dependency holds information about a dependency
type Dependency struct {
r *Repository
Expand All @@ -30,7 +55,7 @@ type Dependency struct {
LicenseURL string `yaml:"legalese" json:"legalese"`
}

func GetDependencyFromRepository(ctx context.Context, r *Repository) *Dependency {
func GetDependencyFromRepository(r *Repository) *Dependency {
return &Dependency{
r: r,
Import: r.Import,
Expand Down Expand Up @@ -80,12 +105,10 @@ func (d *Dependency) GetColorizedLicenseName() (name string) {
return color.New(d.GetColor()).Sprintf(name)
}

func ScanDependencies(options *Options) (ds Dependencies, err error) {
func ScanDependencies(ctx context.Context, options *Options) (ds Dependencies, err error) {
var repos Repositories
var deps Dependencies

ctx := context.Background()

//TODO Handle this concern somewhere
//if thanks && githubAPIKey == "" {
// return ErrNoAPIKey
Expand All @@ -107,8 +130,35 @@ func ScanDependencies(options *Options) (ds Dependencies, err error) {
err = fmt.Errorf("failed to resolve license; %w", err)
goto end
}
deps[i] = GetDependencyFromRepository(ctx, r)
deps[i] = GetDependencyFromRepository(r)
}
end:
return deps, err
}

// ImportWidth returns the length of the longest Import
func (deps Dependencies) ImportWidth() (width int) {
for _, d := range deps {
n := len(d.Import)
if n <= width {
continue
}
width = n
}
return width
}

// LogPrint outputs all rejections in list individually
func (deps Dependencies) LogPrint() {
level := ErrorLevel
LogPrintFunc(level, func() {
width := strconv.Itoa(deps.ImportWidth() + 2)
format := "\n%s: - %-" + width + "s %s"
sort.Slice(deps, func(i, j int) bool {
return deps[i].Import < deps[j].Import
})
for _, d := range deps {
LogPrintf(level, format, levels[level], d.Import+":", d.LicenseID)
}
})
}
51 changes: 0 additions & 51 deletions pkg/disallowed.go

This file was deleted.

25 changes: 22 additions & 3 deletions pkg/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,42 @@ import (
)

type Editors []*Editor
type EditorMap map[string]*Editor
type Editor struct {
Name string `yaml:"name"`
Email string `yaml:"email"`
Reference string `yaml:"ref"`
}

var (
defaultName = "Username Goes Here"
defaultEmail = "email-alias@example.com"
)

const numProperties = 3

var _ yaml.Marshaler = (*Editor)(nil)
var _ yaml.Unmarshaler = (*Editor)(nil)

func (e *Editor) MarshalYAML() (interface{}, error) {
func (em EditorMap) ToEditors() Editors {
editors := make(Editors, len(em))
index := 0
for _, ed := range em {
editors[index] = ed
index++
}
return editors
}

func (e *Editor) String() string {
if e.Reference == "" {
e.Reference = UpToN(e.Email, '@', 1)
}
editor := fmt.Sprintf("&%s %s <%s>", e.Reference, e.Name, e.Email)
return editor, nil
return fmt.Sprintf("&%s %s <%s>", e.Reference, e.Name, e.Email)
}

func (e *Editor) MarshalYAML() (interface{}, error) {
return e.String(), nil
}

var regexParseEditor = regexp.MustCompile(`^\s*&(\S+)\s+(.+)\s+<([^>]+)>\s*$`)
Expand Down
Loading

0 comments on commit d41ce10

Please sign in to comment.