Skip to content

Commit

Permalink
Updates for glice audit to work but w/o tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Schinkel committed Oct 19, 2022
1 parent f28b4a2 commit 32d5b9e
Show file tree
Hide file tree
Showing 24 changed files with 372 additions and 237 deletions.
76 changes: 0 additions & 76 deletions cmd/glice/main.go

This file was deleted.

12 changes: 7 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cmd

import (
"os"

"github.com/ribice/glice/v3/pkg"
"github.com/spf13/cobra"
"os"
"strconv"
)

/*
Expand Down Expand Up @@ -47,7 +47,7 @@ func Execute() {
}

var indirect bool
var verbose bool
var verbose int
var logOutput bool
var nocache bool
var logfile string
Expand All @@ -57,17 +57,19 @@ var cachefile string
func init() {
pf := rootCmd.PersistentFlags()
pf.BoolVar(&indirect, "indirect", false, "Include indirect dependencies")
pf.BoolVar(&verbose, "verbose", false, "Generate verbose output")
pf.IntVar(&verbose, "verbose", glice.NoteLevel, "Verbosity Level: 0=all, 1=info, 2=warn, 3=error, 4=fail")
pf.Lookup("verbose").NoOptDefVal = strconv.Itoa(glice.InfoLevel)
pf.BoolVar(&logOutput, "log", false, "Log output to default logging filepath.")
pf.StringVar(&logfile, "logfile", "", "File to log output to.")
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")
}

func initOptions() {
glice.SetOptions(&glice.Options{
LogVerbosely: verbose,
VerbosityLevel: verbose,
IncludeIndirect: indirect,
LogOuput: logOutput,
NoCache: nocache,
Expand Down
4 changes: 2 additions & 2 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"fmt"
glice "github.com/ribice/glice/v3/pkg"

"github.com/spf13/cobra"
)
Expand All @@ -21,7 +21,7 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("test called")
glice.GetOptions()
},
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/fatih/color v1.13.0
github.com/google/go-github v17.0.0+incompatible
github.com/mikeschinkel/go-phpcereal v0.0.0-20220731232732-62bf5394d863
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.6.0
golang.org/x/mod v0.5.1
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -127,8 +128,11 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mikeschinkel/go-phpcereal v0.0.0-20220731232732-62bf5394d863 h1:6XVWb/g5RerLSG0Yzd1W/LQPRWbU0fGSQPVCFmtYD5s=
github.com/mikeschinkel/go-phpcereal v0.0.0-20220731232732-62bf5394d863/go.mod h1:q9EQUR9CyWvL0qiKqc/VM6RtnOEphwm3FzqedlmrlW8=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
Expand All @@ -141,6 +145,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
36 changes: 23 additions & 13 deletions pkg/audit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package glice

import (
"fmt"
"github.com/spf13/cobra"
"os"
)
Expand All @@ -12,38 +11,49 @@ func RunAudit(cmd *cobra.Command, args []string) {

options := GetOptions()

fmt.Println("\nAuditing...")
Notef("\nBeginning License Audit")
yf, err := LoadYAMLFile(options.SourceDir)
if err != nil {
LogAndExit(exitYAMLFileDoesNotExist,
Failf(exitYAMLFileDoesNotExist,
"Cannot run scan; %s",
err.Error())

}
fmt.Printf("YAML file %s loaded\n", yf.Filepath)
Notef("\nYAML file %s loaded", yf.Filepath)

fmt.Print("Scanning dependencies...")
Notef("\nScanning dependencies...")
deps, err = ScanDependencies(options)
if err != nil {
LogAndExit(exitCannotParseDependencies,
Failf(exitCannotParseDependencies,
"Failed while parsing dependencies: %s",
err.Error())
}

changes, el := yf.AuditDependencies(deps)
Notef("\nAuditing dependencies...")
changes, ds := yf.AuditDependencies(deps)
Notef("\nAudit complete.\n")

if !changes.HasChanges() {
fmt.Println("\nNo chances detected")
Notef("\nNo chances detected")
} else {
fmt.Println()
Notef("\n")
changes.Print()
}

if !el.HasErrors() {
fmt.Println("\nNo disallowed licenses detected")
Errorf("\n")
if !ds.HasDisalloweds() {
Notef("\nOnly allowed licenses detected")
Errorf("\n")
} else {
el.LogPrintWithHeader("ERROR! Disallowed Licenses Detected:")
Errorf("\nDisallowed licenses detected:")
Errorf("\n")
ds.LogPrint()
Errorf("\n")
Errorf("\nAudit FAILED!")
Errorf("\n\n")
os.Exit(exitAuditFoundDisallowedLicenses)
}

fmt.Println("\nAudit completed successfully")
Notef("\nAudit completed successfully")
Notef("\n\n")
}
4 changes: 2 additions & 2 deletions pkg/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ var cacheFilepath = filepath.Join(CacheDir(), CacheFilename)
func CacheDir() string {
dir, err := os.UserCacheDir()
if err != nil {
LogAndExit(exitCannotGetCacheDir,
Failf(exitCannotGetCacheDir,
"Unable to get cache dir as %s",
err.Error())
}
dir = filepath.Join(dir, CacheSubDir)
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
LogAndExit(exitCannotCreateCacheDir,
Failf(exitCannotCreateCacheDir,
"Unable to get create cache subdir %s: %s",
err.Error())
}
Expand Down
36 changes: 20 additions & 16 deletions pkg/changes.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
package glice

import (
"log"
)
import "sort"

type Changes struct {
Old []string
New []string
Additions []string
Deletions []string
}

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

// HasChanges returns true if there are either old or new changes
func (c *Changes) HasChanges() bool {
return len(c.Old) > 0 || len(c.New) > 0
return len(c.Additions) > 0 || len(c.Deletions) > 0
}

// Print outputs all changes, old and new
func (c *Changes) Print() {
LogPrintFunc(func() {
showChanges(c.Old, "Old", "These imports were not found in glice.yaml but were found when scanning.")
showChanges(c.New, "New", "These imports were not found when scanning but were found in glice.yaml.")
LogPrintFunc(WarnLevel, func() {
showChanges(c.Additions, "Additions", "These imports were not found in glice.yaml but were found when scanning:")
showChanges(c.Deletions, "Deletions", "These imports were not found when scanning but were found in glice.yaml:")
})
}

func showChanges(list []string, _type, descr string) {
log.Printf("\nChanges: %s", _type)
log.Println("------------")
log.Println(descr)
if len(list) == 0 {
goto end
}
sort.Strings(list)
Notef("\n%s", _type)
Notef("\n---------")
Notef("\n%s", descr)
Notef("\n")
for _, imp := range list {
log.Printf(" - %s\n", imp)
Notef("\n - %s", imp)
}
log.Println("")
Notef("\n\n")
end:
}
23 changes: 11 additions & 12 deletions pkg/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package glice

import (
"context"
"fmt"
"github.com/fatih/color"
"log"
)

type Dependencies []*Dependency
Expand Down Expand Up @@ -31,7 +31,6 @@ type Dependency struct {
}

func GetDependencyFromRepository(ctx context.Context, r *Repository) *Dependency {
r.Context = ctx
return &Dependency{
r: r,
Import: r.Import,
Expand Down Expand Up @@ -83,7 +82,7 @@ func (d *Dependency) GetColorizedLicenseName() (name string) {

func ScanDependencies(options *Options) (ds Dependencies, err error) {
var repos Repositories
var dependencies Dependencies
var deps Dependencies

ctx := context.Background()

Expand All @@ -97,19 +96,19 @@ func ScanDependencies(options *Options) (ds Dependencies, err error) {
goto end
}

log.Printf("Found %d dependencies", len(repos))
Notef("\nFound %d dependencies", len(repos))
Notef("\nResolving licenses...")

dependencies = make(Dependencies, len(repos))
deps = make(Dependencies, len(repos))
for i, r := range repos {
log.Printf("Fetching license for: %s", r.Import)
d := GetDependencyFromRepository(ctx, r)
Infof("\nFetching license for: %s", r.Import)
err = r.ResolveLicense(ctx, GetOptions())
if err != nil {
log.Println(err)
continue
err = fmt.Errorf("failed to resolve license; %w", err)
goto end
}
dependencies[i] = d

deps[i] = GetDependencyFromRepository(ctx, r)
}
end:
return dependencies, nil
return deps, err
}
Loading

0 comments on commit 32d5b9e

Please sign in to comment.