Skip to content

Commit 3dcf3f0

Browse files
authored
Avoid using filepath.Dir function (#616)
* 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]>
1 parent 9e799de commit 3dcf3f0

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

phase/configure_k0s.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"path/filepath"
7+
gopath "path"
88
"time"
99

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

242242
log.Infof("%s: installing new configuration", h)
243243
configPath := h.K0sConfigPath()
244-
configDir := filepath.Dir(configPath)
244+
configDir := gopath.Dir(configPath)
245245

246246
if !h.Configurer.FileExist(h, configDir) {
247247
if err := h.Execf(`install -d 0750 -o root -g root "%s"`, configDir, exec.Sudo(h)); err != nil {

pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cluster
33
import (
44
"fmt"
55
gos "os"
6-
"path/filepath"
6+
gopath "path"
77
"regexp"
88
"strconv"
99
"strings"
@@ -371,13 +371,17 @@ func (h *Host) K0sServiceName() string {
371371
}
372372
}
373373

374+
func (h *Host) k0sBinaryPathDir() string {
375+
return gopath.Dir(h.Configurer.K0sBinaryPath())
376+
}
377+
374378
// InstallK0sBinary installs the k0s binary from the provided file path to K0sBinaryPath
375379
func (h *Host) InstallK0sBinary(path string) error {
376380
if !h.Configurer.FileExist(h, path) {
377381
return fmt.Errorf("k0s binary tempfile not found")
378382
}
379383

380-
dir := filepath.Dir(h.Configurer.K0sBinaryPath())
384+
dir := h.k0sBinaryPathDir()
381385
if err := h.Execf(`install -m 0755 -o root -g root -d "%s"`, dir, exec.Sudo(h)); err != nil {
382386
return fmt.Errorf("create k0s binary dir: %w", err)
383387
}

pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,10 @@ func TestValidation(t *testing.T) {
134134
require.ErrorContains(t, h.Validate(), "unbalanced quotes")
135135
})
136136
}
137+
138+
func TestBinaryPath(t *testing.T) {
139+
h := Host{}
140+
h.Configurer = &mockconfigurer{}
141+
h.Configurer.SetPath("K0sBinaryPath", "/foo/bar/k0s")
142+
require.Equal(t, "/foo/bar", h.k0sBinaryPathDir())
143+
}

0 commit comments

Comments
 (0)