Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using filepath.Dir function #616

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}