Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Env Variable Retention strategies (toggle between shell- and docs-mode) #761

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build:
.PHONY: wasm
wasm: WASM_OUTPUT ?= examples/web
wasm:
cp $(GO_ROOT)/misc/wasm/wasm_exec.js $(WASM_OUTPUT)
cp $(GO_ROOT)/lib/wasm/wasm_exec.js $(WASM_OUTPUT)
GOOS=js GOARCH=wasm go build -o $(WASM_OUTPUT)/runme.wasm -ldflags="$(LDFLAGS)" ./web

.PHONY: test/execute
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestPromptEnvVars(t *testing.T) {
err = promptEnvVars(cmd, runner, runTasks...)
assert.NoError(t, err)

expectedLines := `#
expectedLines := "# Managed env store retention strategy: first\n\n" + `#
# VAR_NAME1 set in managed env store
# "export VAR_NAME1='Placeholder 1'"
#
Expand Down
2 changes: 1 addition & 1 deletion internal/command/env_collector_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewEnvCollectorFactory() *EnvCollectorFactory {
}
}

func (f *EnvCollectorFactory) WithEnryption(value bool) *EnvCollectorFactory {
func (f *EnvCollectorFactory) WithEncryption(value bool) *EnvCollectorFactory {
f.encryptionEnabled = value
return f
}
Expand Down
44 changes: 33 additions & 11 deletions internal/command/program_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ const (
ProgramResolverStatusResolved
)

type VarRetentionStrategy uint8

const (
VarRetentionStrategyUnspecified VarRetentionStrategy = iota
// VarRetentionStrategyFirst means to always retain the first resolved value.
VarRetentionStrategyFirst
// VarRetentionStrategyLast means to always retain the last resolved value.
VarRetentionStrategyLast
)

type ProgramResolverResult struct {
Variables []ProgramResolverVarResult
ModifiedProgram bool
Expand All @@ -96,15 +106,30 @@ type ProgramResolverVarResult struct {

// Resolve resolves the environment variables found in a shell program.
// It might modify the program and write it provided writer.
func (r *ProgramResolver) Resolve(reader io.Reader, writer io.Writer) (*ProgramResolverResult, error) {
func (r *ProgramResolver) Resolve(reader io.Reader, writer io.Writer, varRetentionStrategy VarRetentionStrategy) (*ProgramResolverResult, error) {
f, err := syntax.NewParser().Parse(reader, "")
if err != nil {
return nil, errors.WithStack(err)
}

decls, modified, err := r.walk(f)
if err != nil {
return nil, err
if len(f.Stmts) > 0 {
s := "first"
if varRetentionStrategy == VarRetentionStrategyLast {
s = "last"
}
info := fmt.Sprintf("# Managed env store retention strategy: %s\n\n", s)
if _, err := writer.Write([]byte(info)); err != nil {
return nil, err
}
}

var decls []*syntax.DeclClause
modified := false
if varRetentionStrategy != VarRetentionStrategyLast {
decls, modified, err = r.walk(f)
if err != nil {
return nil, err
}
}

result := ProgramResolverResult{
Expand Down Expand Up @@ -314,14 +339,11 @@ func (r *ProgramResolver) walk(f *syntax.File) (result []*syntax.DeclClause, mod
return false
}

err = r.modifyStmt(node, decl)
modified, err = r.transformToComment(node, decl)
if err != nil {
return false
}

// TODO(adamb): does not make sense
modified = true

result = append(result, decl)

return false
Expand Down Expand Up @@ -352,11 +374,11 @@ func (r *ProgramResolver) isStmtSupportedDecl(stmt *syntax.Stmt) (*syntax.DeclCl
return decl, true
}

func (r *ProgramResolver) modifyStmt(stmt *syntax.Stmt, decl *syntax.DeclClause) error {
func (r *ProgramResolver) transformToComment(stmt *syntax.Stmt, decl *syntax.DeclClause) (bool, error) {
exportStmt := bytes.NewBuffer(nil)

if err := r.astPrinter.Print(exportStmt, decl); err != nil {
return errors.WithStack(err)
return false, errors.WithStack(err)
}

stmt.Comments = append(stmt.Comments,
Expand All @@ -367,7 +389,7 @@ func (r *ProgramResolver) modifyStmt(stmt *syntax.Stmt, decl *syntax.DeclClause)

stmt.Cmd = nil

return nil
return true, nil
}

// hasExpr walks the AST to check for nested expressions.
Expand Down
Loading