Skip to content

Commit

Permalink
Code cleanup as self-code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Schinkel committed Oct 25, 2022
1 parent c66f694 commit 5d2e5b0
Show file tree
Hide file tree
Showing 32 changed files with 917 additions and 1,411 deletions.
4 changes: 2 additions & 2 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ end:
}

// HandleChanges processes any additions and/or deletions when comparing dependencies
// in the project file with the dependences found on disk during scanning.
// in the project file with the dependencies found on disk during scanning.
//goland:noinspection GoUnusedParameter
func HandleChanges(ctx context.Context, pf *glice.ProjectFile) {
changes := pf.Changes
Expand All @@ -207,7 +207,7 @@ func HandleChanges(ctx context.Context, pf *glice.ProjectFile) {
NoteEnd()
} else {
NoteBegin()
changes.Print()
changes.LogPrint()
}
}

Expand Down
89 changes: 0 additions & 89 deletions pkg/api_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (c *Changes) HasChanges() bool {
return len(c.Additions) > 0 || len(c.Deletions) > 0
}

// Print outputs all changes, old and new
func (c *Changes) Print() {
// LogPrint outputs all changes, old and new
func (c *Changes) LogPrint() {
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:")
Expand Down
15 changes: 9 additions & 6 deletions pkg/const.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package glice

const AppName = "Glice"
const AppVersion = "3.0.0-alpha"

const CLIName = "glice"

const LicenseListSourceURL = "https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json"
const (
AppName = "Glice"
AppVersion = "3.0.0-alpha"
ProjectFileSchemaVersion = "v1"
ProjectFilename = "glice.yaml"
CLIName = "glice"
LicenseListSourceURL = "https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json"
NoAssertion = "NOASSERTION"
)
13 changes: 3 additions & 10 deletions pkg/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ func ScanDependencies(ctx context.Context, options *Options) (ds Dependencies, e
var repos Repositories
var deps Dependencies

//TODO Handle this concern somewhere
//if thanks && githubAPIKey == "" {
// return ErrNoAPIKey
//}

repos, err = ScanRepositories(ctx, options)
if err != nil {
goto end
Expand Down Expand Up @@ -50,14 +45,14 @@ func (deps Dependencies) ToEditorsAndOverrides(ctx context.Context) (editors Edi
overrides = make(Overrides, len(deps))
edMap := make(EditorMap, 0)
for index, dep := range deps {
eg, err := GetEditorGetter(dep)
ua, err := GetUserAdapter(dep)
if err != nil {
Warnf("Unable to add dependency '%s'; %w",
dep.Import,
err)
continue
}
ed := eg.GetEditor()
ed := NewEditor(ua)
overrides[index] = NewOverride(dep, ed)

id := ed.GetID()
Expand Down Expand Up @@ -180,8 +175,6 @@ func (Dependencies) GetReportHeader() []string {
return reportHeaderRow
}

const NoAssertion = "NOASSERTION"

// GetOverridesLicenseIDs returns a map of slices of LicenseIDs with map key
// being the Dependency Import and the LicenseIDs being the merger of the
// Dependencies License as reported by the VCS hot (e.g GitHub) and the
Expand All @@ -200,7 +193,7 @@ func (deps Dependencies) GetOverridesLicenseIDs(overrides Overrides) (licIDs Ove
if dep.LicenseID == NoAssertion {
depLicIDs = or.LicenseIDs
} else {
depLicIDs = gllicscan.UnionString([]string{dep.LicenseID}, or.LicenseIDs)
depLicIDs = gllicscan.UnionStringSlices([]string{dep.LicenseID}, or.LicenseIDs)
}
if len(depLicIDs) == 0 {
depLicIDs = []string{NoAssertion}
Expand Down
2 changes: 0 additions & 2 deletions pkg/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/fatih/color"
)

//goland:noinspection GoUnusedParameter

// Dependency holds information about a dependency
type Dependency struct {
r *Repository
Expand Down
10 changes: 10 additions & 0 deletions pkg/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ var (
defaultEmail = "email-alias@example.com"
)

func NewEditor(ua UserAdapter) *Editor {
e := &Editor{
Name: ua.GetName(),
Email: ua.GetEmail(),
Added: Timestamp(),
}
e.ID = e.GetID()
return e
}

func (em EditorMap) ToEditors() Editors {
editors := make(Editors, len(em))
index := 0
Expand Down
34 changes: 0 additions & 34 deletions pkg/editor_getter.go

This file was deleted.

2 changes: 2 additions & 0 deletions pkg/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ var (
ErrNoAPIKey = errors.New("the GITHUB_API_KEY environment variable is empty")

ErrCannotLogin = errors.New("host cannot login likely because of lacking credentials")

ErrRequestPrefixInstead = errors.New("prefix is a subset of import; request prefix instead")
)
36 changes: 19 additions & 17 deletions pkg/exit.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package glice

// Not using iota here because these values should never change once set
// and iota makes it too easy to accidentally change them.
const (
ExitUnexpectedError = -1
ExitAuditFoundDisallowedLicenses = 1
ExitCannotGetWorkingDir = 3
ExitCannotGetCacheDir = 4
ExitCannotCreateCacheDir = 5
ExitCannotScanDependencies = 6
ExitCannotSaveFile = 7
ExitCannotStatFile = 8
ExitFileExistsCannotOverwrite = 9
ExitFileDoesNotExist = 10
ExitRepoInfoGetterIsNil = 11
ExitCannotGetRepositoryAdapter = 12
ExitCannotSetOptions = 13
ExitHostNotYetSupported = 14
ExitCannotWriteReport = 15
ExitCannotCreateFile = 16
ExitCannotGetReportWriterAdapter = 17
ExitCannotReadFile = 18
ExitCannotUnmarshalJSON = 19
ExitCannotGetWorkingDir = 2
ExitCannotGetCacheDir = 3
ExitCannotCreateCacheDir = 4
ExitCannotScanDependencies = 5
ExitCannotSaveFile = 6
ExitCannotStatFile = 7
ExitFileExistsCannotOverwrite = 8
ExitFileDoesNotExist = 9
ExitRepoInfoGetterIsNil = 10
ExitCannotGetRepositoryAdapter = 11
ExitCannotSetOptions = 12
ExitHostNotYetSupported = 13
ExitCannotWriteReport = 14
ExitCannotCreateFile = 15
ExitCannotGetReportWriterAdapter = 16
ExitCannotReadFile = 17
ExitCannotUnmarshalJSON = 18
)
8 changes: 5 additions & 3 deletions pkg/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ end:
return err
}

type BackupRetentionType bool

//goland:noinspection GoUnusedConst
const (
RetainOriginalFile = true
DeleteOriginalFile = false
RetainOriginalFile BackupRetentionType = true
DeleteOriginalFile BackupRetentionType = false
)

// BackupFile creates a backup of the file passed by adding a ".bak"
// or ".<n>.bak" extension while maintaining all prior backups.
func BackupFile(fp string, retainOriginal bool) (bfs []string, err error) {
func BackupFile(fp string, retainOriginal BackupRetentionType) (bfs []string, err error) {
var bf string

if !FileExists(fp) {
Expand Down
26 changes: 17 additions & 9 deletions pkg/git.go → pkg/git_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@ import (
"strings"
)

type Git struct{}
var _ UserAdapter = (*GitUser)(nil)

func NewGit() *Git {
return &Git{}
type GitUser struct {
Name string
Email string
}

var editor *Editor
func (gu *GitUser) GetName() string {
return gu.Name
}

func (gu *GitUser) GetEmail() string {
return gu.Email
}

var user *GitUser

func (g *Git) GetEditor() *Editor {
func GetGitUser() UserAdapter {
var cmd *exec.Cmd
var name, email []byte
var err error

if editor != nil {
if user != nil {
goto end
}

Expand All @@ -34,11 +43,10 @@ func (g *Git) GetEditor() *Editor {
Warnf("Failed when calling `git config`; %s", err.Error())
email = []byte(defaultName)
}
editor = &Editor{
user = &GitUser{
Name: strings.TrimSpace(string(name)),
Email: strings.TrimSpace(string(email)),
}
editor.ID = editor.GetID()
end:
return editor
return user
}
Loading

0 comments on commit 5d2e5b0

Please sign in to comment.