Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/cli/cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func Exec(options Options) error {
app.WithIncludePaths(options.IncludePaths),
app.WithIncludeStdlib(options.IncludeStd),
app.WithLicenseDetector(licenseDetector),
app.WithVersionOverride(options.Version),
app.WithMainDir(options.Main),
app.WithShortPURLS(options.ShortPURLs))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/cmd/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Options struct {
IncludePaths bool
Main string
ModuleDir string
Version string
}

func (o *Options) RegisterFlags(fs *flag.FlagSet) {
Expand All @@ -52,6 +53,7 @@ func (o *Options) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(&o.IncludePackages, "packages", false, "Include packages")
fs.BoolVar(&o.IncludePaths, "paths", false, "Include file paths relative to their module root")
fs.StringVar(&o.Main, "main", "", "Path to the application's main package, relative to MODULE_PATH")
fs.StringVar(&o.Version, "version", "", "Version of the application")
}

func (o Options) Validate() error {
Expand Down
11 changes: 8 additions & 3 deletions pkg/generate/app/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type generator struct {
mainDir string
moduleDir string
shortPURLs bool
versionOverride string
}

func NewGenerator(moduleDir string, opts ...Option) (generate.Generator, error) {
Expand Down Expand Up @@ -102,9 +103,13 @@ func (g generator) Generate() (*cdx.BOM, error) {
return nil, fmt.Errorf("failed to apply module graph: %w", err)
}

modules[appModuleIndex].Version, err = gomod.GetModuleVersion(g.logger, modules[appModuleIndex].Dir)
if err != nil {
return nil, fmt.Errorf("failed to determine version of main module: %w", err)
if g.versionOverride != "" {
modules[appModuleIndex].Version = g.versionOverride
} else {
modules[appModuleIndex].Version, err = gomod.GetModuleVersion(g.logger, modules[appModuleIndex].Dir)
if err != nil {
return nil, fmt.Errorf("failed to determine version of main module: %w", err)
}
}

mainComponent, err := modConv.ToComponent(g.logger, modules[appModuleIndex],
Expand Down
12 changes: 12 additions & 0 deletions pkg/generate/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,15 @@ func WithShortPURLS(enable bool) Option {
return nil
}
}

// WithVersionOverride overrides the version of the application.
//
// This is useful in cases where a BOM is generated from a monorepo
// containing multiple applications and generating version info from
// git is not useful or not possible.
func WithVersionOverride(version string) Option {
return func(g *generator) error {
g.versionOverride = version
return nil
}
}
7 changes: 7 additions & 0 deletions pkg/generate/app/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ func TestWithLogger(t *testing.T) {
require.NoError(t, err)
require.Equal(t, logger, g.logger)
}

func TestWithVersionOverride(t *testing.T) {
g := &generator{versionOverride: ""}
err := WithVersionOverride("v1.2.3")(g)
require.NoError(t, err)
require.Equal(t, "v1.2.3", g.versionOverride)
}