Skip to content

Commit

Permalink
golangci-lint: add to the mix and fix all warnings
Browse files Browse the repository at this point in the history
Some notable changes:
* documented all the public interfaces
* added a few missing error handlings
* merged the existing gofmt check into golangci
* fixed the bash and tcsh escaping. Hopefully it doesn't break anything.
  • Loading branch information
zimbatm committed Jan 18, 2020
1 parent 6534ed7 commit 219d9f7
Show file tree
Hide file tree
Showing 43 changed files with 324 additions and 220 deletions.
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
linters:
presets:
- bugs
- unused
enable:
- gofmt
- golint
- misspell
- stylecheck
7 changes: 4 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ man: $(roffs)
############################################################################

tests = \
test-shellcheck \
test-shellcheck \
test-go \
test-go-lint \
test-go-fmt \
test-bash \
test-elvish \
Expand All @@ -120,8 +121,8 @@ test: build $(tests)
test-go:
$(GO) test -v ./...

test-go-fmt:
[ $$($(GO) fmt | go run script/both/main.go | wc -l) = 0 ]
test-go-lint:
golangci-lint run

test-shellcheck:
shellcheck stdlib.sh
Expand Down
2 changes: 1 addition & 1 deletion cmd_allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
)

// `direnv allow [PATH_TO_RC]`
// CmdAllow is `direnv allow [PATH_TO_RC]`
var CmdAllow = &Cmd{
Name: "allow",
Desc: "Grants direnv to load the given .envrc",
Expand Down
4 changes: 2 additions & 2 deletions cmd_apply_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io/ioutil"
)

// `direnv apply_dump FILE`
// CmdApplyDump is `direnv apply_dump FILE`
var CmdApplyDump = &Cmd{
Name: "apply_dump",
Desc: "Accepts a filename containing `direnv dump` output and generates a series of bash export statements to apply the given env",
Expand Down Expand Up @@ -33,7 +33,7 @@ var CmdApplyDump = &Cmd{

diff := env.Diff(dumpedEnv)

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

_, err = fmt.Println(exports)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion cmd_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
)

// CmdCurrent is `direnv current`
var CmdCurrent = &Cmd{
Name: "current",
Desc: "Reports whether direnv's view of a file is current (or stale)",
Expand All @@ -22,7 +23,10 @@ func currentCommandFn(env Env, args []string) (err error) {
watches := NewFileTimes()
watchString, ok := env[DIRENV_WATCHES]
if ok {
watches.Unmarshal(watchString)
err = watches.Unmarshal(watchString)
if err != nil {
return
}
}

err = watches.CheckOne(path)
Expand Down
2 changes: 1 addition & 1 deletion cmd_deny.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
)

// `direnv deny [PATH_TO_RC]`
// CmdDeny is `direnv deny [PATH_TO_RC]`
var CmdDeny = &Cmd{
Name: "deny",
Desc: "Revokes the authorization of a given .envrc",
Expand Down
4 changes: 2 additions & 2 deletions cmd_dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/direnv/go-dotenv"
)

// `direnv private dotenv [SHELL [PATH_TO_DOTENV]]`
// CmdDotEnv is `direnv dotenv [SHELL [PATH_TO_DOTENV]]`
// Transforms a .env file to evaluatable `export KEY=PAIR` statements.
//
// See: https://github.com/bkeepers/dotenv and
Expand All @@ -25,7 +25,7 @@ var CmdDotEnv = &Cmd{
if len(args) > 1 {
shell = DetectShell(args[1])
} else {
shell = BASH
shell = Bash
}

if len(args) > 2 {
Expand Down
17 changes: 9 additions & 8 deletions cmd_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
)

// `direnv edit [PATH_TO_RC]`
// CmdEdit is `direnv edit [PATH_TO_RC]`
var CmdEdit = &Cmd{
Name: "edit",
Desc: `Opens PATH_TO_RC or the current .envrc into an $EDITOR and allow
Expand Down Expand Up @@ -43,7 +43,7 @@ var CmdEdit = &Cmd{

editor := env["EDITOR"]
if editor == "" {
log_error("$EDITOR not found.")
logError("$EDITOR not found.")
editor = detectEditor(env["PATH"])
if editor == "" {
err = fmt.Errorf("could not find a default editor in the PATH")
Expand All @@ -62,13 +62,13 @@ var CmdEdit = &Cmd{
}

foundRC = FindRC(rcPath, config)
log_debug("foundRC: %#v", foundRC)
log_debug("times: %#v", times)
logDebug("foundRC: %#v", foundRC)
logDebug("times: %#v", times)
if times != nil {
log_debug("times.Check(): %#v", times.Check())
logDebug("times.Check(): %#v", times.Check())
}
if foundRC != nil && (times == nil || times.Check() != nil) {
foundRC.Allow()
err = foundRC.Allow()
}

return
Expand All @@ -77,7 +77,8 @@ var CmdEdit = &Cmd{

// Utils

var EDITORS = [][]string{
// Editors contains a list of known editors and how to start them.
var Editors = [][]string{
{"subl", "-w"},
{"mate", "-w"},
{"open", "-t", "-W"}, // Opens with the default text editor on mac
Expand All @@ -87,7 +88,7 @@ var EDITORS = [][]string{
}

func detectEditor(pathenv string) string {
for _, editor := range EDITORS {
for _, editor := range Editors {
if _, err := lookPath(editor[0], pathenv); err == nil {
return strings.Join(editor, " ")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"syscall"
)

// `direnv exec DIR <COMMAND> ...`
// CmdExec is `direnv exec DIR <COMMAND> ...`
var CmdExec = &Cmd{
Name: "exec",
Desc: "Executes a command after loading the first .envrc found in DIR",
Expand Down
7 changes: 5 additions & 2 deletions cmd_expand_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func expandPath(path, relTo string) string {
return filepath.Clean(filepath.Join(relTo, path))
}

// `direnv expand_path PATH [REL_TO]`
// CmdExpandPath is `direnv expand_path PATH [REL_TO]`
var CmdExpandPath = &Cmd{
Name: "expand_path",
Desc: "Transforms a PATH to an absolute path to REL_TO or $PWD",
Expand All @@ -24,7 +24,10 @@ var CmdExpandPath = &Cmd{
var path string

flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
flagset.Parse(args[1:])
err = flagset.Parse(args[1:])
if err != nil {
return err
}

path = flagset.Arg(0)
if path == "" {
Expand Down
41 changes: 21 additions & 20 deletions cmd_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

// ExportContext is a sort of state holder struct that is being used to
// record changes before the export finishes.
type ExportContext struct {
config *Config
foundRC *RC
Expand Down Expand Up @@ -39,25 +41,25 @@ func (context *ExportContext) updateRC() (err error) {

context.oldEnv = backupDiff.Reverse().Patch(context.env)

log_debug("Determining action:")
log_debug("foundRC: %#v", context.foundRC)
log_debug("loadedRC: %#v", context.loadedRC)
logDebug("Determining action:")
logDebug("foundRC: %#v", context.foundRC)
logDebug("loadedRC: %#v", context.loadedRC)

switch {
case context.foundRC == nil:
log_debug("no RC found, unloading")
err = context.unloadEnv()
logDebug("no RC found, unloading")
context.unloadEnv()
case context.loadedRC == nil:
log_debug("no RC (implies no DIRENV_DIFF),loading")
logDebug("no RC (implies no DIRENV_DIFF),loading")
err = context.loadRC()
case context.loadedRC.path != context.foundRC.path:
log_debug("new RC, loading")
logDebug("new RC, loading")
err = context.loadRC()
case context.loadedRC.times.Check() != nil:
log_debug("file changed, reloading")
logDebug("file changed, reloading")
err = context.loadRC()
default:
log_debug("no update needed")
logDebug("no update needed")
}

return
Expand All @@ -68,11 +70,10 @@ func (context *ExportContext) loadRC() (err error) {
return
}

func (context *ExportContext) unloadEnv() (err error) {
log_status(context.env, "unloading")
func (context *ExportContext) unloadEnv() {
logStatus(context.env, "unloading")
context.newEnv = context.oldEnv.Copy()
cleanEnv(context.newEnv)
return
}

func cleanEnv(env Env) {
Expand Down Expand Up @@ -104,7 +105,7 @@ func (context *ExportContext) diffString(shell Shell) string {

sort.Strings(out)
if len(out) > 0 {
log_status(context.env, "export %s", strings.Join(out, " "))
logStatus(context.env, "export %s", strings.Join(out, " "))
}
}

Expand All @@ -115,7 +116,7 @@ func (context *ExportContext) diffString(shell Shell) string {
func exportCommand(env Env, args []string, config *Config) (err error) {
defer log.SetPrefix(log.Prefix())
log.SetPrefix(log.Prefix() + "export:")
log_debug("start")
logDebug("start")
context := ExportContext{
env: env,
config: config,
Expand All @@ -132,29 +133,29 @@ func exportCommand(env Env, args []string, config *Config) (err error) {
return fmt.Errorf("unknown target shell '%s'", target)
}

log_debug("loading RCs")
logDebug("loading RCs")
if context.getRCs(); !context.hasRC() {
return nil
}

log_debug("updating RC")
logDebug("updating RC")
if err = context.updateRC(); err != nil {
log_debug("err: %v", err)
logDebug("err: %v", err)
}

if context.newEnv == nil {
log_debug("newEnv nil, exiting")
logDebug("newEnv nil, exiting")
return
}

diffString := context.diffString(shell)
log_debug("env diff %s", diffString)
logDebug("env diff %s", diffString)
fmt.Print(diffString)

return
}

// `direnv export $0`
// CmdExport is `direnv export $0`
var CmdExport = &Cmd{
Name: "export",
Desc: "loads an .envrc and prints the diff in terms of exports",
Expand Down
2 changes: 1 addition & 1 deletion cmd_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

// `direnv help`
// CmdHelp is `direnv help`
var CmdHelp = &Cmd{
Name: "help",
Desc: "shows this help",
Expand Down
2 changes: 1 addition & 1 deletion cmd_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type HookContext struct {
SelfPath string
}

// `direnv hook $0`
// CmdHook is `direnv hook $0`
var CmdHook = &Cmd{
Name: "hook",
Desc: "Used to setup the shell hook",
Expand Down
13 changes: 7 additions & 6 deletions cmd_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"strings"
)

// CmdPrune is `direnv prune`
var CmdPrune = &Cmd{
Name: "prune",
Desc: "removes old allowed files",
Action: actionWithConfig(func(env Env, args []string, config *Config) (err error) {
var dir *os.File
var fi os.FileInfo
var dir_list []string
var dirList []string
var envrc []byte

allowed := config.AllowDir()
Expand All @@ -22,11 +23,11 @@ var CmdPrune = &Cmd{
}
defer dir.Close()

if dir_list, err = dir.Readdirnames(0); err != nil {
if dirList, err = dir.Readdirnames(0); err != nil {
return err
}

for _, hash := range dir_list {
for _, hash := range dirList {
filename := path.Join(allowed, hash)
if fi, err = os.Stat(filename); err != nil {
return err
Expand All @@ -36,13 +37,13 @@ var CmdPrune = &Cmd{
if envrc, err = ioutil.ReadFile(filename); err != nil {
return err
}
envrc_str := strings.TrimSpace(string(envrc))
envrcStr := strings.TrimSpace(string(envrc))

// skip old files, w/o path inside
if envrc_str == "" {
if envrcStr == "" {
continue
}
if !fileExists(envrc_str) {
if !fileExists(envrcStr) {
_ = os.Remove(filename)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd_show_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/direnv/direnv/gzenv"
)

// `direnv show_dump`
// CmdShowDump is `direnv show_dump`
var CmdShowDump = &Cmd{
Name: "show_dump",
Desc: "Show the data inside of a dump for debugging purposes",
Expand Down
2 changes: 1 addition & 1 deletion cmd_stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

// `direnv stdlib`
// CmdStdlib is `direnv stdlib`
var CmdStdlib = &Cmd{
Name: "stdlib",
Desc: "Displays the stdlib available in the .envrc execution context",
Expand Down
Loading

0 comments on commit 219d9f7

Please sign in to comment.