Skip to content

Commit

Permalink
Break up main.go
Browse files Browse the repository at this point in the history
Add new files for abspath, credential, and env functions.
  • Loading branch information
thockin committed Oct 13, 2023
1 parent ff51ca9 commit 8fa652d
Show file tree
Hide file tree
Showing 7 changed files with 833 additions and 717 deletions.
89 changes: 89 additions & 0 deletions abspath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"os"
"path/filepath"
"strings"
)

// absPath is an absolute path string. This type is intended to make it clear
// when strings are absolute paths vs something else. This does not verify or
// mutate the input, so careless callers could make instances of this type that
// are not actually absolute paths, or even "".
type absPath string

// String returns abs as a string.
func (abs absPath) String() string {
return string(abs)
}

// Canonical returns a canonicalized form of abs, similar to filepath.Abs
// (including filepath.Clean). Unlike filepath.Clean, this preserves "" as a
// special case.
func (abs absPath) Canonical() (absPath, error) {
if abs == "" {
return abs, nil
}

result, err := filepath.Abs(abs.String())
if err != nil {
return "", err
}
return absPath(result), nil
}

// Join appends more path elements to abs, like filepath.Join.
func (abs absPath) Join(elems ...string) absPath {
all := make([]string, 0, 1+len(elems))
all = append(all, abs.String())
all = append(all, elems...)
return absPath(filepath.Join(all...))
}

// Split breaks abs into stem and leaf parts (often directory and file, but not
// necessarily), similar to filepath.Split. Unlike filepath.Split, the
// resulting stem part does not have any trailing path separators.
func (abs absPath) Split() (string, string) {
if abs == "" {
return "", ""
}

// filepath.Split promises that dir+base == input, but trailing slashes on
// the dir is confusing and ugly.
pathSep := string(os.PathSeparator)
dir, base := filepath.Split(strings.TrimRight(abs.String(), pathSep))
dir = strings.TrimRight(dir, pathSep)
if len(dir) == 0 {
dir = string(os.PathSeparator)
}

return dir, base
}

// Dir returns the stem part of abs without the leaf, like filepath.Dir.
func (abs absPath) Dir() string {
dir, _ := abs.Split()
return dir
}

// Base returns the leaf part of abs without the stem, like filepath.Base.
func (abs absPath) Base() string {
_, base := abs.Split()
return base
}
233 changes: 233 additions & 0 deletions abspath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"testing"
)

func TestAbsPathString(t *testing.T) {
testCases := []string{
"",
"/",
"//",
"/dir",
"/dir/",
"/dir//",
"/dir/sub",
"/dir/sub/",
"/dir//sub",
"/dir//sub/",
"dir",
"dir/sub",
}

for _, tc := range testCases {
if want, got := tc, absPath(tc).String(); want != got {
t.Errorf("expected %q, got %q", want, got)
}
}
}

func TestAbsPathCanonical(t *testing.T) {
testCases := []struct {
in absPath
exp absPath
}{{
in: "",
exp: "",
}, {
in: "/",
exp: "/",
}, {
in: "/one",
exp: "/one",
}, {
in: "/one/two",
exp: "/one/two",
}, {
in: "/one/two/",
exp: "/one/two",
}, {
in: "/one//two",
exp: "/one/two",
}, {
in: "/one/two/../three",
exp: "/one/three",
}}

for _, tc := range testCases {
want := tc.exp
got, err := tc.in.Canonical()
if err != nil {
t.Errorf("%q: unexpected error: %v", tc.in, err)
} else if want != got {
t.Errorf("%q: expected %q, got %q", tc.in, want, got)
}
}
}

func TestAbsPathJoin(t *testing.T) {
testCases := []struct {
base absPath
more []string
expect absPath
}{{
base: "/dir",
more: nil,
expect: "/dir",
}, {
base: "/dir",
more: []string{"one"},
expect: "/dir/one",
}, {
base: "/dir",
more: []string{"one", "two"},
expect: "/dir/one/two",
}, {
base: "/dir",
more: []string{"one", "two", "three"},
expect: "/dir/one/two/three",
}, {
base: "/dir",
more: []string{"with/slash"},
expect: "/dir/with/slash",
}, {
base: "/dir",
more: []string{"with/trailingslash/"},
expect: "/dir/with/trailingslash",
}, {
base: "/dir",
more: []string{"with//twoslash"},
expect: "/dir/with/twoslash",
}, {
base: "/dir",
more: []string{"one/1", "two/2", "three/3"},
expect: "/dir/one/1/two/2/three/3",
}}

for _, tc := range testCases {
if want, got := tc.expect, tc.base.Join(tc.more...); want != got {
t.Errorf("(%q, %q): expected %q, got %q", tc.base, tc.more, want, got)
}
}
}

func TestAbsPathSplit(t *testing.T) {
testCases := []struct {
in absPath
expDir string
expBase string
}{{
in: "",
expDir: "",
expBase: "",
}, {
in: "/",
expDir: "/",
expBase: "",
}, {
in: "//",
expDir: "/",
expBase: "",
}, {
in: "/one",
expDir: "/",
expBase: "one",
}, {
in: "/one/two",
expDir: "/one",
expBase: "two",
}, {
in: "/one/two/",
expDir: "/one",
expBase: "two",
}, {
in: "/one//two",
expDir: "/one",
expBase: "two",
}}

for _, tc := range testCases {
wantDir, wantBase := tc.expDir, tc.expBase
if gotDir, gotBase := tc.in.Split(); wantDir != gotDir || wantBase != gotBase {
t.Errorf("%q: expected (%q, %q), got (%q, %q)", tc.in, wantDir, wantBase, gotDir, gotBase)
}
}
}

func TestAbsPathDir(t *testing.T) {
testCases := []struct {
in absPath
exp string
}{{
in: "",
exp: "",
}, {
in: "/",
exp: "/",
}, {
in: "/one",
exp: "/",
}, {
in: "/one/two",
exp: "/one",
}, {
in: "/one/two/",
exp: "/one",
}, {
in: "/one//two",
exp: "/one",
}}

for _, tc := range testCases {
if want, got := tc.exp, tc.in.Dir(); want != got {
t.Errorf("%q: expected %q, got %q", tc.in, want, got)
}
}
}

func TestAbsPathBase(t *testing.T) {
testCases := []struct {
in absPath
exp string
}{{
in: "",
exp: "",
}, {
in: "/",
exp: "",
}, {
in: "/one",
exp: "one",
}, {
in: "/one/two",
exp: "two",
}, {
in: "/one/two/",
exp: "two",
}, {
in: "/one//two",
exp: "two",
}}

for _, tc := range testCases {
if want, got := tc.exp, tc.in.Base(); want != got {
t.Errorf("%q: expected %q, got %q", tc.in, want, got)
}
}
}
Loading

0 comments on commit 8fa652d

Please sign in to comment.