Skip to content

Commit d8cb2a6

Browse files
authored
[envsec] Changes to allow launchpad to update (#174)
## Summary For launchpad to update we need: * Allow bootsrapping of a specific envID * Don't check if flag has changed in the commands themselves. * Remove `ToLower` Introduced in #118. This means envsec will be case sensitive. Note that the comments in that PR are not strictly correct, most commands do not do `ToLower` on environment, only `ls` and `exec` did. ## How was it tested? Compiled launchpad with updated version of envsec and tested envsec commands (set, ls, exec, rm)
1 parent 605414e commit d8cb2a6

File tree

7 files changed

+22
-29
lines changed

7 files changed

+22
-29
lines changed

envsec/pkg/envcli/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func DownloadCmd() *cobra.Command {
3535
return errors.Wrapf(errUnsupportedFormat, "format: %s", flags.format)
3636
},
3737
RunE: func(cmd *cobra.Command, args []string) error {
38-
cmdCfg, err := flags.genConfig(cmd.Context())
38+
cmdCfg, err := flags.genConfig(cmd)
3939
if err != nil {
4040
return errors.WithStack(err)
4141
}

envsec/pkg/envcli/exec.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,17 @@ func ExecCmd() *cobra.Command {
2626
Long: "Execute a specified command with remote environment variables being present for the duration of the command. If an environment variable exists both locally and in remote storage, the remotely stored one is prioritized.",
2727
Args: cobra.MinimumNArgs(1),
2828
RunE: func(cmd *cobra.Command, args []string) error {
29-
cmdCfg, err := flags.genConfig(cmd.Context())
29+
cmdCfg, err := flags.genConfig(cmd)
3030
if err != nil {
3131
return err
3232
}
3333
commandString := strings.Join(args, " ")
3434
commandToRun := exec.Command("/bin/sh", "-c", commandString)
3535

36-
// Default environment to fetch values from is DEV
37-
envNames := []string{"dev"}
38-
// If a specific environment was set by the user, then just use that one.
39-
if cmd.Flags().Changed(environmentFlagName) {
40-
envNames = []string{strings.ToLower(cmdCfg.EnvID.EnvName)}
41-
}
4236
envID := envsec.EnvID{
4337
OrgID: cmdCfg.EnvID.OrgID,
4438
ProjectID: cmdCfg.EnvID.ProjectID,
45-
EnvName: envNames[0],
39+
EnvName: cmdCfg.EnvID.EnvName,
4640
}
4741
// Get list of stored env variables
4842
envVars, err := cmdCfg.Store.List(cmd.Context(), envID)

envsec/pkg/envcli/flags.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package envcli
55

66
import (
7-
"context"
87
"fmt"
98
"os"
109

@@ -76,15 +75,17 @@ func (f *configFlags) validateProjectID(orgID typeids.OrganizationID) (string, e
7675
}
7776

7877
type CmdConfig struct {
79-
Store envsec.Store
80-
EnvID envsec.EnvID
78+
Store envsec.Store
79+
EnvID envsec.EnvID
80+
EnvNames []string
8181
}
8282

83-
func (f *configFlags) genConfig(ctx context.Context) (*CmdConfig, error) {
83+
func (f *configFlags) genConfig(cmd *cobra.Command) (*CmdConfig, error) {
8484
if bootstrappedConfig != nil {
8585
return bootstrappedConfig, nil
8686
}
8787

88+
ctx := cmd.Context()
8889
var tok *session.Token
8990
var err error
9091

@@ -132,9 +133,15 @@ func (f *configFlags) genConfig(ctx context.Context) (*CmdConfig, error) {
132133
return nil, errors.WithStack(err)
133134
}
134135

136+
envNames := []string{"dev", "prod", "staging"}
137+
if cmd.Flags().Changed(environmentFlagName) {
138+
envNames = []string{envid.EnvName}
139+
}
140+
135141
return &CmdConfig{
136-
Store: store,
137-
EnvID: envid,
142+
Store: store,
143+
EnvID: envid,
144+
EnvNames: envNames,
138145
}, nil
139146
}
140147

envsec/pkg/envcli/ls.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package envcli
55

66
import (
7-
"strings"
8-
97
"github.com/pkg/errors"
108
"github.com/spf13/cobra"
119
"go.jetpack.io/envsec"
@@ -29,23 +27,17 @@ func ListCmd() *cobra.Command {
2927
Long: "List all stored environment variables. If no environment flag is provided, variables in all environments will be listed.",
3028
Args: cobra.NoArgs,
3129
RunE: func(cmd *cobra.Command, _ []string) error {
32-
cmdCfg, err := flags.genConfig(cmd.Context())
30+
cmdCfg, err := flags.genConfig(cmd)
3331
if err != nil {
3432
return err
3533
}
36-
// Populate the valid Environments
37-
envNames := []string{"dev", "prod", "staging"}
38-
// If a specific environment was set by the user, then just use that one.
39-
if cmd.Flags().Changed(environmentFlagName) {
40-
envNames = []string{strings.ToLower(cmdCfg.EnvID.EnvName)}
41-
}
4234

4335
// TODO: parallelize
44-
for _, envName := range envNames {
36+
for _, envName := range cmdCfg.EnvNames {
4537
envID := envsec.EnvID{
4638
OrgID: cmdCfg.EnvID.OrgID,
4739
ProjectID: cmdCfg.EnvID.ProjectID,
48-
EnvName: strings.ToLower(envName),
40+
EnvName: envName,
4941
}
5042
envVars, err := cmdCfg.Store.List(cmd.Context(), envID)
5143
if err != nil {

envsec/pkg/envcli/rm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func RemoveCmd() *cobra.Command {
2424
Long: "Delete one or more environment variables that are stored.",
2525
Args: cobra.MinimumNArgs(1),
2626
RunE: func(cmd *cobra.Command, envNames []string) error {
27-
cmdCfg, err := flags.genConfig(cmd.Context())
27+
cmdCfg, err := flags.genConfig(cmd)
2828
if err != nil {
2929
return errors.WithStack(err)
3030
}

envsec/pkg/envcli/set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func SetCmd() *cobra.Command {
4242
if err != nil {
4343
return errors.WithStack(err)
4444
}
45-
cmdCfg, err := flags.genConfig(cmd.Context())
45+
cmdCfg, err := flags.genConfig(cmd)
4646
if err != nil {
4747
return errors.WithStack(err)
4848
}

envsec/pkg/envcli/upload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func UploadCmd() *cobra.Command {
7474
}
7575
}
7676

77-
cmdCfg, err := flags.genConfig(cmd.Context())
77+
cmdCfg, err := flags.genConfig(cmd)
7878
if err != nil {
7979
return err
8080
}

0 commit comments

Comments
 (0)