Skip to content

Commit

Permalink
use os-agnostic base function for configmaps and secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Jul 7, 2021
1 parent 3b361e1 commit 88f6d43
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
30 changes: 29 additions & 1 deletion api/internal/utils/pathsplitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package utils

import "strings"
import (
"os"
"strings"
)

// TODO: Move these to kyaml

Expand Down Expand Up @@ -62,3 +65,28 @@ func SmarterPathSplitter(path string, delimiter string) []string {
}
return result
}

// This function is the same as golang's builtin path.Base, except that
// this is os-agnostic.
// Base returns the last element of path.
// Trailing slashes are removed before extracting the last element.
// If the path is empty, Base returns ".".
// If the path consists entirely of slashes, Base returns "/".
func Base(path string) string {
if path == "" {
return "."
}
// Strip trailing slashes.
for len(path) > 0 && path[len(path)-1] == os.PathSeparator {
path = path[0 : len(path)-1]
}
// Find the last element
if i := strings.LastIndex(path, string(os.PathSeparator)); i >= 0 {
path = path[i+1:]
}
// If empty now, it had only slashes.
if path == "" {
return string(os.PathSeparator)
}
return path
}
4 changes: 2 additions & 2 deletions api/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"bytes"
"fmt"
"os"
"path"
"strings"
"unicode"
"unicode/utf8"

"github.com/pkg/errors"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/internal/utils"
"sigs.k8s.io/kustomize/api/types"
)

Expand Down Expand Up @@ -183,7 +183,7 @@ func parseFileSource(source string) (keyName, filePath string, err error) {
numSeparators := strings.Count(source, "=")
switch {
case numSeparators == 0:
return path.Base(source), source, nil
return utils.Base(source), source, nil
case numSeparators == 1 && strings.HasPrefix(source, "="):
return "", "", fmt.Errorf("key name for file path %v missing", strings.TrimPrefix(source, "="))
case numSeparators == 1 && strings.HasSuffix(source, "="):
Expand Down

0 comments on commit 88f6d43

Please sign in to comment.