Skip to content

Commit

Permalink
Avoid using filepath.Dir function
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Jan 3, 2024
1 parent 2a84396 commit 2a971d8
Show file tree
Hide file tree
Showing 4 changed files with 68 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,13 +4,13 @@ import (
"bytes"
"context"
"fmt"
"path/filepath"
"time"

"github.com/k0sproject/dig"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
"github.com/k0sproject/k0sctl/pkg/node"
"github.com/k0sproject/k0sctl/pkg/paths"
"github.com/k0sproject/k0sctl/pkg/retry"
"github.com/k0sproject/rig/exec"
"github.com/k0sproject/version"
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 := paths.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
4 changes: 2 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,6 @@ package cluster
import (
"fmt"
gos "os"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/go-playground/validator/v10"
"github.com/jellydator/validation"
"github.com/jellydator/validation/is"
"github.com/k0sproject/k0sctl/pkg/paths"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/exec"
"github.com/k0sproject/rig/os"
Expand Down Expand Up @@ -377,7 +377,7 @@ func (h *Host) InstallK0sBinary(path string) error {
return fmt.Errorf("k0s binary tempfile not found")
}

dir := filepath.Dir(h.Configurer.K0sBinaryPath())
dir := paths.Dir(h.Configurer.K0sBinaryPath())
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
31 changes: 31 additions & 0 deletions pkg/paths/dirname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Package paths contains utility functions for handling file paths independently from the local OS unlike the path/filepath package.
package paths

import (
"regexp"
"strings"
)

var multipleSlashesRegex = regexp.MustCompile("/{2,}")

func Dir(path string) string {
// Normalize path by replacing multiple slashes with a single slash
normalizedPath := multipleSlashesRegex.ReplaceAllString(path, "/")

// Your existing logic here
if normalizedPath == "/" {
return "/"
}
if strings.HasSuffix(normalizedPath, "/") {
return strings.TrimSuffix(normalizedPath, "/")
}
idx := strings.LastIndex(normalizedPath, "/")
switch idx {
case 0:
return "/"
case -1:
return "."
default:
return normalizedPath[:idx]
}
}
33 changes: 33 additions & 0 deletions pkg/paths/dirname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package paths

import (
"testing"
)

func TestDir(t *testing.T) {
testCases := []struct {
path string
expected string
}{
{"/usr/local/bin", "/usr/local"},
{"/usr/local/bin/", "/usr/local/bin"},
{"usr/local/bin/", "usr/local/bin"},
{"usr/local/bin", "usr/local"},
{"/usr", "/"},
{"usr", "."},
{"/", "/"},
{"", "."},
{"filename.txt", "."},
{"./file.txt", "."},
{"../sibling.txt", ".."},
}

for _, tc := range testCases {
t.Run(tc.path, func(t *testing.T) {
result := Dir(tc.path)
if result != tc.expected {
t.Errorf("Dir(%q) = %q, want %q", tc.path, result, tc.expected)
}
})
}
}

0 comments on commit 2a971d8

Please sign in to comment.