Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd): CLI Config Relative Imports #279

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
jtan-brex marked this conversation as resolved.
Show resolved Hide resolved
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
}
jshlbrd marked this conversation as resolved.
Show resolved Hide resolved
Expand Down