Skip to content

Commit

Permalink
Add jen list vars
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 173e1f8 commit 9396c88
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,36 @@ $ jen do create
8. If in previous prompts you opted for installing your project in CI/CD, the `install` action will be called now to simulate that.
9. At this point, typically, you would commit your project to git, including the `jen.yaml` file.

## Inspecting project variables

Let's have a look at our project's `jen.yaml` file that has just been created:

```bash
cat jen.yaml
version: 0.2.0
templatename: hello-world
vars:
INSTALL: true
NEWRELIC: true
PROJECT: foobar
PSQL: true
TEAM: devops
```

But there's a dedicated command for viewing variables, which can be invoked from anywhere within your project structure:

```bash
$ jen list vars
INSTALL: true
NEWRELIC: true
PROJECT: foobar
PSQL: true
TEAM: devops
```

## Invoking actions

You can now call different project actions with `jen do ACTION`, but first let's see what actions the `hello-world` example defines:
We are now ready to call different project actions with `jen do ACTION`, but first let's see what actions the `hello-world` example defines:

```bash
$ jen list actions
Expand Down Expand Up @@ -120,7 +147,7 @@ $ jen do

## Executing scripts

You can execute custom scripts (or any shell command really) with `jen exec CMD ARG1 ARG2 ...`, but first let's see what scripts the `hello-world` example defines:
Typically, we would always go through higher-level actions to call scripts and shell commands, but we can also invoke them directly using `jen exec CMD ARG1 ARG2 ...`. However, let's first see what scripts the `hello-world` example defines:

```bash
$ jen list scripts
Expand Down Expand Up @@ -151,7 +178,7 @@ $ jen exec
remove-docker-repo
```

Just keep in mind that you are not limited to custom scripts, you can really execute any shell command with the `jen exec CMD ARG1 ARG2 ...` syntax.
Just keep in mind that you are not limited to custom scripts, you can execute really any shell command with the `jen exec CMD ARG1 ARG2 ...` syntax.

## Starting a sub-shell

Expand Down Expand Up @@ -436,7 +463,6 @@ To associate a template with an existing project that was not initially generate

- Add `confirm` step (similar to `if`, but `confirm` property contains message to display and `then` the steps to execute).
- Add `jen export` command to output env variables in a format that can be sourced directly.
- Add `jen list vars` to list project variables and their values (same as `jen export` but more human-readable).
- 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).
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Samasource/jen/src/cmd/list/actions"
"github.com/Samasource/jen/src/cmd/list/scripts"
"github.com/Samasource/jen/src/cmd/list/templates"
"github.com/Samasource/jen/src/cmd/list/vars"
"github.com/Samasource/jen/src/internal/shell"
"github.com/spf13/cobra"
)
Expand All @@ -24,6 +25,7 @@ func New(options *internal.Options) *cobra.Command {
c.AddCommand(actions.New(options))
c.AddCommand(scripts.New(options))
c.AddCommand(templates.New(options))
c.AddCommand(vars.New(options))
return c
}

Expand Down
45 changes: 45 additions & 0 deletions src/cmd/list/vars/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package vars

import (
"fmt"
"sort"

"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: "vars",
Aliases: []string{"var"},
Short: "Lists variables defined in current project and their current values",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, args []string) error {
return run(options, args)
},
}
}

func run(options *internal.Options, args []string) error {
execContext, err := options.NewContext()
if err != nil {
return err
}

vars := execContext.GetVars()

// Sort names
names := make([]string, 0, len(vars))
for name := range vars {
names = append(names, name)
}
sort.Strings(names)

// Print names and values
for _, name := range names {
value := vars[name]
fmt.Printf("%s: %v\n", name, value)
}
return nil
}

0 comments on commit 9396c88

Please sign in to comment.