From 3dcf3f044d267945acd664a9954dc7325099ff11 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 4 Jan 2024 09:40:08 +0200 Subject: [PATCH] Avoid using filepath.Dir function (#616) * Avoid using filepath.Dir function Signed-off-by: Kimmo Lehto * Just use stdlib "path.Dir" Signed-off-by: Kimmo Lehto * Add a test Signed-off-by: Kimmo Lehto --------- Signed-off-by: Kimmo Lehto --- phase/configure_k0s.go | 4 ++-- pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go | 8 ++++++-- .../k0sctl.k0sproject.io/v1beta1/cluster/host_test.go | 7 +++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/phase/configure_k0s.go b/phase/configure_k0s.go index a088a7e7..e131322f 100644 --- a/phase/configure_k0s.go +++ b/phase/configure_k0s.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "path/filepath" + gopath "path" "time" "github.com/k0sproject/dig" @@ -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 { diff --git a/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go b/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go index 20341a00..a10f56ad 100644 --- a/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go +++ b/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go @@ -3,7 +3,7 @@ package cluster import ( "fmt" gos "os" - "path/filepath" + gopath "path" "regexp" "strconv" "strings" @@ -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) } diff --git a/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host_test.go b/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host_test.go index 812681ba..7758c4dd 100644 --- a/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host_test.go +++ b/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host_test.go @@ -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()) +}