Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split helpers/exec libs #2379

Merged
merged 7 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/3-create-a-zarf-package/4-zarf-schema.md
Expand Up @@ -1848,7 +1848,7 @@ Must be one of:
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActionShell |
| **Defined in** | #/definitions/Shell |

<details>
<summary>
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/common/setup.go
Expand Up @@ -10,15 +10,14 @@ import (
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
)

// LogLevelCLI holds the log level as input from a command
var LogLevelCLI string

// SetupCLI sets up the CLI logging, interrupt functions, and more
func SetupCLI() {
exec.ExitOnInterrupt()
ExitOnInterrupt()

match := map[string]message.LogLevel{
"warn": message.WarnLevel,
Expand Down
24 changes: 24 additions & 0 deletions src/cmd/common/utils.go
Expand Up @@ -4,10 +4,34 @@
// Package common handles command configuration across all commands
package common

import (
"os"
"os/signal"
"syscall"

"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
)

// SuppressGlobalInterrupt suppresses the global error on an interrupt
var SuppressGlobalInterrupt = false

// SetBaseDirectory sets the base directory. This is a directory with a zarf.yaml.
func SetBaseDirectory(args []string) string {
if len(args) > 0 {
return args[0]
}
return "."
}

// ExitOnInterrupt catches an interrupt and exits with fatal error
func ExitOnInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if !SuppressGlobalInterrupt {
message.Fatal(lang.ErrInterrupt, lang.ErrInterrupt.Error())
}
}()
}
3 changes: 2 additions & 1 deletion src/cmd/connect.go
Expand Up @@ -10,6 +10,7 @@ import (
"os/signal"
"syscall"

"github.com/defenseunicorns/zarf/src/cmd/common"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/k8s"
Expand Down Expand Up @@ -72,7 +73,7 @@ var (
// Keep this open until an interrupt signal is received.
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM)
exec.SuppressGlobalInterrupt = true
common.SuppressGlobalInterrupt = true

// Wait for the interrupt signal or an error.
select {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/destroy.go
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/defenseunicorns/zarf/src/internal/packager/helm"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -52,7 +52,7 @@ var destroyCmd = &cobra.Command{

// Run all the scripts!
pattern := regexp.MustCompile(`(?mi)zarf-clean-.+\.sh$`)
scripts, _ := utils.RecursiveFileList(config.ZarfCleanupScriptsPath, pattern, true)
scripts, _ := helpers.RecursiveFileList(config.ZarfCleanupScriptsPath, pattern, true)
// Iterate over all matching zarf-clean scripts and exec them
for _, script := range scripts {
// Run the matched script
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/initialize.go
Expand Up @@ -77,7 +77,7 @@ var initCmd = &cobra.Command{

func findInitPackage(initPackageName string) (string, error) {
// First, look for the init package in the current working directory
if !utils.InvalidPath(initPackageName) {
if !helpers.InvalidPath(initPackageName) {
return initPackageName, nil
}

Expand All @@ -87,19 +87,19 @@ func findInitPackage(initPackageName string) (string, error) {
return "", err
}
executableDir := path.Dir(binaryPath)
if !utils.InvalidPath(filepath.Join(executableDir, initPackageName)) {
if !helpers.InvalidPath(filepath.Join(executableDir, initPackageName)) {
return filepath.Join(executableDir, initPackageName), nil
}

// Create the cache directory if it doesn't exist
if utils.InvalidPath(config.GetAbsCachePath()) {
if err := utils.CreateDirectory(config.GetAbsCachePath(), helpers.ReadExecuteAllWriteUser); err != nil {
if helpers.InvalidPath(config.GetAbsCachePath()) {
if err := helpers.CreateDirectory(config.GetAbsCachePath(), helpers.ReadExecuteAllWriteUser); err != nil {
message.Fatalf(err, lang.CmdInitErrUnableCreateCache, config.GetAbsCachePath())
}
}

// Next, look in the cache directory
if !utils.InvalidPath(filepath.Join(config.GetAbsCachePath(), initPackageName)) {
if !helpers.InvalidPath(filepath.Join(config.GetAbsCachePath(), initPackageName)) {
return filepath.Join(config.GetAbsCachePath(), initPackageName), nil
}

Expand Down
3 changes: 1 addition & 2 deletions src/cmd/package.go
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/types"

"oras.land/oras-go/v2/registry"
Expand Down Expand Up @@ -214,7 +213,7 @@ var packagePublishCmd = &cobra.Command{
message.Fatalf(nil, "%s", err.Error())
}

if utils.IsDir(pkgConfig.PkgOpts.PackageSource) {
if helpers.IsDir(pkgConfig.PkgOpts.PackageSource) {
pkgConfig.CreateOpts.BaseDir = pkgConfig.PkgOpts.PackageSource
pkgConfig.CreateOpts.IsSkeleton = true
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/tools/crane.go
Expand Up @@ -10,12 +10,12 @@ import (
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/src/cmd/common"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/defenseunicorns/zarf/src/types"
craneCmd "github.com/google/go-containerregistry/cmd/crane/cmd"
"github.com/google/go-containerregistry/pkg/crane"
Expand All @@ -39,7 +39,7 @@ func init() {
Short: lang.CmdToolsRegistryShort,
PersistentPreRun: func(cmd *cobra.Command, _ []string) {

exec.ExitOnInterrupt()
common.ExitOnInterrupt()

// The crane options loading here comes from the rootCmd of crane
craneOptions = append(craneOptions, crane.WithContext(cmd.Context()))
Expand Down
4 changes: 2 additions & 2 deletions src/extensions/bigbang/bigbang.go
Expand Up @@ -262,7 +262,7 @@ func Skeletonize(tmpPaths *layout.ComponentPaths, c types.ZarfComponent) (types.
rel := filepath.Join(layout.TempDir, skelName)
dst := filepath.Join(tmpPaths.Base, rel)

if err := utils.CreatePathAndCopy(valuesFile, dst); err != nil {
if err := helpers.CreatePathAndCopy(valuesFile, dst); err != nil {
return c, err
}

Expand All @@ -282,7 +282,7 @@ func Skeletonize(tmpPaths *layout.ComponentPaths, c types.ZarfComponent) (types.
rel := filepath.Join(layout.TempDir, skelName)
dst := filepath.Join(tmpPaths.Base, rel)

if err := utils.CreatePathAndCopy(fluxPatchFile, dst); err != nil {
if err := helpers.CreatePathAndCopy(fluxPatchFile, dst); err != nil {
return c, err
}

Expand Down
6 changes: 3 additions & 3 deletions src/internal/packager/helm/repo.go
Expand Up @@ -93,7 +93,7 @@ func (h *Helm) PackageChartFromLocalFiles(cosignKeyPath string) error {
saved, err = client.Run(h.chart.LocalPath, nil)
} else {
saved = filepath.Join(temp, filepath.Base(h.chart.LocalPath))
err = utils.CreatePathAndCopy(h.chart.LocalPath, saved)
err = helpers.CreatePathAndCopy(h.chart.LocalPath, saved)
}
defer os.RemoveAll(temp)

Expand Down Expand Up @@ -204,7 +204,7 @@ func (h *Helm) DownloadPublishedChart(cosignKeyPath string) error {

// Download the file into a temp directory since we don't control what name helm creates here
temp := filepath.Join(h.chartPath, "temp")
if err = utils.CreateDirectory(temp, helpers.ReadWriteExecuteUser); err != nil {
if err = helpers.CreateDirectory(temp, helpers.ReadWriteExecuteUser); err != nil {
return fmt.Errorf("unable to create helm chart temp directory: %w", err)
}
defer os.RemoveAll(temp)
Expand Down Expand Up @@ -269,7 +269,7 @@ func (h *Helm) packageValues(cosignKeyPath string) error {
return fmt.Errorf(lang.ErrDownloading, path, err.Error())
}
} else {
if err := utils.CreatePathAndCopy(path, dst); err != nil {
if err := helpers.CreatePathAndCopy(path, dst); err != nil {
return fmt.Errorf("unable to copy chart values file %s: %w", path, err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/internal/packager/images/pull.go
Expand Up @@ -123,7 +123,7 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {
}

// Create the ImagePath directory
if err := utils.CreateDirectory(i.ImagesPath, helpers.ReadExecuteAllWriteUser); err != nil {
if err := helpers.CreateDirectory(i.ImagesPath, helpers.ReadExecuteAllWriteUser); err != nil {
return nil, fmt.Errorf("failed to create image path %s: %w", i.ImagesPath, err)
}

Expand Down Expand Up @@ -233,7 +233,7 @@ func (i *ImageConfig) PullAll() ([]ImgInfo, error) {

// Create the directory for the blob if it doesn't exist
dir := filepath.Join(string(cranePath), "blobs", digest.Algorithm)
if err := utils.CreateDirectory(dir, os.ModePerm); err != nil {
if err := helpers.CreateDirectory(dir, os.ModePerm); err != nil {
layerWritingConcurrency.ErrorChan <- err
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/internal/packager/sbom/catalog.go
Expand Up @@ -60,7 +60,7 @@ func Catalog(componentSBOMs map[string]*layout.ComponentSBOM, imageList []transf
defer builder.spinner.Stop()

// Ensure the sbom directory exists
_ = utils.CreateDirectory(builder.outputDir, helpers.ReadWriteExecuteUser)
_ = helpers.CreateDirectory(builder.outputDir, helpers.ReadWriteExecuteUser)

// Generate a list of images and files for the sbom viewer
json, err := builder.generateJSONList(componentSBOMs, imageList)
Expand Down Expand Up @@ -152,7 +152,7 @@ func (b *Builder) createImageSBOM(img v1.Image, src string) ([]byte, error) {
imageCachePath := filepath.Join(b.cachePath, layout.ImagesDir)

// Ensure the image cache directory exists.
if err := utils.CreateDirectory(imageCachePath, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(imageCachePath, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}

Expand Down