Skip to content

Commit

Permalink
Add jen require
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Frenette <[email protected]>
  • Loading branch information
silphid committed Mar 7, 2021
1 parent 0c1d185 commit 466f185
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions examples/bin/create-cicd-triggers
Original file line number Diff line number Diff line change
@@ -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."
2 changes: 2 additions & 0 deletions examples/bin/create-docker-repo
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
set -e
jen require PROJECT
echo "Creating docker image repo for project $PROJECT"
sleep 1
echo "Done."
2 changes: 2 additions & 0 deletions examples/bin/remove-cicd-triggers
Original file line number Diff line number Diff line change
@@ -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."
2 changes: 2 additions & 0 deletions examples/bin/remove-docker-repo
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
set -e
jen require PROJECT
echo "Removing docker image repo for project $PROJECT"
sleep 1
echo "Done."
36 changes: 36 additions & 0 deletions src/cmd/require/require.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 466f185

Please sign in to comment.