Skip to content

Commit

Permalink
Avoid using filepath.Dir function (#616)
Browse files Browse the repository at this point in the history
* Avoid using filepath.Dir function

Signed-off-by: Kimmo Lehto <[email protected]>

* Just use stdlib "path.Dir"

Signed-off-by: Kimmo Lehto <[email protected]>

* Add a test

Signed-off-by: Kimmo Lehto <[email protected]>

---------

Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Jan 4, 2024
1 parent 9e799de commit 3dcf3f0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions phase/configure_k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"path/filepath"
gopath "path"
"time"

"github.com/k0sproject/dig"
Expand Down Expand Up @@ -241,7 +241,7 @@ func (p *ConfigureK0s) configureK0s(h *cluster.Host) error {

log.Infof("%s: installing new configuration", h)
configPath := h.K0sConfigPath()
configDir := filepath.Dir(configPath)
configDir := gopath.Dir(configPath)

if !h.Configurer.FileExist(h, configDir) {
if err := h.Execf(`install -d 0750 -o root -g root "%s"`, configDir, exec.Sudo(h)); err != nil {
Expand Down
8 changes: 6 additions & 2 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cluster
import (
"fmt"
gos "os"
"path/filepath"
gopath "path"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -371,13 +371,17 @@ func (h *Host) K0sServiceName() string {
}
}

func (h *Host) k0sBinaryPathDir() string {
return gopath.Dir(h.Configurer.K0sBinaryPath())
}

// InstallK0sBinary installs the k0s binary from the provided file path to K0sBinaryPath
func (h *Host) InstallK0sBinary(path string) error {
if !h.Configurer.FileExist(h, path) {
return fmt.Errorf("k0s binary tempfile not found")
}

dir := filepath.Dir(h.Configurer.K0sBinaryPath())
dir := h.k0sBinaryPathDir()
if err := h.Execf(`install -m 0755 -o root -g root -d "%s"`, dir, exec.Sudo(h)); err != nil {
return fmt.Errorf("create k0s binary dir: %w", err)
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,10 @@ func TestValidation(t *testing.T) {
require.ErrorContains(t, h.Validate(), "unbalanced quotes")
})
}

func TestBinaryPath(t *testing.T) {
h := Host{}
h.Configurer = &mockconfigurer{}
h.Configurer.SetPath("K0sBinaryPath", "/foo/bar/k0s")
require.Equal(t, "/foo/bar", h.k0sBinaryPathDir())
}

0 comments on commit 3dcf3f0

Please sign in to comment.