Skip to content

Commit

Permalink
fix(cmd): CLI Config Relative Imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jshlbrd committed Nov 26, 2024
1 parent bb527a7 commit 313cbbe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
16 changes: 13 additions & 3 deletions cmd/substation/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,20 @@ func testPath(arg string, extVars map[string]string, recursive bool) error {
func testFile(arg string, extVars map[string]string) error {
var cfg customConfig

switch filepath.Ext(arg) {
// Switching directories is required to support relative imports.
// The current directory is saved and restored after each test.
wd, _ := os.Getwd()
defer func() {
_ = os.Chdir(wd)
}()

fileName := filepath.Base(arg)
_ = os.Chdir(filepath.Dir(arg))

switch filepath.Ext(fileName) {
case ".jsonnet", ".libsonnet":
// If the Jsonnet cannot compile, then the file is invalid.
mem, err := compileFile(arg, extVars)
mem, err := compileFile(fileName, extVars)
if err != nil {
fmt.Printf("?\t%s\t[error]\n", arg)
fmt.Fprint(os.Stderr, transformErrStr(err, arg, cfg))
Expand All @@ -211,7 +221,7 @@ func testFile(arg string, extVars map[string]string) error {

cfg = c
case ".json":
c, err := fiConfig(arg)
c, err := fiConfig(fileName)
if err != nil {
return err
}
Expand Down
16 changes: 13 additions & 3 deletions cmd/substation/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,19 @@ func vetFile(arg string, extVars map[string]string) error {
// This uses the custom config from the `test` command.
var cfg customConfig

switch filepath.Ext(arg) {
// Switching directories is required to support relative imports.
// The current directory is saved and restored after each test.
wd, _ := os.Getwd()
defer func() {
_ = os.Chdir(wd)
}()

fileName := filepath.Base(arg)
_ = os.Chdir(filepath.Dir(arg))

switch filepath.Ext(fileName) {
case ".jsonnet", ".libsonnet":
mem, err := compileFile(arg, extVars)
mem, err := compileFile(fileName, extVars)
if err != nil {
// This is an error in the Jsonnet syntax.
// The line number and column range are included.
Expand All @@ -112,7 +122,7 @@ func vetFile(arg string, extVars map[string]string) error {
return err
}
case ".json":
fi, err := fiConfig(arg)
fi, err := fiConfig(fileName)
if err != nil {
return err
}
Expand Down

0 comments on commit 313cbbe

Please sign in to comment.