Skip to content

Commit

Permalink
fix: import paths to avoid cycle
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba committed Aug 8, 2024
1 parent 9f442ce commit 3582bce
Show file tree
Hide file tree
Showing 25 changed files with 299 additions and 313 deletions.
1 change: 0 additions & 1 deletion src/api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/defenseunicorns/pkg/helpers/v2 v2.0.1
github.com/invopop/jsonschema v0.12.0
github.com/stretchr/testify v1.9.0
github.com/zarf-dev/zarf v0.37.0
k8s.io/apimachinery v0.30.3
)

Expand Down
15 changes: 10 additions & 5 deletions src/api/v1alpha1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package v1alpha1
import (
"github.com/invopop/jsonschema"
"github.com/zarf-dev/zarf/src/api/v1alpha1/extensions"
"github.com/zarf-dev/zarf/src/pkg/utils/exec"
"github.com/zarf-dev/zarf/src/pkg/variables"
)

// ZarfComponent is the primary functional grouping of assets to deploy by Zarf.
Expand Down Expand Up @@ -228,7 +226,7 @@ type ZarfComponentActionDefaults struct {
// Additional environment variables for commands.
Env []string `json:"env,omitempty"`
// (cmd only) Indicates a preference for a shell for the provided cmd to be executed in on supported operating systems.
Shell exec.Shell `json:"shell,omitempty"`
Shell Shell `json:"shell,omitempty"`
}

// ZarfComponentAction represents a single action to run during a zarf package operation.
Expand All @@ -246,11 +244,11 @@ type ZarfComponentAction struct {
// The command to run. Must specify either cmd or wait for the action to do anything.
Cmd string `json:"cmd,omitempty"`
// (cmd only) Indicates a preference for a shell for the provided cmd to be executed in on supported operating systems.
Shell *exec.Shell `json:"shell,omitempty"`
Shell *Shell `json:"shell,omitempty"`
// [Deprecated] (replaced by setVariables) (onDeploy/cmd only) The name of a variable to update with the output of the command. This variable will be available to all remaining actions and components in the package. This will be removed in Zarf v1.0.0.
DeprecatedSetVariable string `json:"setVariable,omitempty" jsonschema:"pattern=^[A-Z0-9_]+$"`
// (onDeploy/cmd only) An array of variables to update with the output of the command. These variables will be available to all remaining actions and components in the package.
SetVariables []variables.Variable `json:"setVariables,omitempty"`
SetVariables []Variable `json:"setVariables,omitempty"`
// Description of the action to be displayed during package execution instead of the command.
Description string `json:"description,omitempty"`
// Wait for a condition to be met before continuing. Must specify either cmd or wait for the action. See the 'zarf tools wait-for' command for more info.
Expand Down Expand Up @@ -331,3 +329,10 @@ func (ZarfComponentImport) JSONSchemaExtend(schema *jsonschema.Schema) {
path.Not = notSchema
url.Not = notSchema
}

// Shell represents the desired shell to use for a given command
type Shell struct {
Windows string `json:"windows,omitempty" jsonschema:"description=(default 'powershell') Indicates a preference for the shell to use on Windows systems (note that choosing 'cmd' will turn off migrations like touch -> New-Item),example=powershell,example=cmd,example=pwsh,example=sh,example=bash,example=gsh"`
Linux string `json:"linux,omitempty" jsonschema:"description=(default 'sh') Indicates a preference for the shell to use on Linux systems,example=sh,example=bash,example=fish,example=zsh,example=pwsh"`
Darwin string `json:"darwin,omitempty" jsonschema:"description=(default 'sh') Indicates a preference for the shell to use on macOS systems,example=sh,example=bash,example=fish,example=zsh,example=pwsh"`
}
77 changes: 74 additions & 3 deletions src/api/v1alpha1/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@
package v1alpha1

import (
"github.com/zarf-dev/zarf/src/pkg/variables"
"fmt"
"regexp"
)

// VariableType represents a type of a Zarf package variable
type VariableType string

const (
// RawVariableType is the default type for a Zarf package variable
RawVariableType VariableType = "raw"
// FileVariableType is a type for a Zarf package variable that loads its contents from a file
FileVariableType VariableType = "file"
)

var (
// IsUppercaseNumberUnderscore is a regex for uppercase, numbers and underscores.
// https://regex101.com/r/tfsEuZ/1
IsUppercaseNumberUnderscore = regexp.MustCompile(`^[A-Z0-9_]+$`).MatchString
)

// ZarfPackageKind is an enum of the different kinds of Zarf packages.
Expand All @@ -32,9 +49,9 @@ type ZarfPackage struct {
// List of components to deploy in this package.
Components []ZarfComponent `json:"components" jsonschema:"minItems=1"`
// Constant template values applied on deploy for K8s resources.
Constants []variables.Constant `json:"constants,omitempty"`
Constants []Constant `json:"constants,omitempty"`
// Variable template values applied on deploy for K8s resources.
Variables []variables.InteractiveVariable `json:"variables,omitempty"`
Variables []InteractiveVariable `json:"variables,omitempty"`
}

// IsInitConfig returns whether a Zarf package is an init config.
Expand Down Expand Up @@ -62,6 +79,60 @@ func (pkg ZarfPackage) IsSBOMAble() bool {
return false
}

// Variable represents a variable that has a value set programmatically
type Variable struct {
// The name to be used for the variable
Name string `json:"name" jsonschema:"pattern=^[A-Z0-9_]+$"`
// Whether to mark this variable as sensitive to not print it in the log
Sensitive bool `json:"sensitive,omitempty"`
// Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_VAR_.
AutoIndent bool `json:"autoIndent,omitempty"`
// An optional regex pattern that a variable value must match before a package deployment can continue.
Pattern string `json:"pattern,omitempty"`
// Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB)
Type VariableType `json:"type,omitempty" jsonschema:"enum=raw,enum=file"`
}

// InteractiveVariable is a variable that can be used to prompt a user for more information
type InteractiveVariable struct {
Variable `json:",inline"`
// A description of the variable to be used when prompting the user a value
Description string `json:"description,omitempty"`
// The default value to use for the variable
Default string `json:"default,omitempty"`
// Whether to prompt the user for input for this variable
Prompt bool `json:"prompt,omitempty"`
}

// Constant are constants that can be used to dynamically template K8s resources or run in actions.
type Constant struct {
// The name to be used for the constant
Name string `json:"name" jsonschema:"pattern=^[A-Z0-9_]+$"`
// The value to set for the constant during deploy
Value string `json:"value"`
// A description of the constant to explain its purpose on package create or deploy confirmation prompts
Description string `json:"description,omitempty"`
// Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_CONST_.
AutoIndent bool `json:"autoIndent,omitempty"`
// An optional regex pattern that a constant value must match before a package can be created.
Pattern string `json:"pattern,omitempty"`
}

// SetVariable tracks internal variables that have been set during this run of Zarf
type SetVariable struct {
Variable `json:",inline"`
// The value the variable is currently set with
Value string `json:"value"`
}

// Validate runs all validation checks on a package constant.
func (c Constant) Validate() error {
if !regexp.MustCompile(c.Pattern).MatchString(c.Value) {
return fmt.Errorf("provided value for constant %s does not match pattern %s", c.Name, c.Pattern)
}
return nil
}

// ZarfMetadata lists information about the current ZarfPackage.
type ZarfMetadata struct {
// Name to identify this Zarf package.
Expand Down
Loading

0 comments on commit 3582bce

Please sign in to comment.