Skip to content

Commit

Permalink
Merge pull request #180 from srm09/bug/fix-relative-paths
Browse files Browse the repository at this point in the history
Allows relative paths for kubeconfig and argsfile
  • Loading branch information
srm09 authored Sep 18, 2020
2 parents 3174cdc + b5f0f62 commit 66b572e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions starlark/crashd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vmware-tanzu/crash-diagnostics/ssh"
"github.com/vmware-tanzu/crash-diagnostics/util"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
Expand Down Expand Up @@ -77,6 +78,11 @@ func crashdConfigFn(thread *starlark.Thread, _ *starlark.Builtin, args starlark.
thread.SetLocal(identifiers.sshAgent, agent)
}

workdir, err := util.ExpandPath(workdir)
if err != nil {
return starlark.None, err
}

cfgStruct := starlarkstruct.FromStringDict(starlark.String(identifiers.crashdCfg), starlark.StringDict{
"workdir": starlark.String(workdir),
"gid": starlark.String(gid),
Expand Down
6 changes: 6 additions & 0 deletions starlark/kube_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"

"github.com/pkg/errors"
"github.com/vmware-tanzu/crash-diagnostics/util"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
Expand Down Expand Up @@ -51,6 +52,11 @@ func KubeConfigFn(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple,
path = pathStr.GoString()
}

path, err := util.ExpandPath(path)
if err != nil {
return starlark.None, err
}

structVal := starlarkstruct.FromStringDict(starlark.String(identifiers.kubeCfg), starlark.StringDict{
"path": starlark.String(path),
})
Expand Down
11 changes: 11 additions & 0 deletions util/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ import (
"github.com/sirupsen/logrus"
)

// ReadArgsFile parses the args file and populates the map with the contents
// of that file. The parsing follows the following rules:
// * each line should contain only a single key=value pair
// * lines starting with # are ignored
// * empty lines are ignored
// * any line not following the above patterns are ignored with a warning message
func ReadArgsFile(path string, args map[string]string) error {
path, err := ExpandPath(path)
if err != nil {
return err
}

file, err := os.Open(path)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("args file not found: %s", path))
Expand Down
22 changes: 22 additions & 0 deletions util/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package util

import (
"os"
"path/filepath"
)

// ExpandPath converts the file path to include the home directory when prefixed with `~`.
func ExpandPath(path string) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}

if path[0] == '~' {
path = filepath.Join(home, path[1:])
}
return path, nil
}
27 changes: 27 additions & 0 deletions util/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package util

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("ExpandPath", func() {

It("returns the same path when input does not contain ~", func() {
input := "/foo/bar"
path, err := ExpandPath(input)
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(input))
})

It("replaces the ~ with home directory path", func() {
input := "~/foo/bar"
path, err := ExpandPath(input)
Expect(err).NotTo(HaveOccurred())
Expect(path).NotTo(Equal(input))
Expect(path).NotTo(ContainSubstring("~"))
})
})

0 comments on commit 66b572e

Please sign in to comment.