From 466f185aeb20ef066648a8f9c9aa4271340e9059 Mon Sep 17 00:00:00 2001 From: Mathieu Frenette Date: Sun, 7 Mar 2021 13:42:01 -0500 Subject: [PATCH] Add `jen require` Signed-off-by: Mathieu Frenette --- README.md | 15 ++++++++++++- examples/bin/create-cicd-triggers | 2 ++ examples/bin/create-docker-repo | 2 ++ examples/bin/remove-cicd-triggers | 2 ++ examples/bin/remove-docker-repo | 2 ++ src/cmd/require/require.go | 36 +++++++++++++++++++++++++++++++ src/cmd/root.go | 2 ++ 7 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/cmd/require/require.go diff --git a/README.md b/README.md index 0dd4f95..34553df 100644 --- a/README.md +++ b/README.md @@ -453,6 +453,19 @@ Currently, those two placeholders are hardcoded and are the only ones supported, This feature was inspired by the way we were previously creating new projects by duplicating an existing project and doing a search-and-replace for the project name in different case variants. That strategy was very simple and effective, as long as the project name was a very distinct string that did not appear in any other undesired contexts, hence our choice of `projekt` as something that you are (hopefully!) very unlikely to encounter in your project for any other reason than those placeholders! +# Other commands + +## Verifying required variables in custom scripts + +To make your scripts more robust and self documented, you can use the `jen require VAR1 VAR2 ...` command in their first few lines (typically after `set -e` to make script fail in case of missing variable): + +```bash +#!/bin/bash +set -e +jen require PROJECT TEAM +echo "You are now garanteed that the $PROJECT and $TEAM variables can be used safely" +``` + # Tips ## Associating an existing project with a template @@ -462,9 +475,9 @@ To associate a template with an existing project that was not initially generate # Wishlist - Add `confirm` step (similar to `if`, but `confirm` property contains message to display and `then` the steps to execute). -- Add `jen chk vars VAR1 VAR2 ...` to ensure that all given variables are set in environment (to document and make scripts more robust). - Allow `do` step to define multiple actions to call. - Add reusable modules (including both templates and scripts). +- Add support for injecting snippets in specific sections of files in a second time (ie: adding multiple endpoints to an existing service). - Add `set` step to set multiple variables. - Add `--dry-run` flag (automatically turns on `--verbose`?). - Add regex validation for `input` prompt. diff --git a/examples/bin/create-cicd-triggers b/examples/bin/create-cicd-triggers index ebaa500..2fd3bd7 100755 --- a/examples/bin/create-cicd-triggers +++ b/examples/bin/create-cicd-triggers @@ -1,4 +1,6 @@ #!/bin/bash +set -e +jen require PROJECT echo "Creating triggers on CI/CD pipelines for project $PROJECT" sleep 1 echo "Done." diff --git a/examples/bin/create-docker-repo b/examples/bin/create-docker-repo index d3111fd..8414402 100755 --- a/examples/bin/create-docker-repo +++ b/examples/bin/create-docker-repo @@ -1,4 +1,6 @@ #!/bin/bash +set -e +jen require PROJECT echo "Creating docker image repo for project $PROJECT" sleep 1 echo "Done." diff --git a/examples/bin/remove-cicd-triggers b/examples/bin/remove-cicd-triggers index 8f959e0..e42ec38 100755 --- a/examples/bin/remove-cicd-triggers +++ b/examples/bin/remove-cicd-triggers @@ -1,4 +1,6 @@ #!/bin/bash +set -e +jen require PROJECT echo "Removing triggers from CI/CD pipelines for project $PROJECT" sleep 1 echo "Done." diff --git a/examples/bin/remove-docker-repo b/examples/bin/remove-docker-repo index 75123c8..8812368 100755 --- a/examples/bin/remove-docker-repo +++ b/examples/bin/remove-docker-repo @@ -1,4 +1,6 @@ #!/bin/bash +set -e +jen require PROJECT echo "Removing docker image repo for project $PROJECT" sleep 1 echo "Done." diff --git a/src/cmd/require/require.go b/src/cmd/require/require.go new file mode 100644 index 0000000..4b1481e --- /dev/null +++ b/src/cmd/require/require.go @@ -0,0 +1,36 @@ +package require + +import ( + "fmt" + "os" + + "github.com/Samasource/jen/src/cmd/internal" + "github.com/spf13/cobra" +) + +// New creates a cobra command +func New(options *internal.Options) *cobra.Command { + return &cobra.Command{ + Use: "require", + Short: "Validates that all given variables are defined in current environment", + RunE: func(_ *cobra.Command, args []string) error { + return run(options, args) + }, + } +} + +func run(options *internal.Options, args []string) error { + valid := true + for _, arg := range args { + _, ok := os.LookupEnv(arg) + if !ok { + fmt.Fprintf(os.Stderr, "Missing required variable %q\n", arg) + valid = false + } + } + + if !valid { + os.Exit(1) + } + return nil +} diff --git a/src/cmd/root.go b/src/cmd/root.go index f32d06a..a225ffe 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -7,6 +7,7 @@ import ( "github.com/Samasource/jen/src/cmd/internal" "github.com/Samasource/jen/src/cmd/list" "github.com/Samasource/jen/src/cmd/pull" + "github.com/Samasource/jen/src/cmd/require" "github.com/Samasource/jen/src/cmd/shell" "github.com/Samasource/jen/src/internal/logging" "github.com/spf13/cobra" @@ -34,5 +35,6 @@ continues to support you throughout development in executing project-related com c.AddCommand(shell.New(&options)) c.AddCommand(list.New(&options)) c.AddCommand(export.New(&options)) + c.AddCommand(require.New(&options)) return c }