Skip to content

Commit

Permalink
fix(cmd): CLI Config Relative Imports (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshlbrd authored Nov 27, 2024
1 parent bb527a7 commit 3b18bfe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
18 changes: 15 additions & 3 deletions cmd/substation/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,22 @@ 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))

// The default case is a no-op -- the file is skipped because
// it cannot be tested.
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 +223,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
18 changes: 15 additions & 3 deletions cmd/substation/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,21 @@ 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))

// The default case is a no-op -- the file is skipped because
// it cannot be vetted.
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 +124,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 3b18bfe

Please sign in to comment.