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

gosh-related refactors #874

Merged
merged 4 commits into from Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}