Skip to content

Commit

Permalink
refactor: utilize invopop comment feature (#2781)
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 authored Jul 31, 2024
1 parent 1b95916 commit 568a160
Show file tree
Hide file tree
Showing 13 changed files with 666 additions and 447 deletions.
26 changes: 25 additions & 1 deletion src/cmd/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -156,12 +157,30 @@ tableOfContents: false
},
}

func addGoComments(reflector *jsonschema.Reflector) error {
addCommentErr := errors.New("this command must be called from the root of the Zarf repo")

typePackagePath := filepath.Join("src", "types")
if err := reflector.AddGoComments("github.com/zarf-dev/zarf", typePackagePath); err != nil {
return fmt.Errorf("%w: %w", addCommentErr, err)
}
varPackagePath := filepath.Join("src", "pkg", "variables")
if err := reflector.AddGoComments("github.com/zarf-dev/zarf", varPackagePath); err != nil {
return fmt.Errorf("%w: %w", addCommentErr, err)
}
return nil
}

var genConfigSchemaCmd = &cobra.Command{
Use: "gen-config-schema",
Aliases: []string{"gc"},
Short: lang.CmdInternalConfigSchemaShort,
RunE: func(_ *cobra.Command, _ []string) error {
reflector := jsonschema.Reflector(jsonschema.Reflector{ExpandedStruct: true})
if err := addGoComments(&reflector); err != nil {
return err
}

schema := reflector.Reflect(&types.ZarfPackage{})
output, err := json.MarshalIndent(schema, "", " ")
if err != nil {
Expand All @@ -183,7 +202,12 @@ var genTypesSchemaCmd = &cobra.Command{
Aliases: []string{"gt"},
Short: lang.CmdInternalTypesSchemaShort,
RunE: func(_ *cobra.Command, _ []string) error {
schema := jsonschema.Reflect(&zarfTypes{})
reflector := jsonschema.Reflector(jsonschema.Reflector{ExpandedStruct: true})
if err := addGoComments(&reflector); err != nil {
return err
}

schema := reflector.Reflect(&zarfTypes{})
output, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return fmt.Errorf("unable to generate the JSON schema for the Zarf types DeployedPackage, ZarfPackage, and ZarfState: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions src/extensions/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c type
MaxTotalSeconds: &maxTotalSeconds,
Wait: &types.ZarfComponentActionWait{
Cluster: &types.ZarfComponentActionWaitCluster{
Kind: "HelmRelease",
Identifier: hr.Metadata.Name,
Namespace: hr.Metadata.Namespace,
Condition: "ready",
Kind: "HelmRelease",
Name: hr.Metadata.Name,
Namespace: hr.Metadata.Namespace,
Condition: "ready",
},
},
}
Expand All @@ -171,7 +171,7 @@ func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c type
action.Wait.Cluster = &types.ZarfComponentActionWaitCluster{
Kind: "APIService",
// https://github.com/kubernetes-sigs/metrics-server#compatibility-matrix
Identifier: "v1beta1.metrics.k8s.io",
Name: "v1beta1.metrics.k8s.io",
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func convertWaitToCmd(wait types.ZarfComponentActionWait, timeout *int) (string,

// Build a call to the zarf tools wait-for command.
return fmt.Sprintf("./zarf tools wait-for %s %s %s %s %s",
cluster.Kind, cluster.Identifier, cluster.Condition, ns, timeoutString), nil
cluster.Kind, cluster.Name, cluster.Condition, ns, timeoutString), nil
}

network := wait.Network
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (n *Node) Prev() *Node {
// otherwise the name of the component will be used
func (n *Node) ImportName() string {
name := n.ZarfComponent.Name
if n.Import.ComponentName != "" {
name = n.Import.ComponentName
if n.Import.Name != "" {
name = n.Import.Name
}
return name
}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func (p *Packager) Publish(ctx context.Context) (err error) {
ex = append(ex, types.ZarfComponent{
Name: fmt.Sprintf("import-%s", c.Name),
Import: types.ZarfComponentImport{
ComponentName: c.Name,
URL: helpers.OCIURLPrefix + remote.Repo().Reference.String(),
Name: c.Name,
URL: helpers.OCIURLPrefix + remote.Repo().Reference.String(),
},
})
}
Expand Down
45 changes: 29 additions & 16 deletions src/pkg/variables/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,48 @@ var (

// Variable represents a variable that has a value set programmatically
type Variable struct {
Name string `json:"name" jsonschema:"description=The name to be used for the variable,pattern=^[A-Z0-9_]+$"`
Sensitive bool `json:"sensitive,omitempty" jsonschema:"description=Whether to mark this variable as sensitive to not print it in the log"`
AutoIndent bool `json:"autoIndent,omitempty" jsonschema:"description=Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_VAR_."`
Pattern string `json:"pattern,omitempty" jsonschema:"description=An optional regex pattern that a variable value must match before a package deployment can continue."`
Type VariableType `json:"type,omitempty" jsonschema:"description=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),enum=raw,enum=file"`
// 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"`
Description string `json:"description,omitempty" jsonschema:"description=A description of the variable to be used when prompting the user a value"`
Default string `json:"default,omitempty" jsonschema:"description=The default value to use for the variable"`
Prompt bool `json:"prompt,omitempty" jsonschema:"description=Whether to prompt the user for input for this variable"`
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 {
Name string `json:"name" jsonschema:"description=The name to be used for the constant,pattern=^[A-Z0-9_]+$"`
Value string `json:"value" jsonschema:"description=The value to set for the constant during deploy"`
// Include a description that will only be displayed during package create/deploy confirm prompts
Description string `json:"description,omitempty" jsonschema:"description=A description of the constant to explain its purpose on package create or deploy confirmation prompts"`
AutoIndent bool `json:"autoIndent,omitempty" jsonschema:"description=Whether to automatically indent the variable's value (if multiline) when templating. Based on the number of chars before the start of ###ZARF_CONST_."`
Pattern string `json:"pattern,omitempty" jsonschema:"description=An optional regex pattern that a constant value must match before a package can be created."`
// 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"`
Value string `json:"value" jsonschema:"description=The value the variable is currently set with"`
// The value the variable is currently set with
Value string `json:"value"`
}

// Validate runs all validation checks on a package constant.
Expand Down
Loading

0 comments on commit 568a160

Please sign in to comment.