Skip to content

Commit

Permalink
chore: move dev generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewg-xyz committed Feb 9, 2024
1 parent 0097563 commit 28393f0
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 79 deletions.
74 changes: 74 additions & 0 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
"github.com/mholt/archiver/v3"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -60,6 +61,69 @@ var devDeployCmd = &cobra.Command{
},
}

var devGenerateCmd = &cobra.Command{
Use: "generate NAME",
Aliases: []string{"g"},
Args: cobra.ExactArgs(1),
Short: "Use to generate either an example package or a package from resources",
Example: "zarf dev generate podinfo --url https://github.com/stefanprodan/podinfo.git --version 6.4.0 --gitPath charts/podinfo",
Run: func(cmd *cobra.Command, args []string) {
newPkg := types.ZarfPackage{
Metadata: types.ZarfMetadata{
Name: args[0],
},
Kind: "ZarfPackageConfig",
}

message.Info("Unimplemented - Package with name " + "\"" + newPkg.Metadata.Name + "\"" + " will be generated upon implementation")

// if cmd.Flags().Changed("from") {
// if cmd.Flags().Changed("assume") {
// message.Warn("Zarf will assume all necessary parts of components because \"--assume\" has been set")
// }
// for _, componentSource := range types.PackagerConfig.GenerateOpts.From.From {
// message.Notef("Starting component generation from %s", componentSource)
// spinner := message.NewProgressSpinner("Deducing component type for %s", componentSource)
// result := generator.DeduceResourceType(componentSource)
// switch result {
// case "unknown path":
// spinner.Fatalf(errors.New("invalid path"), "The path %s is not valid or an empty folder", componentSource)
// case "unknown url":
// spinner.Fatalf(errors.New("invalid url"), "The url %s could not be reconciled into a component type", componentSource)
// case "unparsable":
// spinner.Fatalf(errors.New("invalid from arg"), "The value %s could not be reconciled into a url or path", componentSource)
// }
// spinner.Successf("%s's component from %s is a %s", pkgName, componentSource, result)

// switch result {
// case "localChart":
// newPkg.Components = append(newPkg.Components, generator.GenLocalChart(componentSource))
// case "manifests":
// newPkg.Components = append(newPkg.Components, generator.GenManifests(componentSource))
// case "localFiles":
// newPkg.Components = append(newPkg.Components, generator.GenLocalFiles(componentSource))
// case "gitChart":
// newPkg.Components = append(newPkg.Components, generator.GenGitChart(componentSource))
// case "helmRepoChart":
// newPkg.Components = append(newPkg.Components, generator.GenHelmRepoChart(componentSource))
// case "remoteFile":
// newPkg.Components = append(newPkg.Components, generator.GenRemoteFile(componentSource))
// }
// }
// } else {
// message.Fatal(errors.New("Unimplemented"), "Unimplemented")
// }
// packageLocation := "./" + pkgName + ".zarf.yaml"
// message.Note("Component Generation Complete!")
// spinner := message.NewProgressSpinner("Writing package file to %s", packageLocation)
// err := utils.WriteYaml(packageLocation, newPkg, 0644)
// if err != nil {
// spinner.Fatalf(err, err.Error())
// }
// spinner.Successf("Package generated successfully! Package saved to %s", packageLocation)
},
}

var devTransformGitLinksCmd = &cobra.Command{
Use: "patch-git HOST FILE",
Aliases: []string{"p"},
Expand Down Expand Up @@ -252,13 +316,15 @@ func init() {
rootCmd.AddCommand(devCmd)

devCmd.AddCommand(devDeployCmd)
devCmd.AddCommand(devGenerateCmd)
devCmd.AddCommand(devTransformGitLinksCmd)
devCmd.AddCommand(devSha256SumCmd)
devCmd.AddCommand(devFindImagesCmd)
devCmd.AddCommand(devGenConfigFileCmd)
devCmd.AddCommand(devLintCmd)

bindDevDeployFlags(v)
bindDevGenerateFlags(v)

devSha256SumCmd.Flags().StringVarP(&extractPath, "extract-path", "e", "", lang.CmdDevFlagExtractPath)

Expand Down Expand Up @@ -286,3 +352,11 @@ func bindDevDeployFlags(v *viper.Viper) {

devDeployFlags.BoolVar(&pkgConfig.CreateOpts.NoYOLO, "no-yolo", v.GetBool(common.VDevDeployNoYolo), lang.CmdDevDeployFlagNoYolo)
}

func bindDevGenerateFlags(v *viper.Viper) {
generateFlags := devGenerateCmd.Flags()

generateFlags.StringArrayVarP(&pkgConfig.GenerateOpts.URL, "url", "u", []string{}, "URL to the source git repository")
generateFlags.StringArrayVarP(&pkgConfig.GenerateOpts.Version, "version", "v", []string{}, "The Version of the chart to use")
generateFlags.StringArrayVarP(&pkgConfig.GenerateOpts.GitPath, "gitPath", "g", []string{}, "Relative path to the chart in the git repository")
}
73 changes: 0 additions & 73 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package cmd

import (
"errors"
"fmt"
"path/filepath"
"regexp"
Expand All @@ -16,13 +15,11 @@ import (
"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"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/internal/packager/generator"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/packager"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
Expand Down Expand Up @@ -115,66 +112,6 @@ var packageMirrorCmd = &cobra.Command{
},
}

var packageGenerateCmd = &cobra.Command{
Use: "generate NAME [--from data]...",
Aliases: []string{"g"},
Args: cobra.ExactArgs(1),
Short: "Use to generate either an example package or a package from resources",
Run: func(cmd *cobra.Command, args []string) {
pkgName := args[0]
newPkg := types.ZarfPackage{
Metadata: types.ZarfMetadata{
Name: pkgName,
},
Kind: "ZarfPackageConfig",
}
if cmd.Flags().Changed("from") {
if cmd.Flags().Changed("assume") {
message.Warn("Zarf will assume all necessary parts of components because \"--assume\" has been set")
}
for _, componentSource := range config.GenerateOptions.From {
message.Notef("Starting component generation from %s", componentSource)
spinner := message.NewProgressSpinner("Deducing component type for %s", componentSource)
result := generator.DeduceResourceType(componentSource)
switch result {
case "unknown path":
spinner.Fatalf(errors.New("invalid path"), "The path %s is not valid or an empty folder", componentSource)
case "unknown url":
spinner.Fatalf(errors.New("invalid url"), "The url %s could not be reconciled into a component type", componentSource)
case "unparsable":
spinner.Fatalf(errors.New("invalid from arg"), "The value %s could not be reconciled into a url or path", componentSource)
}
spinner.Successf("%s's component from %s is a %s", pkgName, componentSource, result)

switch result {
case "localChart":
newPkg.Components = append(newPkg.Components, generator.GenLocalChart(componentSource))
case "manifests":
newPkg.Components = append(newPkg.Components, generator.GenManifests(componentSource))
case "localFiles":
newPkg.Components = append(newPkg.Components, generator.GenLocalFiles(componentSource))
case "gitChart":
newPkg.Components = append(newPkg.Components, generator.GenGitChart(componentSource))
case "helmRepoChart":
newPkg.Components = append(newPkg.Components, generator.GenHelmRepoChart(componentSource))
case "remoteFile":
newPkg.Components = append(newPkg.Components, generator.GenRemoteFile(componentSource))
}
}
} else {
message.Fatal(errors.New("Unimplemented"), "Unimplemented")
}
packageLocation := "./" + pkgName + ".zarf.yaml"
message.Note("Component Generation Complete!")
spinner := message.NewProgressSpinner("Writing package file to %s", packageLocation)
err := utils.WriteYaml(packageLocation, newPkg, 0644)
if err != nil {
spinner.Fatalf(err, err.Error())
}
spinner.Successf("Package generated successfully! Package saved to %s", packageLocation)
},
}

var packageInspectCmd = &cobra.Command{
Use: "inspect [ PACKAGE_SOURCE ]",
Aliases: []string{"i"},
Expand Down Expand Up @@ -377,7 +314,6 @@ func init() {
packageCmd.AddCommand(packageDeployCmd)
packageCmd.AddCommand(packageMirrorCmd)
packageCmd.AddCommand(packageInspectCmd)
packageCmd.AddCommand(packageGenerateCmd)
packageCmd.AddCommand(packageRemoveCmd)
packageCmd.AddCommand(packageListCmd)
packageCmd.AddCommand(packagePublishCmd)
Expand All @@ -386,7 +322,6 @@ func init() {
bindPackageFlags(v)
bindCreateFlags(v)
bindDeployFlags(v)
bindPackageGenerateFlags()
bindMirrorFlags(v)
bindInspectFlags(v)
bindRemoveFlags(v)
Expand Down Expand Up @@ -481,14 +416,6 @@ func bindMirrorFlags(v *viper.Viper) {
mirrorFlags.StringVar(&pkgConfig.InitOpts.RegistryInfo.PushPassword, "registry-push-password", v.GetString(common.VInitRegistryPushPass), lang.CmdInitFlagRegPushPass)
}

func bindPackageGenerateFlags() {
generateFlags := packageGenerateCmd.Flags()

generateFlags.BoolVar(&config.CommonOptions.Confirm, "assume", false, "WARNING: Can have unexpected and usually incorrect results\nZarf will make assumptions about all aspects of package generation and will not ask the user for any input")
generateFlags.StringArrayVarP(&config.GenerateOptions.From, "from", "f", []string{}, "The location of the resource to generate a package from")
generateFlags.StringVarP(&config.GenerateOptions.Namespace, "namespace", "n", "", "The namespace for all generated components")
}

func bindInspectFlags(_ *viper.Viper) {
inspectFlags := packageInspectCmd.Flags()
inspectFlags.BoolVarP(&pkgConfig.InspectOpts.ViewSBOM, "sbom", "s", false, lang.CmdPackageInspectFlagSbom)
Expand Down
3 changes: 0 additions & 3 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ var (
// DeployOptions tracks user-defined values for the active deployment
DeployOptions types.ZarfDeployOptions

// GenerateOptions tracks user-defined values for package generation.
GenerateOptions types.ZarfGenerateOptions

// InitOptions tracks user-defined values for the active Zarf initialization.
InitOptions types.ZarfInitOptions

Expand Down
41 changes: 41 additions & 0 deletions src/test/e2e/13_zarf_package_generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package test provides e2e tests for Zarf.
package test

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestZarfDevGenerate(t *testing.T) {
t.Log("E2E: Zarf Dev Generate")

stdOut, stdErr, err := e2e.Zarf("dev", "generate", "podinfo", "--url", "https://github.com/stefanprodan/podinfo.git", "--version", "6.4.0", "--gitPath", "charts/podinfo")
require.NoError(t, err, stdOut, stdErr)



// Assert the ZarfPackageConfig
// ```yaml
// kind: ZarfPackageConfig
// metadata:
// name: podinfo # This is <package-name>

// components:
// - name: podinfo # This is <package-name>
// required: true
// charts:
// - name: podinfo # This is <chart-name> from the Chart.yaml
// version: 6.4.0 # This is --version
// namespace: podinfo # This is <chart-name> from the Chart.yaml
// url: https://github.com/stefanprodan/podinfo.git # This is --url
// gitPath: charts/podinfo # This is --gitPath
// images: # These are autodiscovered with find-images logic
// - ghcr.io/stefanprodan/podinfo:6.4.0
// # This is the cosign signature for the podinfo image for image signature verification
// - ghcr.io/stefanprodan/podinfo:sha256-57a654ace69ec02ba8973093b6a786faa15640575fbf0dbb603db55aca2ccec8.sig
// ```
}
3 changes: 3 additions & 0 deletions src/types/packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type PackagerConfig struct {
// FindImagesOpts tracks user-defined options used to find images
FindImagesOpts ZarfFindImagesOptions

// GenerateOpts tracks user-defined values for package generation.
GenerateOpts ZarfGenerateOptions

// The package data
Pkg ZarfPackage

Expand Down
7 changes: 4 additions & 3 deletions src/types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ type ZarfPullOptions struct {

// ZarfGenerateOptions tracks the user-defined options during package generation.
type ZarfGenerateOptions struct {
From []string `json:"from" jsonschema:"description=The location of a resource to generate a package from"`
Namespace string `json:"namespace" jsonschema:"description=The namespace to deploy the generated package to"`
URL []string `json:"url" jsonschema:"description=URL to the source git repository"`
Version []string `json:"version" jsonschema:"description=Version of the chart to use"`
GitPath []string `json:"gitPath" jsonschema:"description=Relative path to the chart in the git repository"`
}

// ZarfInitOptions tracks the user-defined options during cluster initialization.
Expand Down Expand Up @@ -119,7 +120,7 @@ type ZarfCreateOptions struct {
Flavor string `json:"flavor" jsonschema:"description=An optional variant that controls which components will be included in a package"`
IsSkeleton bool `json:"isSkeleton" jsonschema:"description=Whether to create a skeleton package"`
NoYOLO bool `json:"noYOLO" jsonschema:"description=Whether to create a YOLO package"`
OutputDirectory string `json:"outputDirectory" jsonschema:"description=Location where the finalized Zarf package will be placed"`
OutputDirectory string `json:"outputDirectory" jsonschema:"description=Location where the finalized Zarf package will be placed"`
}

// ZarfSplitPackageData contains info about a split package.
Expand Down

0 comments on commit 28393f0

Please sign in to comment.