From 0c1d18551de69eae2b229764d2892042092ab951 Mon Sep 17 00:00:00 2001 From: Mathieu Frenette Date: Sun, 7 Mar 2021 13:05:15 -0500 Subject: [PATCH] Add `jen export` Signed-off-by: Mathieu Frenette --- README.md | 3 +-- src/cmd/exec/exec.go | 2 +- src/cmd/export/export.go | 35 +++++++++++++++++++++++++++ src/cmd/internal/internal.go | 10 +++++--- src/cmd/list/list.go | 15 ------------ src/cmd/root.go | 2 ++ src/cmd/shell/shell.go | 2 +- src/internal/evaluation/evaluation.go | 2 +- src/internal/exec/exec.go | 2 +- src/internal/steps/exec/exec.go | 2 +- 10 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 src/cmd/export/export.go diff --git a/README.md b/README.md index 0fbfe39..0dd4f95 100644 --- a/README.md +++ b/README.md @@ -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`: @@ -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). diff --git a/src/cmd/exec/exec.go b/src/cmd/exec/exec.go index d59118f..1247f78 100644 --- a/src/cmd/exec/exec.go +++ b/src/cmd/exec/exec.go @@ -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) { diff --git a/src/cmd/export/export.go b/src/cmd/export/export.go new file mode 100644 index 0000000..3063240 --- /dev/null +++ b/src/cmd/export/export.go @@ -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 +} diff --git a/src/cmd/internal/internal.go b/src/cmd/internal/internal.go index f075f6b..0d2b4cc 100644 --- a/src/cmd/internal/internal.go +++ b/src/cmd/internal/internal.go @@ -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") @@ -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) + } } } diff --git a/src/cmd/list/list.go b/src/cmd/list/list.go index 647066c..039a436 100644 --- a/src/cmd/list/list.go +++ b/src/cmd/list/list.go @@ -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" ) @@ -18,9 +15,6 @@ 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)) @@ -28,12 +22,3 @@ func New(options *internal.Options) *cobra.Command { 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, " ")) -} diff --git a/src/cmd/root.go b/src/cmd/root.go index cdfd16d..f32d06a 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -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" @@ -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 } diff --git a/src/cmd/shell/shell.go b/src/cmd/shell/shell.go index ebd34d6..59ee3f2 100644 --- a/src/cmd/shell/shell.go +++ b/src/cmd/shell/shell.go @@ -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") } diff --git a/src/internal/evaluation/evaluation.go b/src/internal/evaluation/evaluation.go index 3d87d12..aece8ae 100644 --- a/src/internal/evaluation/evaluation.go +++ b/src/internal/evaluation/evaluation.go @@ -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 diff --git a/src/internal/exec/exec.go b/src/internal/exec/exec.go index 8e79bdc..5ec53bd 100644 --- a/src/internal/exec/exec.go +++ b/src/internal/exec/exec.go @@ -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. diff --git a/src/internal/steps/exec/exec.go b/src/internal/steps/exec/exec.go index 38a388c..1a61947 100644 --- a/src/internal/steps/exec/exec.go +++ b/src/internal/steps/exec/exec.go @@ -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...) }