Skip to content

Commit

Permalink
Merge pull request #874 from direnv/refactor
Browse files Browse the repository at this point in the history
gosh-related refactors
  • Loading branch information
zimbatm committed Apr 17, 2022
2 parents aba32e8 + 66b0691 commit 829f9a4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
3 changes: 1 addition & 2 deletions internal/cmd/cmd_stdlib.go
Expand Up @@ -2,15 +2,14 @@ package cmd

import (
"fmt"
"strings"
)

// CmdStdlib is `direnv stdlib`
var CmdStdlib = &Cmd{
Name: "stdlib",
Desc: "Displays the stdlib available in the .envrc execution context",
Action: actionWithConfig(func(env Env, args []string, config *Config) error {
fmt.Println(strings.Replace(stdlib, "$(command -v direnv)", config.SelfPath, 1))
fmt.Println(getStdlib(config))
return nil
}),
}
45 changes: 25 additions & 20 deletions internal/cmd/rc.go
Expand Up @@ -146,18 +146,39 @@ func (rc *RC) Load(previousEnv Env) (newEnv Env, err error) {
newEnv[DIRENV_DIFF] = previousEnv.Diff(newEnv).Serialize()
}()

// Abort if the file is not allowed
if !rc.Allowed() {
err = fmt.Errorf(notAllowed, rc.Path())
return
}

// Allow RC loads to be canceled with SIGINT
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
cancel()
}()

// check what type of RC we're processing
// use different exec method for each
fn := "source_env"
if filepath.Base(rc.path) == ".env" {
fn = "dotenv"
}

// Set stdin based on the config
var stdin *os.File
if config.DisableStdin {
stdin, err = os.Open(os.DevNull)
if err != nil {
return
}
} else {
stdin = os.Stdin
}

prelude := ""
if config.StrictEnv {
prelude = "set -euo pipefail && "
Expand All @@ -171,32 +192,16 @@ func (rc *RC) Load(previousEnv Env) (newEnv Env, err error) {
rc.Path(),
)

// Allow RC loads to be canceled with SIGINT
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
cancel()
}()

// G204: Subprocess launched with function call as argument or cmd arguments
// #nosec
cmd := exec.CommandContext(ctx, config.BashPath, "--noprofile", "--norc", "-c", arg)
cmd := exec.CommandContext(ctx, config.BashPath, "-c", arg)
cmd.Dir = wd
cmd.Env = newEnv.ToGoEnv()
cmd.Stdin = stdin
cmd.Stderr = os.Stderr

if config.DisableStdin {
cmd.Stdin, err = os.Open(os.DevNull)
if err != nil {
return
}
} else {
cmd.Stdin = os.Stdin
}

if out, err := cmd.Output(); err == nil && len(out) > 0 {
var out []byte
if out, err = cmd.Output(); err == nil && len(out) > 0 {
var newEnv2 Env
newEnv2, err = LoadEnvJSON(out)
if err == nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/stdlib.go
@@ -0,0 +1,8 @@
package cmd

import "strings"

// getStdlib returns the stdlib.sh, with references to direnv replaced.
func getStdlib(config *Config) string {
return strings.Replace(stdlib, "$(command -v direnv)", config.SelfPath, 1)
}

0 comments on commit 829f9a4

Please sign in to comment.