Skip to content

Commit

Permalink
Add jen export
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 9396c88 commit 0c1d185
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 26 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ Actions are named operations that can be invoked by user via the `jen do ACTION`

You can have any arbitrary actions with any names in your template specs, however it is recommended to follow the convention of having at least the following actions:

- `create`:
- `create`:
- first invoke the `prompt` action below
- then render project template
- `prompt`:
Expand Down Expand Up @@ -462,7 +462,6 @@ 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 export` command to output env variables in a format that can be sourced directly.
- 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: 1 addition & 1 deletion src/cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func run(options *internal.Options, args []string) error {
args = []string{script}
}

return shell.Execute(execContext.GetShellVars(), "", strings.Join(args, " "))
return shell.Execute(execContext.GetShellVars(true), "", strings.Join(args, " "))
}

func promptScript(context exec.Context) (string, error) {
Expand Down
35 changes: 35 additions & 0 deletions src/cmd/export/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package export

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: "export",
Short: `Outputs vars in "export VAR=value" format to be sourced using "$(jen export)"`,
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.GetShellVars(false)
sort.Strings(vars)
for _, v := range vars {
fmt.Printf("export %s\n", v)
}
return nil
}
10 changes: 6 additions & 4 deletions src/cmd/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (c context) GetScripts() ([]string, error) {
// GetShellVars returns all env vars to be used when invoking shell commands,
// including the current process' env vars, the project's vars and an augmented
// PATH var including extra bin dirs.
func (c context) GetShellVars() []string {
func (c context) GetShellVars(includeProcessVars bool) []string {
// Add bin dirs to PATH env var
binDirs := c.getBinDirs()
pathVar := os.Getenv("PATH")
Expand All @@ -174,9 +174,11 @@ func (c context) GetShellVars() []string {

// Collect all current process env vars, except PATH
var env []string
for _, entry := range os.Environ() {
if !strings.HasPrefix(entry, "PATH=") {
env = append(env, entry)
if includeProcessVars {
for _, entry := range os.Environ() {
if !strings.HasPrefix(entry, "PATH=") {
env = append(env, entry)
}
}
}

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

import (
"strings"

"github.com/Samasource/jen/src/cmd/internal"
"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 @@ -18,22 +15,10 @@ func New(options *internal.Options) *cobra.Command {
Use: "list",
Aliases: []string{"ls"},
Short: "Lists available templates, actions, variables or scripts",
RunE: func(_ *cobra.Command, args []string) error {
return run(options, args)
},
}
c.AddCommand(actions.New(options))
c.AddCommand(scripts.New(options))
c.AddCommand(templates.New(options))
c.AddCommand(vars.New(options))
return c
}

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

return shell.Execute(execContext.GetShellVars(), "", strings.Join(args, " "))
}
2 changes: 2 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/Samasource/jen/src/cmd/do"
"github.com/Samasource/jen/src/cmd/exec"
"github.com/Samasource/jen/src/cmd/export"
"github.com/Samasource/jen/src/cmd/internal"
"github.com/Samasource/jen/src/cmd/list"
"github.com/Samasource/jen/src/cmd/pull"
Expand Down Expand Up @@ -32,5 +33,6 @@ continues to support you throughout development in executing project-related com
c.AddCommand(exec.New(&options))
c.AddCommand(shell.New(&options))
c.AddCommand(list.New(&options))
c.AddCommand(export.New(&options))
return c
}
2 changes: 1 addition & 1 deletion src/cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func run(options *internal.Options, args []string) error {
return err
}

return shell.Execute(execContext.GetShellVars(), "", "$SHELL")
return shell.Execute(execContext.GetShellVars(true), "", "$SHELL")
}
2 changes: 1 addition & 1 deletion src/internal/evaluation/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Context interface {
// GetShellVars returns all env vars to be used when invoking shell commands,
// including the current process' env vars, the project's vars and an augmented
// PATH var including extra bin dirs.
GetShellVars() []string
GetShellVars(includeProcessVars bool) []string
}

// RenderMode determines how/if rendering enabled/disabled state should change for an item
Expand Down
2 changes: 1 addition & 1 deletion src/internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Context interface {
// GetShellVars returns all env vars to be used when invoking shell commands,
// including the current process' env vars, the project's vars and an augmented
// PATH var including extra bin dirs.
GetShellVars() []string
GetShellVars(includeProcessVars bool) []string

// GetAction returns action with given name within same spec file or nil if not
// found.
Expand Down
2 changes: 1 addition & 1 deletion src/internal/steps/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func (e Exec) String() string {

// Execute runs one or multiple shell commands with project's variables and bin dirs
func (e Exec) Execute(context exec.Context) error {
return shell.Execute(context.GetShellVars(), context.GetProjectDir(), e.Commands...)
return shell.Execute(context.GetShellVars(true), context.GetProjectDir(), e.Commands...)
}

0 comments on commit 0c1d185

Please sign in to comment.