From 505456a5ad6bf7090d85bcd5c05e14eb172e6319 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Wed, 3 Jan 2024 10:41:47 +0200 Subject: [PATCH] Just use stdlib "path.Dir" Signed-off-by: Kimmo Lehto --- phase/configure_k0s.go | 4 +-- .../v1beta1/cluster/host.go | 4 +-- pkg/paths/dirname.go | 32 ------------------ pkg/paths/dirname_test.go | 33 ------------------- 4 files changed, 4 insertions(+), 69 deletions(-) delete mode 100644 pkg/paths/dirname.go delete mode 100644 pkg/paths/dirname_test.go diff --git a/phase/configure_k0s.go b/phase/configure_k0s.go index c705bba9..e131322f 100644 --- a/phase/configure_k0s.go +++ b/phase/configure_k0s.go @@ -4,13 +4,13 @@ import ( "bytes" "context" "fmt" + gopath "path" "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" @@ -241,7 +241,7 @@ func (p *ConfigureK0s) configureK0s(h *cluster.Host) error { log.Infof("%s: installing new configuration", h) configPath := h.K0sConfigPath() - configDir := paths.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 892e446b..b9f5a55c 100644 --- a/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go +++ b/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go @@ -3,6 +3,7 @@ package cluster import ( "fmt" gos "os" + gopath "path" "regexp" "strconv" "strings" @@ -13,7 +14,6 @@ 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" @@ -377,7 +377,7 @@ func (h *Host) InstallK0sBinary(path string) error { return fmt.Errorf("k0s binary tempfile not found") } - dir := paths.Dir(h.Configurer.K0sBinaryPath()) + dir := gopath.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) } diff --git a/pkg/paths/dirname.go b/pkg/paths/dirname.go deleted file mode 100644 index 96eae5f0..00000000 --- a/pkg/paths/dirname.go +++ /dev/null @@ -1,32 +0,0 @@ -// 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,}") - -// Dir is like filepath.Dir but it doesn't use the local OS path separator. -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] - } -} diff --git a/pkg/paths/dirname_test.go b/pkg/paths/dirname_test.go deleted file mode 100644 index 8b19d8a4..00000000 --- a/pkg/paths/dirname_test.go +++ /dev/null @@ -1,33 +0,0 @@ -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) - } - }) - } -}