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

chore: slogger for zarf dev generate #3178

Merged
merged 1 commit into from
Nov 4, 2024
Merged
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
10 changes: 10 additions & 0 deletions src/pkg/packager/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,40 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/defenseunicorns/pkg/helpers/v2"
goyaml "github.com/goccy/go-yaml"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
)

// Generate generates a Zarf package definition.
func (p *Packager) Generate(ctx context.Context) error {
l := logger.From(ctx)
start := time.Now()
generatedZarfYAMLPath := filepath.Join(p.cfg.GenerateOpts.Output, layout.ZarfYAML)
spinner := message.NewProgressSpinner("Generating package for %q at %s", p.cfg.GenerateOpts.Name, generatedZarfYAMLPath)

if !helpers.InvalidPath(generatedZarfYAMLPath) {
prefixed := filepath.Join(p.cfg.GenerateOpts.Output, fmt.Sprintf("%s-%s", p.cfg.GenerateOpts.Name, layout.ZarfYAML))

message.Warnf("%s already exists, writing to %s", generatedZarfYAMLPath, prefixed)
l.Warn("using a prefixed name since zarf.yaml already exists in the output directory",
"output-directory", p.cfg.GenerateOpts.Output,
"name", prefixed)

generatedZarfYAMLPath = prefixed

if !helpers.InvalidPath(generatedZarfYAMLPath) {
return fmt.Errorf("unable to generate package, %s already exists", generatedZarfYAMLPath)
}
}
l.Info("generating package", "name", p.cfg.GenerateOpts.Name, "path", generatedZarfYAMLPath)

generatedComponent := v1alpha1.ZarfComponent{
Name: p.cfg.GenerateOpts.Name,
Expand Down Expand Up @@ -67,6 +75,7 @@ func (p *Packager) Generate(ctx context.Context) error {
if err != nil {
// purposefully not returning error here, as we can still generate the package without images
message.Warnf("Unable to find images: %s", err.Error())
l.Error("failed to find images", "error", err.Error())
}

for i := range p.cfg.Pkg.Components {
Expand Down Expand Up @@ -96,6 +105,7 @@ func (p *Packager) Generate(ctx context.Context) error {
content = strings.Replace(content, "components:\n", "\ncomponents:\n", 1)

spinner.Successf("Generated package for %q at %s", p.cfg.GenerateOpts.Name, generatedZarfYAMLPath)
l.Debug("generated package", "name", p.cfg.GenerateOpts.Name, "path", generatedZarfYAMLPath, "duration", time.Since(start))

return os.WriteFile(generatedZarfYAMLPath, []byte(content), helpers.ReadAllWriteUser)
}