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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.so
*.dylib
/trivy
/trivy-debug

## chart release
.cr-release-packages
Expand Down
9 changes: 8 additions & 1 deletion docs/community/contribute/pr.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ Install the necessary tools for development by following their respective instal
After making changes to the Go source code, build the project with the following command:

```shell
$ mage build
$ mage build:prod
$ ./trivy -h
```

Or if you want to use [delve](https://github.com/go-delve/delve) for debugging, build with the following command:

```shell
$ mage build:dev
$ dlv exec ./trivy-debug -h
```

### Lint
You must pass the linter checks:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ This allows you to verify that your vulnerabilities are correctly stored in the

```bash
# Build Trivy with your changes
mage build
mage build:prod

# Use your local database
./trivy image --skip-db-update --cache-dir /path/to/cache your-test-image
Expand Down
43 changes: 36 additions & 7 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,20 @@ func version() (string, error) {
return strings.TrimPrefix(ver, "v"), nil
}

func buildLdflags() (string, error) {
func buildLdflags(debug bool) (string, error) {
ver, err := version()
if err != nil {
return "", err
}
return fmt.Sprintf("-s -w -X=github.com/aquasecurity/trivy/pkg/version/app.ver=%s", ver), nil
var build string

if debug {
build = "-X=github.com/aquasecurity/trivy/pkg/version/app.ver=%s"
} else {
build = "-s -w -X=github.com/aquasecurity/trivy/pkg/version/app.ver=%s"
}

return fmt.Sprintf(build, ver), nil
}

type Tool mg.Namespace
Expand Down Expand Up @@ -374,28 +382,49 @@ func Tidy() error {
return sh.RunV("go", "mod", "tidy")
}

// Build builds Trivy
func Build() error {
type Build mg.Namespace

// Prod build Trivy without debug symbols
func (b Build) Prod() error {
fmt.Println("Building Trivy production binary")
return build(false)
}

// Dev build Trivy with debug symbols
func (b Build) Dev() error {
fmt.Println("Building Trivy debug binary")
return build(true)
}

func build(debug bool) error {
if updated, err := target.Dir("trivy", "pkg", "cmd"); err != nil {
return err
} else if !updated {
return nil
}

ldflags, err := buildLdflags()
execName := "trivy"
gcflags := ""
if debug {
execName = "trivy-debug"
gcflags = "-gcflags=all=-N -l"
}

ldflags, err := buildLdflags(debug)
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
return sh.RunWith(ENV, "go", "build", "-ldflags", ldflags, filepath.Join(wd, "cmd", "trivy"))
return sh.RunWith(ENV, "go", "build", "-o", execName, "-ldflags", ldflags,
gcflags, filepath.Join(wd, "cmd", "trivy"))
}

// Install installs Trivy
func Install() error {
ldflags, err := buildLdflags()
ldflags, err := buildLdflags(false)
if err != nil {
return err
}
Expand Down