Skip to content

Commit

Permalink
update apply logic
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Kvapil <[email protected]>
  • Loading branch information
kvaps committed May 5, 2024
1 parent 5903bd0 commit fa37e2d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
18 changes: 8 additions & 10 deletions pkg/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import (

var initCmdFlags struct {
force bool
preset string
talosVersion string
}

// initCmd represents the `init` command.
var initCmd = &cobra.Command{
Use: "init [preset]",
Use: "init",
Short: "Initialize a new project and generate default values",
Long: ``,
Args: cobra.MaximumNArgs(1),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var (
secretsBundle *secrets.Bundle
Expand All @@ -45,13 +46,6 @@ var initCmd = &cobra.Command{
return fmt.Errorf("invalid talos-version: %w", err)
}
}
presetName := "generic"
if len(args) == 1 {
presetName = args[0]
}
if !isValidPreset(presetName) {
return fmt.Errorf("invalid preset: %s. Valid presets are: %s", presetName, generated.AvailablePresets)
}

secretsBundle, err = secrets.NewBundle(secrets.NewFixedClock(time.Now()),
versionContract,
Expand All @@ -60,6 +54,9 @@ var initCmd = &cobra.Command{
return fmt.Errorf("failed to create secrets bundle: %w", err)
}
var genOptions []generate.Option //nolint:prealloc
if !isValidPreset(initCmdFlags.preset) {
return fmt.Errorf("invalid preset: %s. Valid presets are: %s", initCmdFlags.preset, generated.AvailablePresets)
}
if initCmdFlags.talosVersion != "" {
var versionContract *config.VersionContract

Expand Down Expand Up @@ -104,7 +101,7 @@ var initCmd = &cobra.Command{
parts := strings.SplitN(path, "/", 2)
chartName := parts[0]
// Write preset files
if chartName == presetName {
if chartName == initCmdFlags.preset {
file := filepath.Join(Config.RootDir, filepath.Join(parts[1:]...))
if parts[len(parts)-1] == "Chart.yaml" {
writeToDestination([]byte(fmt.Sprintf(content, clusterName, Config.InitOptions.Version)), file, 0o644)
Expand Down Expand Up @@ -150,6 +147,7 @@ func writeSecretsBundleToFile(bundle *secrets.Bundle) error {

func init() {
initCmd.Flags().StringVar(&initCmdFlags.talosVersion, "talos-version", "", "the desired Talos version to generate config for (backwards compatibility, e.g. v0.8)")
initCmd.Flags().StringVarP(&initCmdFlags.preset, "preset", "p", "generic", "specify preset to generate files")
initCmd.Flags().BoolVar(&initCmdFlags.force, "force", false, "will overwrite existing files")

initCmd.PreRunE = func(cmd *cobra.Command, args []string) error {
Expand Down
12 changes: 7 additions & 5 deletions pkg/commands/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (

var templateCmdFlags struct {
insecure bool
valueFiles []string // -f/--values
valueFiles []string // --values
templateFiles []string // -t/--template
stringValues []string // --set-string
values []string // --set
fileValues []string // --set-file
Expand All @@ -32,10 +33,10 @@ var templateCmdFlags struct {
}

var templateCmd = &cobra.Command{
Use: "template <file ..>",
Use: "template",
Short: "Render templates locally and display the output",
Long: ``,
Args: cobra.MinimumNArgs(1),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if templateCmdFlags.offline {
return template(args)(context.Background(), nil)
Expand Down Expand Up @@ -65,7 +66,7 @@ func template(args []string) func(ctx context.Context, c *client.Client) error {
Root: Config.RootDir,
Offline: templateCmdFlags.offline,
KubernetesVersion: templateCmdFlags.kubernetesVersion,
TemplateFiles: args,
TemplateFiles: templateCmdFlags.templateFiles,
}

result, err := engine.Render(ctx, c, opts)
Expand All @@ -87,7 +88,8 @@ func template(args []string) func(ctx context.Context, c *client.Client) error {

func init() {
templateCmd.Flags().BoolVarP(&templateCmdFlags.insecure, "insecure", "i", false, "template using the insecure (encrypted with no auth) maintenance service")
templateCmd.Flags().StringSliceVarP(&templateCmdFlags.valueFiles, "values", "f", []string{}, "specify values in a YAML file (can specify multiple)")
templateCmd.Flags().StringSliceVarP(&templateCmdFlags.valueFiles, "values", "", []string{}, "specify values in a YAML file (can specify multiple)")
templateCmd.Flags().StringSliceVarP(&templateCmdFlags.templateFiles, "template", "t", []string{}, "specify templates to rendered manifest from (can specify multiple)")
templateCmd.Flags().StringArrayVar(&templateCmdFlags.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
templateCmd.Flags().StringArrayVar(&templateCmdFlags.stringValues, "set-string", []string{}, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
templateCmd.Flags().StringArrayVar(&templateCmdFlags.fileValues, "set-file", []string{}, "set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)")
Expand Down

0 comments on commit fa37e2d

Please sign in to comment.