mirror of
https://github.com/wader/fq.git
synced 2024-09-11 12:05:39 +03:00
Merge pull request #902 from wader/gojq-update-halt_error-rework
gojq: Update fq fork
This commit is contained in:
commit
ae519be2fd
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module github.com/wader/fq
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
// fork of github.com/itchyny/gojq, see github.com/wader/gojq fq branch
|
// fork of github.com/itchyny/gojq, see github.com/wader/gojq fq branch
|
||||||
require github.com/wader/gojq v0.12.1-0.20240118170525-e920352821d6
|
require github.com/wader/gojq v0.12.1-0.20240324083939-2b4dc721594a
|
||||||
|
|
||||||
require (
|
require (
|
||||||
// bump: gomod-BurntSushi/toml /github\.com\/BurntSushi\/toml v(.*)/ https://github.com/BurntSushi/toml.git|^1
|
// bump: gomod-BurntSushi/toml /github\.com\/BurntSushi\/toml v(.*)/ https://github.com/BurntSushi/toml.git|^1
|
||||||
|
4
go.sum
4
go.sum
@ -23,8 +23,8 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx
|
|||||||
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||||
github.com/wader/gojq v0.12.1-0.20240118170525-e920352821d6 h1:0zhn+HFzBP6i4XjyR+OMKauJ9zOZROpJCW9D75Z0fRE=
|
github.com/wader/gojq v0.12.1-0.20240324083939-2b4dc721594a h1:8SdZghxMYax232ktkzCOqtjnIQ9hO7sP7AXJipAZh8o=
|
||||||
github.com/wader/gojq v0.12.1-0.20240118170525-e920352821d6/go.mod h1:E7walEZ03d5WBrEMutC7+tagVBDdtNTDe0jRxMCC6N0=
|
github.com/wader/gojq v0.12.1-0.20240324083939-2b4dc721594a/go.mod h1:gPc2V6SapmFtdvALwJRg9WKk97piqkagc6vW6PoPqM4=
|
||||||
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||||
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||||
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
|
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
|
||||||
|
@ -123,13 +123,13 @@ def _cli_eval_on_expr_error:
|
|||||||
# other expr error, other errors then cancel should not happen, report and halt
|
# other expr error, other errors then cancel should not happen, report and halt
|
||||||
def _cli_eval_on_error:
|
def _cli_eval_on_error:
|
||||||
if .error | _is_context_canceled_error then (null | halt_error(_exit_code_expr_error))
|
if .error | _is_context_canceled_error then (null | halt_error(_exit_code_expr_error))
|
||||||
else halt_error(_exit_code_expr_error)
|
else _fatal_error(_exit_code_expr_error)
|
||||||
end;
|
end;
|
||||||
# could not compile expr, report and halt
|
# could not compile expr, report and halt
|
||||||
def _cli_eval_on_compile_error:
|
def _cli_eval_on_compile_error:
|
||||||
( .error
|
( .error
|
||||||
| _eval_compile_error_tostring
|
| _eval_compile_error_tostring
|
||||||
| halt_error(_exit_code_compile_error)
|
| _fatal_error(_exit_code_compile_error)
|
||||||
);
|
);
|
||||||
def _cli_repl_error($_):
|
def _cli_repl_error($_):
|
||||||
_eval_error("compile"; "repl can only be used from interactive repl");
|
_eval_error("compile"; "repl can only be used from interactive repl");
|
||||||
@ -164,7 +164,7 @@ def _main:
|
|||||||
try (open | decode)
|
try (open | decode)
|
||||||
catch
|
catch
|
||||||
( "--argdecode \($a[0]): \(.)"
|
( "--argdecode \($a[0]): \(.)"
|
||||||
| halt_error(_exit_code_args_error)
|
| _fatal_error(_exit_code_args_error)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -172,7 +172,7 @@ def _main:
|
|||||||
# make sure we don't unintentionally use . to make things clearer
|
# make sure we don't unintentionally use . to make things clearer
|
||||||
| null
|
| null
|
||||||
| ( try _args_parse($args[1:]; _opt_cli_opts)
|
| ( try _args_parse($args[1:]; _opt_cli_opts)
|
||||||
catch halt_error(_exit_code_args_error)
|
catch _fatal_error(_exit_code_args_error)
|
||||||
) as {parsed: $parsed_args, $rest}
|
) as {parsed: $parsed_args, $rest}
|
||||||
# combine default fixed opt, parsed args and -o key=value opts
|
# combine default fixed opt, parsed args and -o key=value opts
|
||||||
| _options_stack([
|
| _options_stack([
|
||||||
|
@ -40,6 +40,8 @@ def stderr:
|
|||||||
, .
|
, .
|
||||||
);
|
);
|
||||||
|
|
||||||
|
def _fatal_error($code): "error: \(.)\n" | halt_error($code);
|
||||||
|
|
||||||
# try to be same exit codes as jq
|
# try to be same exit codes as jq
|
||||||
# TODO: jq seems to halt processing inputs on JSON decode error but not IO errors,
|
# TODO: jq seems to halt processing inputs on JSON decode error but not IO errors,
|
||||||
# seems strange.
|
# seems strange.
|
||||||
|
@ -115,11 +115,6 @@ type Exiter interface {
|
|||||||
ExitCode() int
|
ExitCode() int
|
||||||
}
|
}
|
||||||
|
|
||||||
// gojq halt_error uses this
|
|
||||||
type IsEmptyErrorer interface {
|
|
||||||
IsEmptyError() bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type Terminal interface {
|
type Terminal interface {
|
||||||
Size() (int, int)
|
Size() (int, int)
|
||||||
IsTerminal() bool
|
IsTerminal() bool
|
||||||
@ -403,8 +398,25 @@ func (i *Interp) Main(ctx context.Context, output Output, versionStr string) err
|
|||||||
|
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case error:
|
case error:
|
||||||
if emptyErr, ok := v.(IsEmptyErrorer); ok && emptyErr.IsEmptyError() {
|
var haltErr gojq.HaltError
|
||||||
// no output
|
if errors.As(v, &haltErr) {
|
||||||
|
if haltErrV := haltErr.Value(); haltErrV != nil {
|
||||||
|
if str, ok := haltErrV.(string); ok {
|
||||||
|
if _, err := i.OS.Stderr().Write([]byte(str)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bs, _ := gojq.Marshal(haltErrV)
|
||||||
|
if _, err := i.OS.Stderr().Write(bs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := i.OS.Stderr().Write([]byte{'\n'}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return haltErr
|
||||||
} else if errors.Is(v, context.Canceled) {
|
} else if errors.Is(v, context.Canceled) {
|
||||||
// ignore context cancel here for now, which means user somehow interrupted the interpreter
|
// ignore context cancel here for now, which means user somehow interrupted the interpreter
|
||||||
// TODO: handle this inside interp.jq instead but then we probably have to do nested
|
// TODO: handle this inside interp.jq instead but then we probably have to do nested
|
||||||
@ -413,8 +425,6 @@ func (i *Interp) Main(ctx context.Context, output Output, versionStr string) err
|
|||||||
fmt.Fprintln(i.OS.Stderr(), v)
|
fmt.Fprintln(i.OS.Stderr(), v)
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
case [2]any:
|
|
||||||
fmt.Fprintln(i.OS.Stderr(), v[:]...)
|
|
||||||
default:
|
default:
|
||||||
// TODO: can this happen?
|
// TODO: can this happen?
|
||||||
fmt.Fprintln(i.OS.Stderr(), v)
|
fmt.Fprintln(i.OS.Stderr(), v)
|
||||||
|
@ -126,7 +126,7 @@ def _opt_eval($rest):
|
|||||||
)
|
)
|
||||||
catch
|
catch
|
||||||
( "-o \($opt)=@\($v[1:]): \(.)"
|
( "-o \($opt)=@\($v[1:]): \(.)"
|
||||||
| halt_error(_exit_code_args_error)
|
| _fatal_error(_exit_code_args_error)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -140,7 +140,7 @@ def _opt_eval($rest):
|
|||||||
try fromjson
|
try fromjson
|
||||||
catch
|
catch
|
||||||
( "--argjson \($a[0]): \(.)"
|
( "--argjson \($a[0]): \(.)"
|
||||||
| halt_error(_exit_code_args_error)
|
| _fatal_error(_exit_code_args_error)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -160,7 +160,7 @@ def _opt_eval($rest):
|
|||||||
| . as $expr_file
|
| . as $expr_file
|
||||||
| if . then
|
| if . then
|
||||||
try (open | tobytes | tostring)
|
try (open | tobytes | tostring)
|
||||||
catch ("\($expr_file): \(.)" | halt_error(_exit_code_args_error))
|
catch ("\($expr_file): \(.)" | _fatal_error(_exit_code_args_error))
|
||||||
else $rest[0] // null
|
else $rest[0] // null
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
@ -201,7 +201,7 @@ def _opt_eval($rest):
|
|||||||
( map(.[1] |=
|
( map(.[1] |=
|
||||||
( . as $f
|
( . as $f
|
||||||
| try (open | tobytes | tostring)
|
| try (open | tobytes | tostring)
|
||||||
catch ("\($f): \(.)" | halt_error(_exit_code_args_error))
|
catch ("\($f): \(.)" | _fatal_error(_exit_code_args_error))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -195,7 +195,7 @@ def _repl_on_expr_error:
|
|||||||
def _repl_on_error:
|
def _repl_on_error:
|
||||||
# was interrupted by user, just ignore
|
# was interrupted by user, just ignore
|
||||||
if .error | _is_context_canceled_error then empty
|
if .error | _is_context_canceled_error then empty
|
||||||
else halt_error(_exit_code_expr_error)
|
else _fatal_error(_exit_code_expr_error)
|
||||||
end;
|
end;
|
||||||
# compile error
|
# compile error
|
||||||
def _repl_on_compile_error:
|
def _repl_on_compile_error:
|
||||||
|
4
pkg/interp/testdata/exitcode.fqtest
vendored
4
pkg/interp/testdata/exitcode.fqtest
vendored
@ -6,10 +6,10 @@ $ fq -n '1+"a"'
|
|||||||
exitcode: 5
|
exitcode: 5
|
||||||
stderr:
|
stderr:
|
||||||
error: cannot add: number (1) and string ("a")
|
error: cannot add: number (1) and string ("a")
|
||||||
$ fq -n '"abc" | halt_error(123)'
|
$ fq -n '"abc\n" | halt_error(123)'
|
||||||
exitcode: 123
|
exitcode: 123
|
||||||
stderr:
|
stderr:
|
||||||
error: abc
|
abc
|
||||||
$ fq -n 'null | halt_error(123)'
|
$ fq -n 'null | halt_error(123)'
|
||||||
exitcode: 123
|
exitcode: 123
|
||||||
$ fq -n invalid
|
$ fq -n invalid
|
||||||
|
Loading…
Reference in New Issue
Block a user