Skip to content

Commit

Permalink
Added update, report save, version and verify commands
Browse files Browse the repository at this point in the history
- `glice update` updates glice.yaml with new additions. (TODO: Add --update to `glice audit`)
- `glice report save` writes dependencies to ascii table, JSON, YAML or CSV format. (TODO: Add HTML?)
- `glice version` just outputs the current version in 'Glice vN.N.N' format.
- `glice verify` just loads the `glice.yaml` file. This may be removed later.
- Also refactored to support `thanks` and `licenses` commands, but not tested yet.
  • Loading branch information
Mike Schinkel committed Oct 23, 2022
1 parent 1fc8db2 commit c27d0e0
Show file tree
Hide file tree
Showing 49 changed files with 1,129 additions and 764 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
[![Coverage Status](https://coveralls.io/repos/github/ribice/glice/badge.svg?branch=master)](https://coveralls.io/github/ribice/glice?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/ribice/glice)](https://goreportcard.com/report/github.com/ribice/glice)
-->
Golang license and dependency checker. Prints list of all dependencies, their URL, license and saves all the license files in /licenses.
License and dependency checker for GoLang projects. Prints list of all dependencies, their URL, license and saves all the license files in /licenses.

## Status/Intention

This code is in a sort of no-man's land state.

Is has been updated from the fork to a level sufficient to meet the needs of a client. The client wants to submit the changes back to the original project ([Glice](https://github.com/ribice/glice)) so that they do not need to maintain for their needs, but they have also asked me to only update to the level of meet their needs, at least for the time being, because they have other things for me to work on that they consider more urgent.
It has been updated from the fork to a level sufficient to meet the needs of a client. The client wants to submit the changes back to the original project ([Glice](https://github.com/ribice/glice)) so that they do not need to maintain for their needs, but they have also asked me to only update to the level of meet their needs, at least for the time being, because they have other things for me to work on that they consider more urgent.

OTOH it if a major breaking change to the forked code so the original developer may have zero interest in merging it. But even if they do want to merge the code the README does not yet reflect the changes made nor is the functionality fixed yet that was broken during refactoring, all per the client's limits on my time _(and I have had no free time to do on my own given the time required for the client's projects.)_

Expand All @@ -23,13 +23,10 @@ Feel free to use at your own risk. Also, feel free to submit issues if you have
- Update `.goReleaser.yaml` and ensure it can produce a viable release.
- (Re)Implement full suite of tests
- (Re)Implement these commands:
- `repost save` — Generate a list of dependencies and licenses to a file.
- `report print` — Generate a list of dependencies and licenses to Stdout.
- Figure out how apply colors to different licenses.
- `licenses save` — Write dependency's licenses to text files.
- `licenses download` — Download JSON of license from [spdx.org's GitHub](https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json).
- Add validation from this list to `glice audit`
- `thank` — Upvote each dependency on its host ("Star" on Github.)
- `test` — TBD

# Glice v2 README Follows
Expand Down
13 changes: 7 additions & 6 deletions cmd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ func init() {
auditCmd.Flags().Bool("overrides", false, "Write an `overrides.yaml` file if any disallowed licenses are found.")
}

//goland:noinspection GoUnusedParameter
func RunAudit(cmd *cobra.Command, args []string) {
ctx := context.Background()
glice.Notef("\n")
glice.Notef("\nBeginning License Audit")
deps := ScanningDependencies(ctx)
NoteBegin()
Notef("\nBeginning License Audit")
deps := ScanDependencies(ctx)
pf := AuditingProjectDependencies(ctx, deps)
glice.Notef("\n\n")
NoteEnd()
HandleChanges(ctx, pf)
exceptions := HasDisalloweds(ctx, pf)
exceptions := HandleDisalloweds(ctx, pf)
GeneratingOverrides(ctx, cmd, pf, glice.WarnLevel)
if exceptions {
os.Exit(glice.ExitAuditFoundDisallowedLicenses)
}
glice.Notef("\n\n")
NoteEnd()
}
9 changes: 5 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ func init() {
rootCmd.AddCommand(initCmd)
}

//goland:noinspection GoUnusedParameter
func RunInit(cmd *cobra.Command, args []string) {
ctx := context.Background()
glice.Notef("\n")
glice.Notef("\nInitializing %s for project", glice.AppName)
NoteBegin()
Notef("\nInitializing %s for project", glice.AppName)
pf := CreatingProjectFile(ctx)
pf.Dependencies = ScanningDependencies(ctx)
pf.Dependencies = ScanDependencies(ctx)
SavingProjectFile(ctx, pf)
glice.Notef("\n\n")
NoteEnd()
}
50 changes: 32 additions & 18 deletions cmd/licenses.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
package cmd

import (
"context"
"fmt"

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

const DefaultLicensesPath = "licenses"

// licensesCmd represents the licenses command
var licensesCmd = &cobra.Command{
Use: "licenses",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
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("licenses called")
},
Run: RunLicenses,
Short: "Write licenses for each dependency to a file",
}

func init() {
rootCmd.AddCommand(licensesCmd)
saveCmd.Flags().String("path",
DefaultLicensesPath,
fmt.Sprintf("Directory path in which to write licenses. Can be relative to %s, or absolute.",
glice.ProjectFilename))
}

// Here you will define your flags and configuration settings.
//goland:noinspection GoUnusedParameter
func RunLicenses(cmd *cobra.Command, args []string) {
dir := glice.Flag(cmd, "path")
ctx := context.Background()

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// licensesCmd.PersistentFlags().String("foo", "", "A help for foo")
NoteBegin()
Notef("\nWriting Licenses to %s", dir)
deps := ScanDependencies(ctx)
SavingLicenses(deps, dir)
NoteEnd()
}

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// licensesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
func SavingLicenses(deps glice.Dependencies, dir string) {
Notef("\nSaving Licenses")
err := deps.SaveLicenses(dir, func(dep *glice.Dependency, fp string) {
Infof("\nSaving license for %s to %s", dep.Import, fp)
})
if err != nil {
Failf(glice.ExitCannotSaveFile,
"\nUnable to write licenses for individual files; %w",
err)
}
Notef("\nLicenses saved")
}
9 changes: 5 additions & 4 deletions cmd/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ func init() {
addTTLFlag(overridesCmd)
}

//goland:noinspection GoUnusedParameter
func RunOverrides(cmd *cobra.Command, args []string) {
ctx := context.Background()
glice.Notef("\n")
glice.Notef("\nGenerating Overrides file")
deps := ScanningDependencies(ctx)
NoteBegin()
Notef("\nGenerating Overrides file")
deps := ScanDependencies(ctx)
pf := AuditingProjectDependencies(ctx, deps)
glice.Notef("\n\n")
NoteEnd()
GeneratingOverrides(ctx, cmd, pf, glice.ErrorLevel)
}
36 changes: 0 additions & 36 deletions cmd/print.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/print_test.go → cmd/print_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package glice_test
package cmd_test

//import (
// "bytes"
Expand Down Expand Up @@ -85,7 +85,7 @@ package glice_test
// for name, tt := range tests {
// t.Run(name, func(t *testing.T) {
// writeTo := &bytes.Buffer{}
// err := Print(tt.path, false, writeTo)
// err := AllPrint(tt.path, false, writeTo)
// if (err != nil) != tt.wantErr {
// t.Errorf("Print() error = %v, wantErr %v", err, tt.wantErr)
// return
Expand Down
23 changes: 1 addition & 22 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,15 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// reportCmd represents the report command
var reportCmd = &cobra.Command{
Use: "report",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
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("report called")
},
Short: "Generate a dependency report to screen or a file using a subcommand",
}

func init() {
rootCmd.AddCommand(reportCmd)

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// reportCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
12 changes: 8 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"github.com/ribice/glice/v3/pkg"
"github.com/spf13/cobra"
"os"
Expand All @@ -19,9 +20,9 @@ Commands & Switches:
audit - CI check
--ttl={cache_ttl}
report - Generate a license report
- print - Print license report to stdout
- write - Write license report to file
--file={report_file}
- print - AllPrint license report to stdout
- save - Write license report to file
--output={report_file}
text - Write licenses to text files
--output={output_dir}
thank - Give thanks by starring repositories
Expand Down Expand Up @@ -50,20 +51,22 @@ var direct bool
var verbose int
var logOutput bool
var nocache bool
var captureLic bool
var logfile string
var source string
var cachefile string

func init() {
pf := rootCmd.PersistentFlags()
pf.BoolVar(&direct, "direct-only", false, "Exclude direct dependencies")
pf.IntVar(&verbose, "verbose", glice.NoteLevel, "Verbosity Level: 0=all, 1=info, 2=warn, 3=error, 4=fail")
pf.IntVar(&verbose, "verbose", glice.NoteLevel, fmt.Sprintf("Specify a verbosity level: %s", glice.ValidVerbositiesString))
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.")
pf.StringVar(&cachefile, "cache-file", glice.CacheFilepath(), "Full filepath to the cachefile to create.")
pf.BoolVar(&nocache, "nocache", false, "Disable use of caching")
pf.BoolVar(&captureLic, "capture-license", false, "Download license from host while processing (slower)")

rootCmd.MarkFlagsMutuallyExclusive("nocache", "cache-file")
}
Expand All @@ -79,6 +82,7 @@ func initOptions() {
LogFilepath: logfile,
SourceDir: source,
CacheFilepath: cachefile,
CaptureLicense: captureLic,
})
}

Expand Down
43 changes: 23 additions & 20 deletions cmd/save.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
package cmd

import (
"context"
"fmt"

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

const (
DefaultReportFilename = "glice-dependencies-report.txt"
DefaultReportFormat = glice.TableFormat
)

// saveCmd represents the save command
var saveCmd = &cobra.Command{
Use: "save",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
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("save called")
},
Run: RunReportSave,
Short: fmt.Sprintf("Save a report to file in %s, %s or %s format", glice.TableFormat, glice.JSONFormat, glice.CSVFormat),
}

func init() {
reportCmd.AddCommand(saveCmd)
saveCmd.Flags().String("filename", DefaultReportFilename, "File to save the license report to")
saveCmd.Flags().String("format", string(DefaultReportFormat), fmt.Sprintf("Format in which to save report: %s, %s or %s", glice.TableFormat, glice.JSONFormat, glice.CSVFormat))
saveCmd.MarkFlagsMutuallyExclusive("filename", "format")
}

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// saveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
//goland:noinspection GoUnusedParameter
func RunReportSave(cmd *cobra.Command, args []string) {
NoteBegin()
Notef("\nGenerating report to save")
ctx := context.Background()
adapter := GetReportWriterAdapter(cmd)
adapter.SetDependencies(ScanDependencies(ctx))
WriteReport(adapter)
Notef("\nReport generated")
NoteEnd()
}
Loading

0 comments on commit c27d0e0

Please sign in to comment.