Skip to content

Commit

Permalink
misc: cosmetic tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Sep 6, 2021
1 parent 5221f2a commit b04354b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 38 deletions.
4 changes: 4 additions & 0 deletions e2e/cmd_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAppCmdSimple(t *testing.T) {
SerialNumber: zeroUUID.String(),
},
ModuleDir: fixturePath,
Main: "main.go",
}

runSnapshotIT(t, &appOptions.OutputOptions, func() error { return appcmd.Exec(appOptions) })
Expand All @@ -49,6 +50,7 @@ func TestAppCmdSimpleWithFiles(t *testing.T) {
SerialNumber: zeroUUID.String(),
},
ModuleDir: fixturePath,
Main: "main.go",
IncludeFiles: true,
}

Expand Down Expand Up @@ -97,6 +99,7 @@ func TestAppCmdVendored(t *testing.T) {
SerialNumber: zeroUUID.String(),
},
ModuleDir: fixturePath,
Main: "main.go",
}

runSnapshotIT(t, &appOptions.OutputOptions, func() error { return appcmd.Exec(appOptions) })
Expand All @@ -112,6 +115,7 @@ func TestAppCmdVendoredWithFiles(t *testing.T) {
SerialNumber: zeroUUID.String(),
},
ModuleDir: fixturePath,
Main: "main.go",
IncludeFiles: true,
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/cmd_bin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func TestBinCmdSimple(t *testing.T) {
binOptions := bincmd.BinOptions{
binOptions := bincmd.Options{
SBOMOptions: options.SBOMOptions{
Reproducible: true,
ResolveLicenses: true,
Expand Down
10 changes: 5 additions & 5 deletions e2e/cmd_mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
func TestModCmdSimple(t *testing.T) {
fixturePath := extractFixture(t, "./testdata/modcmd/simple.tar.gz")

modOptions := modcmd.ModOptions{
modOptions := modcmd.Options{
SBOMOptions: options.SBOMOptions{
Reproducible: true,
ResolveLicenses: true,
Expand All @@ -49,7 +49,7 @@ func TestModCmdSimple(t *testing.T) {
func TestModCmdLocal(t *testing.T) {
fixturePath := extractFixture(t, "./testdata/modcmd/local.tar.gz")

modOptions := modcmd.ModOptions{
modOptions := modcmd.Options{
SBOMOptions: options.SBOMOptions{
Reproducible: true,
ResolveLicenses: true,
Expand All @@ -66,7 +66,7 @@ func TestModCmdLocal(t *testing.T) {
func TestModCmdNoDependencies(t *testing.T) {
fixturePath := extractFixture(t, "./testdata/modcmd/no-dependencies.tar.gz")

modOptions := modcmd.ModOptions{
modOptions := modcmd.Options{
SBOMOptions: options.SBOMOptions{
Reproducible: true,
ResolveLicenses: true,
Expand All @@ -84,7 +84,7 @@ func TestModCmdNoDependencies(t *testing.T) {
func TestModCmdVendored(t *testing.T) {
fixturePath := extractFixture(t, "./testdata/modcmd/vendored.tar.gz")

modOptions := modcmd.ModOptions{
modOptions := modcmd.Options{
SBOMOptions: options.SBOMOptions{
Reproducible: true,
ResolveLicenses: true,
Expand All @@ -110,7 +110,7 @@ func TestModCmdVendored(t *testing.T) {
func TestModCmdNested(t *testing.T) {
fixturePath := extractFixture(t, "./testdata/modcmd/nested.tar.gz")

modOptions := modcmd.ModOptions{
modOptions := modcmd.Options{
SBOMOptions: options.SBOMOptions{
Reproducible: true,
ResolveLicenses: true,
Expand Down
8 changes: 3 additions & 5 deletions internal/cli/cmd/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ func (o Options) Validate() error {
}
}

if o.Main != "" {
err := o.validateMain(o.Main, &errs)
if err != nil {
return err
}
err := o.validateMain(o.Main, &errs)
if err != nil {
return err
}

if len(errs) > 0 {
Expand Down
28 changes: 14 additions & 14 deletions internal/cli/cmd/bin/bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
func New() *ffcli.Command {
fs := flag.NewFlagSet("cyclonedx-gomod bin", flag.ExitOnError)

var options BinOptions
var options Options
options.RegisterFlags(fs)

return &ffcli.Command{
Expand Down Expand Up @@ -70,24 +70,24 @@ Example:
}
}

func Exec(binOptions BinOptions) error {
err := binOptions.Validate()
func Exec(options Options) error {
err := options.Validate()
if err != nil {
return err
}

modules, hashes, err := gomod.GetModulesFromBinary(binOptions.BinaryPath)
modules, hashes, err := gomod.GetModulesFromBinary(options.BinaryPath)
if err != nil {
return fmt.Errorf("failed to extract modules: %w", err)
} else if len(modules) == 0 {
return fmt.Errorf("couldn't parse any modules from %s", binOptions.BinaryPath)
return fmt.Errorf("couldn't parse any modules from %s", options.BinaryPath)
}

if binOptions.Version != "" {
modules[0].Version = binOptions.Version
if options.Version != "" {
modules[0].Version = options.Version
}

if binOptions.ResolveLicenses {
if options.ResolveLicenses {
err = downloadModules(modules, hashes)
if err != nil {
return err
Expand All @@ -101,14 +101,14 @@ func Exec(binOptions BinOptions) error {

mainComponent, err := modconv.ToComponent(modules[0],
modconv.WithComponentType(cdx.ComponentTypeApplication),
modconv.WithLicenses(binOptions.ResolveLicenses),
modconv.WithLicenses(options.ResolveLicenses),
)
if err != nil {
return err
}

components, err := modconv.ToComponents(modules[1:],
modconv.WithLicenses(binOptions.ResolveLicenses),
modconv.WithLicenses(options.ResolveLicenses),
withModuleHashes(hashes),
)
if err != nil {
Expand All @@ -117,14 +117,14 @@ func Exec(binOptions BinOptions) error {

dependencyGraph := sbom.BuildDependencyGraph(modules)

binaryProperties, err := createBinaryProperties(binOptions.BinaryPath)
binaryProperties, err := createBinaryProperties(options.BinaryPath)
if err != nil {
return err
}

bom := cdx.NewBOM()

err = cliutil.SetSerialNumber(bom, binOptions.SBOMOptions)
err = cliutil.SetSerialNumber(bom, options.SBOMOptions)
if err != nil {
return err
}
Expand All @@ -133,7 +133,7 @@ func Exec(binOptions BinOptions) error {
Component: mainComponent,
Properties: &binaryProperties,
}
err = cliutil.AddCommonMetadata(bom, binOptions.SBOMOptions)
err = cliutil.AddCommonMetadata(bom, options.SBOMOptions)
if err != nil {
return err
}
Expand All @@ -142,7 +142,7 @@ func Exec(binOptions BinOptions) error {
bom.Dependencies = &dependencyGraph
bom.Compositions = createCompositions(*mainComponent, components)

return cliutil.WriteBOM(bom, binOptions.OutputOptions)
return cliutil.WriteBOM(bom, options.OutputOptions)
}

func withModuleHashes(hashes map[string]string) modconv.Option {
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/cmd/bin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/CycloneDX/cyclonedx-gomod/internal/cli/options"
)

type BinOptions struct {
type Options struct {
options.LogOptions
options.OutputOptions
options.SBOMOptions
Expand All @@ -35,15 +35,15 @@ type BinOptions struct {
Version string
}

func (b *BinOptions) RegisterFlags(fs *flag.FlagSet) {
func (b *Options) RegisterFlags(fs *flag.FlagSet) {
b.LogOptions.RegisterFlags(fs)
b.OutputOptions.RegisterFlags(fs)
b.SBOMOptions.RegisterFlags(fs)

fs.StringVar(&b.Version, "version", "", "Version of the main component")
}

func (b BinOptions) Validate() error {
func (b Options) Validate() error {
errs := make([]error, 0)

if err := b.OutputOptions.Validate(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/cmd/bin/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

func TestBinOptions_Validate(t *testing.T) {
t.Run("BinaryPath Not Exists", func(t *testing.T) {
var binOptions BinOptions
var binOptions Options
binOptions.BinaryPath = "./doesNotExist"

err := binOptions.Validate()
Expand All @@ -34,7 +34,7 @@ func TestBinOptions_Validate(t *testing.T) {
})

t.Run("BinaryPath Is Dir", func(t *testing.T) {
var binOptions BinOptions
var binOptions Options
binOptions.BinaryPath = "./"

err := binOptions.Validate()
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/cmd/mod/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
func New() *ffcli.Command {
fs := flag.NewFlagSet("cyclonedx-gomod mod", flag.ExitOnError)

var options ModOptions
var options Options
options.RegisterFlags(fs)

return &ffcli.Command{
Expand Down Expand Up @@ -67,7 +67,7 @@ Examples:
}
}

func Exec(options ModOptions) error {
func Exec(options Options) error {
err := options.Validate()
if err != nil {
return err
Expand Down
7 changes: 3 additions & 4 deletions internal/cli/cmd/mod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import (
"github.com/CycloneDX/cyclonedx-gomod/internal/cli/options"
)

// ModOptions provides options for the `mod` command.
type ModOptions struct {
type Options struct {
options.LogOptions
options.OutputOptions
options.SBOMOptions
Expand All @@ -38,7 +37,7 @@ type ModOptions struct {
IncludeTest bool
}

func (m *ModOptions) RegisterFlags(fs *flag.FlagSet) {
func (m *Options) RegisterFlags(fs *flag.FlagSet) {
m.LogOptions.RegisterFlags(fs)
m.OutputOptions.RegisterFlags(fs)
m.SBOMOptions.RegisterFlags(fs)
Expand All @@ -54,7 +53,7 @@ var allowedComponentTypes = []cdx.ComponentType{
cdx.ComponentTypeLibrary,
}

func (m ModOptions) Validate() error {
func (m Options) Validate() error {
errs := make([]error, 0)

if err := m.OutputOptions.Validate(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/mod/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func TestModOptions_Validate(t *testing.T) {
t.Run("InvalidComponentType", func(t *testing.T) {
var modOptions ModOptions
var modOptions Options
modOptions.ComponentType = "foobar"

err := modOptions.Validate()
Expand Down
2 changes: 1 addition & 1 deletion internal/gocmd/gocmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func ModWhy(moduleDir string, modules []string, writer io.Writer) error {
)
}

// ModWhy executes `go version -m` and writes the output to a given writer.
// GetModulesFromBinary executes `go version -m` and writes the output to a given writer.
func GetModulesFromBinary(binaryPath string, writer io.Writer) error {
return executeGoCommand([]string{"version", "-m", binaryPath}, withStdout(writer))
}
Expand Down

0 comments on commit b04354b

Please sign in to comment.