Skip to content

Commit

Permalink
more formatting cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zimbatm committed Jan 18, 2020
1 parent 219d9f7 commit e1b73f4
Show file tree
Hide file tree
Showing 25 changed files with 448 additions and 416 deletions.
5 changes: 3 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ direnv: stdlib.go *.go
$(GO) build $(GO_BUILD_FLAGS) -o $(exe)

stdlib.go: stdlib.sh
cat $< | ./script/str2go main STDLIB $< > $@
cat $< | ./script/str2go main StdLib $< > $@

version.go: version.txt
echo package main > $@
echo >> $@
echo 'const VERSION = "$(shell cat $<)"' >> $@
echo "// Version is direnv's version"
echo 'const Version = "$(shell cat $<)"' >> $@

############################################################################
# Format all the things
Expand Down
36 changes: 19 additions & 17 deletions cmd_allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import (

// CmdAllow is `direnv allow [PATH_TO_RC]`
var CmdAllow = &Cmd{
Name: "allow",
Desc: "Grants direnv to load the given .envrc",
Args: []string{"[PATH_TO_RC]"},
Action: actionWithConfig(func(env Env, args []string, config *Config) (err error) {
var rcPath string
if len(args) > 1 {
rcPath = args[1]
} else {
if rcPath, err = os.Getwd(); err != nil {
return
}
}
Name: "allow",
Desc: "Grants direnv to load the given .envrc",
Args: []string{"[PATH_TO_RC]"},
Action: actionWithConfig(cmdAllowAction),
}

rc := FindRC(rcPath, config)
if rc == nil {
return fmt.Errorf(".envrc file not found")
func cmdAllowAction(env Env, args []string, config *Config) (err error) {
var rcPath string
if len(args) > 1 {
rcPath = args[1]
} else {
if rcPath, err = os.Getwd(); err != nil {
return
}
return rc.Allow()
}),
}

rc := FindRC(rcPath, config)
if rc == nil {
return fmt.Errorf(".envrc file not found")
}
return rc.Allow()
}
50 changes: 26 additions & 24 deletions cmd_apply_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,37 @@ var CmdApplyDump = &Cmd{
Desc: "Accepts a filename containing `direnv dump` output and generates a series of bash export statements to apply the given env",
Args: []string{"FILE"},
Private: true,
Action: actionSimple(func(env Env, args []string) (err error) {
if len(args) < 2 {
return fmt.Errorf("not enough arguments")
}
Action: actionSimple(cmdApplyDumpAction),
}

func cmdApplyDumpAction(env Env, args []string) (err error) {
if len(args) < 2 {
return fmt.Errorf("not enough arguments")
}

if len(args) > 2 {
return fmt.Errorf("too many arguments")
}
filename := args[1]
if len(args) > 2 {
return fmt.Errorf("too many arguments")
}
filename := args[1]

dumped, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
dumped, err := ioutil.ReadFile(filename)
if err != nil {
return err
}

dumpedEnv, err := LoadEnv(string(dumped))
if err != nil {
return err
}
dumpedEnv, err := LoadEnv(string(dumped))
if err != nil {
return err
}

diff := env.Diff(dumpedEnv)
diff := env.Diff(dumpedEnv)

exports := diff.ToShell(Bash)
exports := diff.ToShell(Bash)

_, err = fmt.Println(exports)
if err != nil {
return err
}
_, err = fmt.Println(exports)
if err != nil {
return err
}

return
}),
return
}
4 changes: 2 additions & 2 deletions cmd_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ var CmdCurrent = &Cmd{
Desc: "Reports whether direnv's view of a file is current (or stale)",
Args: []string{"PATH"},
Private: true,
Action: actionSimple(currentCommandFn),
Action: actionSimple(cmdCurrentAction),
}

func currentCommandFn(env Env, args []string) (err error) {
func cmdCurrentAction(env Env, args []string) (err error) {
if len(args) < 2 {
err = errors.New("missing PATH argument")
return
Expand Down
36 changes: 19 additions & 17 deletions cmd_deny.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ import (

// CmdDeny is `direnv deny [PATH_TO_RC]`
var CmdDeny = &Cmd{
Name: "deny",
Desc: "Revokes the authorization of a given .envrc",
Args: []string{"[PATH_TO_RC]"},
Action: actionWithConfig(func(env Env, args []string, config *Config) (err error) {
var rcPath string
Name: "deny",
Desc: "Revokes the authorization of a given .envrc",
Args: []string{"[PATH_TO_RC]"},
Action: actionWithConfig(cmdDenyAction),
}

if len(args) > 1 {
rcPath = args[1]
} else {
if rcPath, err = os.Getwd(); err != nil {
return
}
}
func cmdDenyAction(env Env, args []string, config *Config) (err error) {
var rcPath string

rc := FindRC(rcPath, config)
if rc == nil {
return fmt.Errorf(".envrc file not found")
if len(args) > 1 {
rcPath = args[1]
} else {
if rcPath, err = os.Getwd(); err != nil {
return
}
return rc.Deny()
}),
}

rc := FindRC(rcPath, config)
if rc == nil {
return fmt.Errorf(".envrc file not found")
}
return rc.Deny()
}
66 changes: 34 additions & 32 deletions cmd_dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,40 @@ var CmdDotEnv = &Cmd{
Desc: "Transforms a .env file to evaluatable `export KEY=PAIR` statements",
Args: []string{"[SHELL]", "[PATH_TO_DOTENV]"},
Private: true,
Action: actionSimple(func(env Env, args []string) (err error) {
var shell Shell
var newenv Env
var target string

if len(args) > 1 {
shell = DetectShell(args[1])
} else {
shell = Bash
}

if len(args) > 2 {
target = args[2]
}

if target == "" {
target = ".env"
}

var data []byte
if data, err = ioutil.ReadFile(target); err != nil {
return
}

newenv, err = dotenv.Parse(string(data))
if err != nil {
return err
}

str := newenv.ToShell(shell)
fmt.Println(str)
Action: actionSimple(cmdDotEnvAction),
}

func cmdDotEnvAction(env Env, args []string) (err error) {
var shell Shell
var newenv Env
var target string

if len(args) > 1 {
shell = DetectShell(args[1])
} else {
shell = Bash
}

if len(args) > 2 {
target = args[2]
}

if target == "" {
target = ".env"
}

var data []byte
if data, err = ioutil.ReadFile(target); err != nil {
return
}),
}

newenv, err = dotenv.Parse(string(data))
if err != nil {
return err
}

str := newenv.ToShell(shell)
fmt.Println(str)

return
}
54 changes: 28 additions & 26 deletions cmd_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,37 @@ var CmdDump = &Cmd{
Desc: "Used to export the inner bash state at the end of execution",
Args: []string{"[SHELL]", "[FILE]"},
Private: true,
Action: actionSimple(func(env Env, args []string) (err error) {
target := "gzenv"
w := os.Stdout

if len(args) > 1 {
target = args[1]
}

var filePath string
if len(args) > 2 {
filePath = args[2]
} else {
filePath = os.Getenv("DIRENV_DUMP_FILE_PATH")
}
Action: actionSimple(cmdDumpAction),
}

if filePath != "" {
w, err = os.OpenFile(filePath, os.O_WRONLY, 0666)
if err != nil {
return err
}
func cmdDumpAction(env Env, args []string) (err error) {
target := "gzenv"
w := os.Stdout

if len(args) > 1 {
target = args[1]
}

var filePath string
if len(args) > 2 {
filePath = args[2]
} else {
filePath = os.Getenv("DIRENV_DUMP_FILE_PATH")
}

if filePath != "" {
w, err = os.OpenFile(filePath, os.O_WRONLY, 0666)
if err != nil {
return err
}
}

shell := DetectShell(target)
if shell == nil {
return fmt.Errorf("unknown target shell '%s'", target)
}
shell := DetectShell(target)
if shell == nil {
return fmt.Errorf("unknown target shell '%s'", target)
}

_, err = fmt.Fprintln(w, shell.Dump(env))
_, err = fmt.Fprintln(w, shell.Dump(env))

return
}),
return
}
Loading

0 comments on commit e1b73f4

Please sign in to comment.