From 966570ce6c4fa7eae8fbcb9a97b56e63f52a37e4 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 9 Oct 2023 21:47:23 +1100 Subject: [PATCH 1/4] Forward env vars to commands We want to forward env vars to commands just because it's good practice, and probably enables things that previously weren't possible. --- pkg/commands/container.go | 2 +- pkg/commands/os.go | 17 +++++++++-------- pkg/commands/os_test.go | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/pkg/commands/container.go b/pkg/commands/container.go index 56aaa1e..b45feef 100644 --- a/pkg/commands/container.go +++ b/pkg/commands/container.go @@ -100,7 +100,7 @@ func (c *Container) Attach() (*exec.Cmd, error) { c.Log.Warn(fmt.Sprintf("attaching to container %s", c.Name)) // TODO: use SDK - cmd := c.OSCommand.PrepareSubProcess("docker", "attach", "--sig-proxy=false", c.ID) + cmd := c.OSCommand.NewCmd("docker", "attach", "--sig-proxy=false", c.ID) return cmd, nil } diff --git a/pkg/commands/os.go b/pkg/commands/os.go index a94acc3..259f782 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -87,7 +87,7 @@ func (c *OSCommand) RunExecutable(cmd *exec.Cmd) error { // ExecutableFromString takes a string like `docker ps -a` and returns an executable command for it func (c *OSCommand) ExecutableFromString(commandStr string) *exec.Cmd { splitCmd := str.ToArgv(commandStr) - return c.command(splitCmd[0], splitCmd[1:]...) + return c.NewCmd(splitCmd[0], splitCmd[1:]...) } // Same as ExecutableFromString but cancellable via a context @@ -96,6 +96,12 @@ func (c *OSCommand) ExecutableFromStringContext(ctx context.Context, commandStr return exec.CommandContext(ctx, splitCmd[0], splitCmd[1:]...) } +func (c *OSCommand) NewCmd(cmdName string, commandArgs ...string) *exec.Cmd { + cmd := c.command(cmdName, commandArgs...) + cmd.Env = os.Environ() + return cmd +} + func (c *OSCommand) NewCommandStringWithShell(commandStr string) string { var quotedCommand string // Windows does not seem to like quotes around the command @@ -187,12 +193,7 @@ func (c *OSCommand) EditFile(filename string) (*exec.Cmd, error) { return nil, errors.New("No editor defined in $VISUAL or $EDITOR") } - return c.PrepareSubProcess(editor, filename), nil -} - -// PrepareSubProcess iniPrepareSubProcessrocess then tells the Gui to switch to it -func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *exec.Cmd { - return c.command(cmdName, commandArgs...) + return c.NewCmd(editor, filename), nil } // Quote wraps a message in platform-specific quotation marks @@ -301,7 +302,7 @@ func (c *OSCommand) GetLazydockerPath() string { // RunCustomCommand returns the pointer to a custom command func (c *OSCommand) RunCustomCommand(command string) *exec.Cmd { - return c.PrepareSubProcess(c.Platform.shell, c.Platform.shellArg, command) + return c.NewCmd(c.Platform.shell, c.Platform.shellArg, command) } // PipeCommands runs a heap of commands and pipes their inputs/outputs together like A | B | C diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go index 978844d..92be790 100644 --- a/pkg/commands/os_test.go +++ b/pkg/commands/os_test.go @@ -90,7 +90,7 @@ func TestOSCommandEditFile(t *testing.T) { assert.EqualValues(t, "nano", name) - return nil + return exec.Command("exit", "0") }, func(env string) string { if env == "VISUAL" { @@ -112,7 +112,7 @@ func TestOSCommandEditFile(t *testing.T) { assert.EqualValues(t, "emacs", name) - return nil + return exec.Command("exit", "0") }, func(env string) string { if env == "EDITOR" { @@ -134,7 +134,7 @@ func TestOSCommandEditFile(t *testing.T) { assert.EqualValues(t, "vi", name) - return nil + return exec.Command("exit", "0") }, func(env string) string { return "" From e69449c8242dd27628175174e5b6bd361f190229 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 9 Oct 2023 21:58:17 +1100 Subject: [PATCH 2/4] Use go 1.20 --- .github/workflows/cd.yml | 2 +- .github/workflows/ci.yml | 10 +++++----- .golangci.yml | 2 +- Dockerfile | 2 +- go.mod | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index a52d129..a94b173 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -16,7 +16,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v1 with: - go-version: 1.18.x + go-version: 1.20.x - name: Run goreleaser uses: goreleaser/goreleaser-action@v1 with: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7ffbda5..e85f70a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,7 @@ name: Continuous Integration env: - GO_VERSION: 1.18 + GO_VERSION: 1.20 on: push: @@ -27,7 +27,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v1 with: - go-version: 1.18.x + go-version: 1.20.x - name: Cache build uses: actions/cache@v1 with: @@ -49,7 +49,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v1 with: - go-version: 1.18.x + go-version: 1.20.x - name: Cache build uses: actions/cache@v1 with: @@ -77,7 +77,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v1 with: - go-version: 1.18.x + go-version: 1.20.x - name: Cache build uses: actions/cache@v1 with: @@ -104,7 +104,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v1 with: - go-version: 1.18.x + go-version: 1.20.x - name: Cache build uses: actions/cache@v1 with: diff --git a/.golangci.yml b/.golangci.yml index de90dc5..219a97c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,4 +26,4 @@ linters-settings: max-func-lines: 0 run: - go: 1.18 + go: '1.20' diff --git a/Dockerfile b/Dockerfile index a3506d7..9719593 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ ARG BASE_IMAGE_BUILDER=golang ARG ALPINE_VERSION=3.15 -ARG GO_VERSION=1.18 +ARG GO_VERSION=1.20 FROM ${BASE_IMAGE_BUILDER}:${GO_VERSION}-alpine${ALPINE_VERSION} AS builder ARG GOARCH=amd64 diff --git a/go.mod b/go.mod index 530df30..419f715 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/jesseduffield/lazydocker -go 1.18 +go 1.20 require ( github.com/OpenPeeDeeP/xdg v0.2.1-0.20190312153938-4ba9e1eb294c From 39e694f496979f323685515a417077d70a9633a3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 9 Oct 2023 21:58:36 +1100 Subject: [PATCH 3/4] Add timeout to go linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 219a97c..d9c6779 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -27,3 +27,4 @@ linters-settings: run: go: '1.20' + timeout: 10m From 2dda39dff03859b145b0c229a15cf287eb1f495d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 9 Oct 2023 22:02:53 +1100 Subject: [PATCH 4/4] Appease linter --- pkg/commands/dummies.go | 4 ++-- pkg/commands/os.go | 6 +++--- pkg/commands/os_test.go | 3 +-- pkg/commands/ssh/ssh.go | 3 +-- pkg/config/app_config.go | 3 +-- pkg/gui/subprocess.go | 6 +++--- pkg/log/log.go | 4 ++-- 7 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pkg/commands/dummies.go b/pkg/commands/dummies.go index 416a57d..552171a 100644 --- a/pkg/commands/dummies.go +++ b/pkg/commands/dummies.go @@ -1,7 +1,7 @@ package commands import ( - "io/ioutil" + "io" "github.com/jesseduffield/lazydocker/pkg/config" "github.com/jesseduffield/lazydocker/pkg/i18n" @@ -31,7 +31,7 @@ func NewDummyAppConfig() *config.AppConfig { // NewDummyLog creates a new dummy Log for testing func NewDummyLog() *logrus.Entry { log := logrus.New() - log.Out = ioutil.Discard + log.Out = io.Discard return log.WithField("test", "test") } diff --git a/pkg/commands/os.go b/pkg/commands/os.go index 259f782..b8dec4a 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -3,7 +3,7 @@ package commands import ( "context" "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -240,7 +240,7 @@ func (c *OSCommand) AppendLineToFile(filename, line string) error { // CreateTempFile writes a string to a new temp file and returns the file's name func (c *OSCommand) CreateTempFile(filename, content string) (string, error) { - tmpfile, err := ioutil.TempFile("", filename) + tmpfile, err := os.CreateTemp("", filename) if err != nil { c.Log.Error(err) return "", WrapError(err) @@ -342,7 +342,7 @@ func (c *OSCommand) PipeCommands(commandStrings ...string) error { c.Log.Error(err) } - if b, err := ioutil.ReadAll(stderr); err == nil { + if b, err := io.ReadAll(stderr); err == nil { if len(b) > 0 { finalErrors = append(finalErrors, string(b)) } diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go index 92be790..3e036db 100644 --- a/pkg/commands/os_test.go +++ b/pkg/commands/os_test.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "io/ioutil" "os" "os/exec" "testing" @@ -290,7 +289,7 @@ func TestOSCommandCreateTempFile(t *testing.T) { func(path string, err error) { assert.NoError(t, err) - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) assert.NoError(t, err) assert.Equal(t, "content", string(content)) diff --git a/pkg/commands/ssh/ssh.go b/pkg/commands/ssh/ssh.go index 9f57268..ecc8404 100644 --- a/pkg/commands/ssh/ssh.go +++ b/pkg/commands/ssh/ssh.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net" "net/url" "os" @@ -37,7 +36,7 @@ func NewSSHHandler(oSCommand CmdKiller) *SSHHandler { return (&net.Dialer{}).DialContext(ctx, network, addr) }, startCmd: func(cmd *exec.Cmd) error { return cmd.Start() }, - tempDir: ioutil.TempDir, + tempDir: os.MkdirTemp, getenv: os.Getenv, setenv: os.Setenv, } diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index f3a47db..d35a244 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -13,7 +13,6 @@ package config import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -569,7 +568,7 @@ func loadUserConfig(configDir string, base *UserConfig) (*UserConfig, error) { } } - content, err := ioutil.ReadFile(fileName) + content, err := os.ReadFile(fileName) if err != nil { return nil, err } diff --git a/pkg/gui/subprocess.go b/pkg/gui/subprocess.go index 6200339..b41ac47 100644 --- a/pkg/gui/subprocess.go +++ b/pkg/gui/subprocess.go @@ -2,7 +2,7 @@ package gui import ( "fmt" - "io/ioutil" + "io" "os" "os/exec" "os/signal" @@ -65,8 +65,8 @@ func (gui *Gui) runCommand(cmd *exec.Cmd, msg string) { } cmd.Stdin = nil - cmd.Stdout = ioutil.Discard - cmd.Stderr = ioutil.Discard + cmd.Stdout = io.Discard + cmd.Stderr = io.Discard gui.promptToReturn() } diff --git a/pkg/log/log.go b/pkg/log/log.go index e80cadc..232a7bb 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -2,7 +2,7 @@ package log import ( "fmt" - "io/ioutil" + "io" "os" "path/filepath" @@ -54,7 +54,7 @@ func newDevelopmentLogger(config *config.AppConfig) *logrus.Logger { func newProductionLogger() *logrus.Logger { log := logrus.New() - log.Out = ioutil.Discard + log.Out = io.Discard log.SetLevel(logrus.ErrorLevel) return log }