Skip to content

Commit

Permalink
Merge branch 'main' into fix/version-command
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Rodriguez committed Jun 7, 2024
2 parents 36a48f5 + 42cbd4e commit fa64a5f
Show file tree
Hide file tree
Showing 35 changed files with 144 additions and 121 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/defenseunicorns/zarf

go 1.21.8
go 1.22.4

// TODO (@AABRO): Pending merge into github.com/gojsonschema/gojsonschema (https://github.com/gojsonschema/gojsonschema/pull/5)
replace github.com/xeipuuv/gojsonschema => github.com/defenseunicorns/gojsonschema v0.0.0-20231116163348-e00f069122d6
Expand Down
17 changes: 16 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"context"
"embed"
"os"
"os/signal"
"syscall"

Expand All @@ -22,8 +23,22 @@ var cosignPublicKey string
var zarfSchema embed.FS

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
first := true
for {
<-signalCh
if first {
first = false
cancel()
continue
}
os.Exit(1)
}
}()

config.CosignPublicKey = cosignPublicKey
lint.ZarfSchema = zarfSchema
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ var devFindImagesCmd = &cobra.Command{
Args: cobra.MaximumNArgs(1),
Short: lang.CmdDevFindImagesShort,
Long: lang.CmdDevFindImagesLong,
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.CreateOpts.BaseDir = common.SetBaseDirectory(args)

v := common.GetViper()
Expand All @@ -216,7 +216,7 @@ var devFindImagesCmd = &cobra.Command{
pkgClient := packager.NewOrDie(&pkgConfig)
defer pkgClient.ClearTempPaths()

if _, err := pkgClient.FindImages(); err != nil {
if _, err := pkgClient.FindImages(cmd.Context()); err != nil {
message.Fatalf(err, lang.CmdDevFindImagesErr, err.Error())
}
},
Expand Down Expand Up @@ -249,12 +249,12 @@ var devLintCmd = &cobra.Command{
Aliases: []string{"l"},
Short: lang.CmdDevLintShort,
Long: lang.CmdDevLintLong,
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.CreateOpts.BaseDir = common.SetBaseDirectory(args)
v := common.GetViper()
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)
validator, err := lint.Validate(pkgConfig.CreateOpts)
validator, err := lint.Validate(cmd.Context(), pkgConfig.CreateOpts)
if err != nil {
message.Fatal(err, err.Error())
}
Expand Down
11 changes: 6 additions & 5 deletions src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -49,7 +50,7 @@ var initCmd = &cobra.Command{

// Try to use an init-package in the executable directory if none exist in current working directory
var err error
if pkgConfig.PkgOpts.PackageSource, err = findInitPackage(initPackageName); err != nil {
if pkgConfig.PkgOpts.PackageSource, err = findInitPackage(cmd.Context(), initPackageName); err != nil {
message.Fatal(err, err.Error())
}

Expand All @@ -74,7 +75,7 @@ var initCmd = &cobra.Command{
},
}

func findInitPackage(initPackageName string) (string, error) {
func findInitPackage(ctx context.Context, initPackageName string) (string, error) {
// First, look for the init package in the current working directory
if !helpers.InvalidPath(initPackageName) {
return initPackageName, nil
Expand Down Expand Up @@ -103,7 +104,7 @@ func findInitPackage(initPackageName string) (string, error) {
}

// Finally, if the init-package doesn't exist in the cache directory, suggest downloading it
downloadCacheTarget, err := downloadInitPackage(config.GetAbsCachePath())
downloadCacheTarget, err := downloadInitPackage(ctx, config.GetAbsCachePath())
if err != nil {
if errors.Is(err, lang.ErrInitNotFound) {
message.Fatal(err, err.Error())
Expand All @@ -114,7 +115,7 @@ func findInitPackage(initPackageName string) (string, error) {
return downloadCacheTarget, nil
}

func downloadInitPackage(cacheDirectory string) (string, error) {
func downloadInitPackage(ctx context.Context, cacheDirectory string) (string, error) {
if config.CommonOptions.Confirm {
return "", lang.ErrInitNotFound
}
Expand Down Expand Up @@ -144,7 +145,7 @@ func downloadInitPackage(cacheDirectory string) (string, error) {
return "", err
}
source := &sources.OCISource{Remote: remote}
return source.Collect(cacheDirectory)
return source.Collect(ctx, cacheDirectory)
}
// Otherwise, exit and tell the user to manually download the init-package
return "", errors.New(lang.CmdInitPullErrManual)
Expand Down
16 changes: 8 additions & 8 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var packageCreateCmd = &cobra.Command{
Args: cobra.MaximumNArgs(1),
Short: lang.CmdPackageCreateShort,
Long: lang.CmdPackageCreateLong,
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.CreateOpts.BaseDir = common.SetBaseDirectory(args)

var isCleanPathRegex = regexp.MustCompile(`^[a-zA-Z0-9\_\-\/\.\~\\:]+$`)
Expand All @@ -55,7 +55,7 @@ var packageCreateCmd = &cobra.Command{
pkgClient := packager.NewOrDie(&pkgConfig)
defer pkgClient.ClearTempPaths()

if err := pkgClient.Create(); err != nil {
if err := pkgClient.Create(cmd.Context()); err != nil {
message.Fatalf(err, lang.CmdPackageCreateErr, err.Error())
}
},
Expand Down Expand Up @@ -112,15 +112,15 @@ var packageInspectCmd = &cobra.Command{
Short: lang.CmdPackageInspectShort,
Long: lang.CmdPackageInspectLong,
Args: cobra.MaximumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.PkgOpts.PackageSource = choosePackage(args)

src := identifyAndFallbackToClusterSource()

pkgClient := packager.NewOrDie(&pkgConfig, packager.WithSource(src))
defer pkgClient.ClearTempPaths()

if err := pkgClient.Inspect(); err != nil {
if err := pkgClient.Inspect(cmd.Context()); err != nil {
message.Fatalf(err, lang.CmdPackageInspectErr, err.Error())
}
},
Expand Down Expand Up @@ -190,7 +190,7 @@ var packagePublishCmd = &cobra.Command{
Short: lang.CmdPackagePublishShort,
Example: lang.CmdPackagePublishExample,
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.PkgOpts.PackageSource = args[0]

if !helpers.IsOCIURL(args[1]) {
Expand All @@ -216,7 +216,7 @@ var packagePublishCmd = &cobra.Command{
pkgClient := packager.NewOrDie(&pkgConfig)
defer pkgClient.ClearTempPaths()

if err := pkgClient.Publish(); err != nil {
if err := pkgClient.Publish(cmd.Context()); err != nil {
message.Fatalf(err, lang.CmdPackagePublishErr, err.Error())
}
},
Expand All @@ -227,13 +227,13 @@ var packagePullCmd = &cobra.Command{
Short: lang.CmdPackagePullShort,
Example: lang.CmdPackagePullExample,
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.PkgOpts.PackageSource = args[0]

pkgClient := packager.NewOrDie(&pkgConfig)
defer pkgClient.ClearTempPaths()

if err := pkgClient.Pull(); err != nil {
if err := pkgClient.Pull(cmd.Context()); err != nil {
message.Fatalf(err, lang.CmdPackagePullErr, err.Error())
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/tools/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ var clearCacheCmd = &cobra.Command{
var downloadInitCmd = &cobra.Command{
Use: "download-init",
Short: lang.CmdToolsDownloadInitShort,
Run: func(_ *cobra.Command, _ []string) {
Run: func(cmd *cobra.Command, _ []string) {
url := zoci.GetInitPackageURL(config.CLIVersion)

remote, err := zoci.NewRemote(url, oci.PlatformForArch(config.GetArch()))
Expand All @@ -196,7 +196,7 @@ var downloadInitCmd = &cobra.Command{

source := &sources.OCISource{Remote: remote}

_, err = source.Collect(outputDirectory)
_, err = source.Collect(cmd.Context(), outputDirectory)
if err != nil {
message.Fatalf(err, lang.CmdToolsDownloadInitErr, err.Error())
}
Expand Down
10 changes: 5 additions & 5 deletions src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (ic *ImportChain) append(c types.ZarfComponent, index int, originalPackageN

// NewImportChain creates a new import chain from a component
// Returning the chain on error so we can have additional information to use during lint
func NewImportChain(head types.ZarfComponent, index int, originalPackageName, arch, flavor string) (*ImportChain, error) {
func NewImportChain(ctx context.Context, head types.ZarfComponent, index int, originalPackageName, arch, flavor string) (*ImportChain, error) {
ic := &ImportChain{}
if arch == "" {
return ic, fmt.Errorf("cannot build import chain: architecture must be provided")
Expand Down Expand Up @@ -179,11 +179,11 @@ func NewImportChain(head types.ZarfComponent, index int, originalPackageName, ar
}
} else if isRemote {
importURL = node.Import.URL
remote, err := ic.getRemote(node.Import.URL)
remote, err := ic.getRemote(ctx, node.Import.URL)
if err != nil {
return ic, err
}
pkg, err = remote.FetchZarfYAML(context.TODO())
pkg, err = remote.FetchZarfYAML(ctx)
if err != nil {
return ic, err
}
Expand Down Expand Up @@ -274,15 +274,15 @@ func (ic *ImportChain) Migrate(build types.ZarfBuildData) (warnings []string) {

// Compose merges the import chain into a single component
// fixing paths, overriding metadata, etc
func (ic *ImportChain) Compose() (composed *types.ZarfComponent, err error) {
func (ic *ImportChain) Compose(ctx context.Context) (composed *types.ZarfComponent, err error) {
composed = &ic.tail.ZarfComponent

if ic.tail.prev == nil {
// only had one component in the import chain
return composed, nil
}

if err := ic.fetchOCISkeleton(); err != nil {
if err := ic.fetchOCISkeleton(ctx); err != nil {
return nil, err
}

Expand Down
5 changes: 3 additions & 2 deletions src/pkg/packager/composer/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package composer

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestNewImportChain(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

_, err := NewImportChain(tt.head, 0, testPackageName, tt.arch, tt.flavor)
_, err := NewImportChain(context.Background(), tt.head, 0, testPackageName, tt.arch, tt.flavor)
require.ErrorContains(t, err, tt.expectedErr)
})
}
Expand Down Expand Up @@ -239,7 +240,7 @@ func TestCompose(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

composed, err := tt.ic.Compose()
composed, err := tt.ic.Compose(context.Background())
require.NoError(t, err)
require.EqualValues(t, &tt.expectedComposed, composed)
})
Expand Down
10 changes: 4 additions & 6 deletions src/pkg/packager/composer/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
ocistore "oras.land/oras-go/v2/content/oci"
)

func (ic *ImportChain) getRemote(url string) (*zoci.Remote, error) {
func (ic *ImportChain) getRemote(ctx context.Context, url string) (*zoci.Remote, error) {
if ic.remote != nil {
return ic.remote, nil
}
Expand All @@ -32,7 +32,7 @@ func (ic *ImportChain) getRemote(url string) (*zoci.Remote, error) {
if err != nil {
return nil, err
}
_, err = ic.remote.ResolveRoot(context.TODO())
_, err = ic.remote.ResolveRoot(ctx)
if err != nil {
return nil, fmt.Errorf("published skeleton package for %q does not exist: %w", url, err)
}
Expand All @@ -45,17 +45,16 @@ func (ic *ImportChain) ContainsOCIImport() bool {
return ic.tail.prev != nil && ic.tail.prev.Import.URL != ""
}

func (ic *ImportChain) fetchOCISkeleton() error {
func (ic *ImportChain) fetchOCISkeleton(ctx context.Context) error {
if !ic.ContainsOCIImport() {
return nil
}
node := ic.tail.prev
remote, err := ic.getRemote(node.Import.URL)
remote, err := ic.getRemote(ctx, node.Import.URL)
if err != nil {
return err
}

ctx := context.TODO()
manifest, err := remote.FetchRoot(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -91,7 +90,6 @@ func (ic *ImportChain) fetchOCISkeleton() error {
return err
}

ctx := context.TODO()
// ensure the tarball is in the cache
exists, err := store.Exists(ctx, componentDesc)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package packager

import (
"context"
"fmt"
"os"

Expand All @@ -16,7 +17,7 @@ import (
)

// Create generates a Zarf package tarball for a given PackageConfig and optional base directory.
func (p *Packager) Create() (err error) {
func (p *Packager) Create(ctx context.Context) (err error) {
cwd, err := os.Getwd()
if err != nil {
return err
Expand All @@ -34,7 +35,7 @@ func (p *Packager) Create() (err error) {
return err
}

p.cfg.Pkg, p.warnings, err = pc.LoadPackageDefinition(p.layout)
p.cfg.Pkg, p.warnings, err = pc.LoadPackageDefinition(ctx, p.layout)
if err != nil {
return err
}
Expand All @@ -43,7 +44,7 @@ func (p *Packager) Create() (err error) {
return fmt.Errorf("package creation canceled")
}

if err := pc.Assemble(p.layout, p.cfg.Pkg.Components, p.cfg.Pkg.Metadata.Architecture); err != nil {
if err := pc.Assemble(ctx, p.layout, p.cfg.Pkg.Components, p.cfg.Pkg.Metadata.Architecture); err != nil {
return err
}

Expand All @@ -52,5 +53,5 @@ func (p *Packager) Create() (err error) {
return err
}

return pc.Output(p.layout, &p.cfg.Pkg)
return pc.Output(ctx, p.layout, &p.cfg.Pkg)
}
Loading

0 comments on commit fa64a5f

Please sign in to comment.