Skip to content

Commit a587d03

Browse files
feat(scan): current and released package scan (#65)
* feat(scan): zarf and release scanning with grype * added missing license * added a comment * drop -v for verbose output - it's already used for version * add missing check for errors on mkdirtemp * slog logging * add some colors to logging * dropped zarf package loader deps, switched to a supported yaml lib * minor fixes * drop unnecessary oci url building * minor clean up * added scan and compare * added an option to override image name - if image name changed between the released and the current version * generated some e2e tests * sth working * separate mock files * e2e tests for scan commands * lint fixes * added missing license * explicitly ignore fprintf error * drop unnecessary todos * minor fix * fix comment * switch scanning commands to structs * switch scanning commands to structs * reworks - batch 1 * simplified fetching sboms for flavors logic * more rework * try direct defer calls * try to silence linter when swallowing irrelevant errors * refactored commands - not working fetching * superfluous logging * fixed running * switched to `flags` for flag parsing in grype mock * a bit of rework + fixing lint errors * rearranged the commands to be under `scan` parent command * pulled out exec function to options * drop overcomplicated Close() error swallowing * drop more overcomplicated Close() error swallowing
1 parent 62f3acd commit a587d03

File tree

10 files changed

+1585
-24
lines changed

10 files changed

+1585
-24
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ go.work.sum
2727
# vscode
2828
.vscode
2929

30+
# idea
31+
.idea
32+
3033
# build artifacts
3134
build/
3235
uds-pk

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ require (
88
github.com/go-git/go-git/v5 v5.16.2
99
github.com/goccy/go-yaml v1.18.0
1010
github.com/google/go-github/v69 v69.2.0
11+
github.com/google/go-github/v73 v73.0.0
1112
github.com/mikefarah/yq/v4 v4.47.2
1213
github.com/olekukonko/tablewriter v1.0.9
1314
github.com/spf13/cobra v1.10.1
1415
github.com/stretchr/testify v1.11.1
1516
github.com/zarf-dev/zarf v0.61.2
17+
go.yaml.in/yaml/v4 v4.0.0-rc.2
18+
golang.org/x/oauth2 v0.30.0
1619
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
1720
)
1821

@@ -83,7 +86,6 @@ require (
8386
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
8487
golang.org/x/crypto v0.41.0 // indirect
8588
golang.org/x/net v0.43.0 // indirect
86-
golang.org/x/oauth2 v0.30.0 // indirect
8789
golang.org/x/sys v0.35.0 // indirect
8890
golang.org/x/text v0.28.0 // indirect
8991
golang.org/x/time v0.12.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
125125
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
126126
github.com/google/go-github/v69 v69.2.0 h1:wR+Wi/fN2zdUx9YxSmYE0ktiX9IAR/BeePzeaUUbEHE=
127127
github.com/google/go-github/v69 v69.2.0/go.mod h1:xne4jymxLR6Uj9b7J7PyTpkMYstEMMwGZa0Aehh1azM=
128+
github.com/google/go-github/v73 v73.0.0 h1:aR+Utnh+Y4mMkS+2qLQwcQ/cF9mOTpdwnzlaw//rG24=
129+
github.com/google/go-github/v73 v73.0.0/go.mod h1:fa6w8+/V+edSU0muqdhCVY7Beh1M8F1IlQPZIANKIYw=
128130
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
129131
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
130132
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
@@ -275,6 +277,8 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
275277
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
276278
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
277279
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
280+
go.yaml.in/yaml/v4 v4.0.0-rc.2 h1:/FrI8D64VSr4HtGIlUtlFMGsm7H7pWTbj6vOLVZcA6s=
281+
go.yaml.in/yaml/v4 v4.0.0-rc.2/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
278282
go4.org v0.0.0-20230225012048-214862532bf5 h1:nifaUDeh+rPaBCMPMQHZmvJf+QdpLFnuQPwx+LxVmtc=
279283
go4.org v0.0.0-20230225012048-214862532bf5/go.mod h1:F57wTi5Lrj6WLyswp5EYV1ncrEbFGHD4hhz6S1ZYeaU=
280284
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

src/cmd/root.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
9+
"log/slog"
810
"os"
911

12+
"github.com/defenseunicorns/uds-pk/src/utils"
13+
1014
"github.com/spf13/cobra"
1115
)
1216

@@ -15,8 +19,8 @@ var rootCmd = &cobra.Command{
1519
Use: "uds-pk",
1620
Short: "UDS Package Kit is a tool for managing UDS packages",
1721
Long: `UDS Package Kit is a tool that facilitates the development, maintenance and release
18-
of UDS packages. It provides commands for automating releases verifying packages and
19-
generating configuration.`,
22+
of UDS packages. It provides commands for automating releases verifying packages and
23+
generating configuration.`,
2024
}
2125

2226
// deprecatedCheckCmd is the deprecated location for the check command
@@ -52,6 +56,48 @@ var deprecatedUpdateYamlCmd = &cobra.Command{
5256
},
5357
}
5458

59+
type contextKey string
60+
61+
const loggerKey contextKey = "logger"
62+
const verboseKey contextKey = "verbose"
63+
64+
func initLogger(cmd *cobra.Command, _ []string) {
65+
verbose, err := cmd.Root().PersistentFlags().GetBool("verbose")
66+
if err != nil {
67+
verbose = false
68+
}
69+
ctx := InitLoggerContext(verbose, cmd.Context())
70+
cmd.SetContext(ctx)
71+
}
72+
73+
func InitLoggerContext(verbose bool, ctx context.Context) context.Context {
74+
logger := CreateLogger(verbose)
75+
ctx = context.WithValue(ctx, loggerKey, logger)
76+
return context.WithValue(ctx, verboseKey, verbose)
77+
}
78+
79+
func CreateLogger(verbose bool) *slog.Logger {
80+
level := slog.LevelInfo
81+
if verbose {
82+
level = slog.LevelDebug
83+
}
84+
return slog.New(utils.PrettyLogHandler(os.Stderr, level))
85+
}
86+
87+
func Logger(ctx *context.Context) *slog.Logger {
88+
if ctx == nil {
89+
return CreateLogger(false)
90+
}
91+
return (*ctx).Value(loggerKey).(*slog.Logger)
92+
}
93+
94+
func Verbose(ctx *context.Context) bool {
95+
if ctx == nil {
96+
return false
97+
}
98+
return (*ctx).Value(verboseKey).(bool)
99+
}
100+
55101
// Execute adds all child commands to the root command and sets flags appropriately.
56102
// This is called by main.main(). It only needs to happen once to the rootCmd.
57103
func Execute() {
@@ -62,6 +108,8 @@ func Execute() {
62108
}
63109

64110
func init() {
111+
rootCmd.PersistentFlags().Bool("verbose", false, "Enable debug output")
112+
rootCmd.PersistentPreRun = initLogger
65113
rootCmd.AddCommand(deprecatedCheckCmd)
66114
rootCmd.AddCommand(deprecatedShowCmd)
67115
rootCmd.AddCommand(deprecatedUpdateYamlCmd)

0 commit comments

Comments
 (0)