From f109eb1b42857b990559b1700cb66704078a8678 Mon Sep 17 00:00:00 2001 From: David Cassany Date: Tue, 24 Sep 2024 18:12:14 +0200 Subject: [PATCH] Allow features to be passed as arguments Signed-off-by: David Cassany --- cmd/init.go | 27 ++++++++++++++++++++++----- examples/green-rpi/Dockerfile | 12 +++++++++++- pkg/features/features.go | 1 - 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index d77788cb88b..dd294577417 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -17,6 +17,8 @@ limitations under the License. package cmd import ( + "fmt" + "slices" "strings" "github.com/spf13/cobra" @@ -34,10 +36,22 @@ func InitCmd(root *cobra.Command) *cobra.Command { Use: "init FEATURES", Short: "Initialize container image for booting", Long: "Init a container image with elemental configuration\n\n" + - "FEATURES - should be provided as a comma-separated list of features to install.\n" + - " Available features: " + strings.Join(features.All, ",") + "\n" + - " Defaults to " + strings.Join(features.Default, ","), - Args: cobra.MaximumNArgs(1), + "FEATURES - provided as an argument list of features to install.\n" + + " Available features:\n\t" + strings.Join(features.All, "\n\t") + "\n\n" + + " Defaults to:\n\t" + strings.Join(features.Default, "\n\t"), + ValidArgs: features.All, + Args: func(cmd *cobra.Command, args []string) error { + // This is logic is just to keep backward compatibility with + // comma separated values + if len(args) == 1 { + for _, arg := range strings.Split(args[0], ",") { + if !slices.Contains(features.All, arg) { + return fmt.Errorf("invalid argument \"%s\" for \"elemental init\"", arg) + } + } + } + return cobra.OnlyValidArgs(cmd, args) + }, RunE: func(cmd *cobra.Command, args []string) error { cfg, err := config.ReadConfigRun(viper.GetString("config-dir"), cmd.Flags(), types.NewDummyMounter()) if err != nil { @@ -54,8 +68,11 @@ func InitCmd(root *cobra.Command) *cobra.Command { if len(args) == 0 { spec.Features = features.Default - } else { + } else if len(args) == 1 { + // The old behavior is kept to keep backward compatibiliy spec.Features = strings.Split(args[0], ",") + } else { + spec.Features = args } cfg.Logger.Infof("Initializing system...") diff --git a/examples/green-rpi/Dockerfile b/examples/green-rpi/Dockerfile index 4a731b38350..4960d9ee1f7 100644 --- a/examples/green-rpi/Dockerfile +++ b/examples/green-rpi/Dockerfile @@ -55,7 +55,17 @@ COPY --from=TOOLKIT /usr/bin/elemental /usr/bin/elemental RUN systemctl enable NetworkManager.service # Generate initrd with required elemental services -RUN elemental --debug init --force +RUN elemental --debug init --force \ + elemental-rootfs \ + elemental-sysroot \ + grub-config \ + grub-default-bootargs \ + elemental-setup \ + dracut-config \ + cloud-config-defaults \ + cloud-config-essentials \ + boot-assessment \ + arm-firmware # Update os-release file with some metadata RUN echo IMAGE_REPO=\"${REPO}\" >> /etc/os-release && \ diff --git a/pkg/features/features.go b/pkg/features/features.go index 3619e7c3c13..4afc1c44fea 100644 --- a/pkg/features/features.go +++ b/pkg/features/features.go @@ -81,7 +81,6 @@ var ( FeatureCloudConfigDefaults, FeatureCloudConfigEssentials, FeatureBootAssessment, - FeatureArmFirmware, } )