From c9f760ed69d262a506180309112242a33ae6180e Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 17 Nov 2023 09:23:24 +0200 Subject: [PATCH 1/2] Tidy up the bundle commands Signed-off-by: Stefan Prodan --- cmd/timoni/bundle_apply.go | 16 +++++++++++----- cmd/timoni/bundle_build.go | 16 +++++++++++----- cmd/timoni/bundle_vet.go | 18 ++++++++++++++++++ cmd/timoni/runtime_build.go | 20 +++++++++++++++++++- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/cmd/timoni/bundle_apply.go b/cmd/timoni/bundle_apply.go index 8d37a4e4..ff0fd6a1 100644 --- a/cmd/timoni/bundle_apply.go +++ b/cmd/timoni/bundle_apply.go @@ -59,6 +59,7 @@ var bundleApplyCmd = &cobra.Command{ # Pass secret values from stdin cat ./bundle_secrets.cue | timoni bundle apply -f ./bundle.cue -f - `, + Args: cobra.NoArgs, RunE: runBundleApplyCmd, } @@ -102,18 +103,23 @@ func init() { func runBundleApplyCmd(cmd *cobra.Command, _ []string) error { start := time.Now() files := bundleApplyArgs.files + if len(files) == 0 { + return fmt.Errorf("no bundle provided with -f") + } + var stdinFile string for i, file := range files { if file == "-" { - path, err := saveReaderToFile(cmd.InOrStdin()) + stdinFile, err := saveReaderToFile(cmd.InOrStdin()) if err != nil { return err } - - defer os.Remove(path) - - files[i] = path + files[i] = stdinFile + break } } + if stdinFile != "" { + defer os.Remove(stdinFile) + } tmpDir, err := os.MkdirTemp("", apiv1.FieldManager) if err != nil { diff --git a/cmd/timoni/bundle_build.go b/cmd/timoni/bundle_build.go index 6d3ce8c5..b0621b64 100644 --- a/cmd/timoni/bundle_build.go +++ b/cmd/timoni/bundle_build.go @@ -50,6 +50,7 @@ var bundleBuildCmd = &cobra.Command{ # Pass secret values from stdin cat ./bundle_secrets.cue | timoni bundle build -f ./bundle.cue -f - `, + Args: cobra.NoArgs, RunE: runBundleBuildCmd, } @@ -77,18 +78,23 @@ func init() { func runBundleBuildCmd(cmd *cobra.Command, _ []string) error { files := bundleBuildArgs.files + if len(files) == 0 { + return fmt.Errorf("no bundle provided with -f") + } + var stdinFile string for i, file := range files { if file == "-" { - path, err := saveReaderToFile(cmd.InOrStdin()) + stdinFile, err := saveReaderToFile(cmd.InOrStdin()) if err != nil { return err } - - defer os.Remove(path) - - files[i] = path + files[i] = stdinFile + break } } + if stdinFile != "" { + defer os.Remove(stdinFile) + } tmpDir, err := os.MkdirTemp("", apiv1.FieldManager) if err != nil { diff --git a/cmd/timoni/bundle_vet.go b/cmd/timoni/bundle_vet.go index 231f308d..849b262b 100644 --- a/cmd/timoni/bundle_vet.go +++ b/cmd/timoni/bundle_vet.go @@ -55,6 +55,7 @@ with Timoni's schema and optionally prints the computed value. -r runtime.cue \ --print-value `, + Args: cobra.NoArgs, RunE: runBundleVetCmd, } @@ -84,6 +85,23 @@ func init() { func runBundleVetCmd(cmd *cobra.Command, args []string) error { log := LoggerFrom(cmd.Context()) files := bundleVetArgs.files + if len(files) == 0 { + return fmt.Errorf("no bundle provided with -f") + } + var stdinFile string + for i, file := range files { + if file == "-" { + stdinFile, err := saveReaderToFile(cmd.InOrStdin()) + if err != nil { + return err + } + files[i] = stdinFile + break + } + } + if stdinFile != "" { + defer os.Remove(stdinFile) + } tmpDir, err := os.MkdirTemp("", apiv1.FieldManager) if err != nil { diff --git a/cmd/timoni/runtime_build.go b/cmd/timoni/runtime_build.go index 76dcc4e8..32aee668 100644 --- a/cmd/timoni/runtime_build.go +++ b/cmd/timoni/runtime_build.go @@ -32,10 +32,11 @@ import ( var runtimeBuildCmd = &cobra.Command{ Use: "build", - Short: "Build queries the cluster, extracts the values and prints them", + Short: "Build validates the runtime definition, queries the cluster, extracts the values and prints them", Example: ` # Print the runtime values from a cluster timoni runtime build -f runtime.cue `, + Args: cobra.NoArgs, RunE: runRuntimeBuildCmd, } @@ -53,6 +54,23 @@ func init() { func runRuntimeBuildCmd(cmd *cobra.Command, args []string) error { files := runtimeBuildArgs.files + if len(files) == 0 { + return fmt.Errorf("no runtime provided with -f") + } + var stdinFile string + for i, file := range files { + if file == "-" { + stdinFile, err := saveReaderToFile(cmd.InOrStdin()) + if err != nil { + return err + } + files[i] = stdinFile + break + } + } + if stdinFile != "" { + defer os.Remove(stdinFile) + } rt, err := buildRuntime(files) if err != nil { From 63eae982f285a7a4db24ad00a57ec1625e3fc724 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 17 Nov 2023 09:23:47 +0200 Subject: [PATCH 2/2] Tidy up the mod init command Signed-off-by: Stefan Prodan --- cmd/timoni/mod_init.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/timoni/mod_init.go b/cmd/timoni/mod_init.go index af22ea69..bc6ba14e 100644 --- a/cmd/timoni/mod_init.go +++ b/cmd/timoni/mod_init.go @@ -33,8 +33,11 @@ import ( var initModCmd = &cobra.Command{ Use: "init [MODULE NAME] [PATH]", Short: "Create a module along with common files and directories", - Example: ` # create a module in the current directory - timoni mod init my-app . + Example: ` # Create a module in the current directory + timoni mod init my-app + + # Create a module at the specified path + timoni mod init my-app ./modules `, RunE: runInitModCmd, } @@ -56,12 +59,16 @@ const ( ) func runInitModCmd(cmd *cobra.Command, args []string) error { - if len(args) < 2 { - return fmt.Errorf("module name and path are required") + if len(args) < 1 { + return fmt.Errorf("module name is required") } - initModArgs.name = args[0] - initModArgs.path = args[1] + + if len(args) == 2 { + initModArgs.path = args[1] + } else { + initModArgs.path = "." + } log := LoggerFrom(cmd.Context())