Skip to content

Commit

Permalink
zarf dev generate
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
  • Loading branch information
AustinAbro321 committed Nov 4, 2024
1 parent 9a0874a commit b3e5a0f
Showing 1 changed file with 10 additions and 0 deletions.
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)
}

0 comments on commit b3e5a0f

Please sign in to comment.