-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to kong, make parsers extensible, add tests
- Loading branch information
Showing
23 changed files
with
590 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cli | ||
|
||
import ( | ||
"io" | ||
"reflect" | ||
|
||
"github.com/alecthomas/kong" | ||
"github.com/tomwright/dasel/v3/internal" | ||
) | ||
|
||
type Globals struct { | ||
Stdin io.Reader `kong:"-"` | ||
Stdout io.Writer `kong:"-"` | ||
Stderr io.Writer `kong:"-"` | ||
} | ||
|
||
type CLI struct { | ||
Globals | ||
|
||
Query QueryCmd `cmd:"" help:"Execute a query"` | ||
} | ||
|
||
func MustRun(stdin io.Reader, stdout, stderr io.Writer) { | ||
ctx, err := Run(stdin, stdout, stderr) | ||
ctx.FatalIfErrorf(err) | ||
} | ||
|
||
func Run(stdin io.Reader, stdout, stderr io.Writer) (*kong.Context, error) { | ||
cli := &CLI{ | ||
Globals: Globals{ | ||
Stdin: stdin, | ||
Stdout: stdout, | ||
Stderr: stderr, | ||
}, | ||
} | ||
|
||
ctx := kong.Parse( | ||
cli, | ||
kong.Name("dasel"), | ||
kong.Description("Query and modify data structures from the command line."), | ||
kong.UsageOnError(), | ||
kong.ConfigureHelp(kong.HelpOptions{Compact: true}), | ||
kong.Vars{ | ||
"version": internal.Version, | ||
}, | ||
kong.Bind(&cli.Globals), | ||
kong.TypeMapper(reflect.TypeFor[*[]variable](), &variableMapper{}), | ||
kong.OptionFunc(func(k *kong.Kong) error { | ||
k.Stdout = cli.Stdout | ||
k.Stderr = cli.Stderr | ||
return nil | ||
}), | ||
) | ||
err := ctx.Run() | ||
return ctx, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package cli_test | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/tomwright/dasel/v3/internal/cli" | ||
) | ||
|
||
func runDasel(args []string, in []byte) ([]byte, []byte, error) { | ||
stdOut := bytes.NewBuffer([]byte{}) | ||
stdErr := bytes.NewBuffer([]byte{}) | ||
stdIn := bytes.NewReader(in) | ||
|
||
originalArgs := os.Args | ||
defer func() { | ||
os.Args = originalArgs | ||
}() | ||
|
||
os.Args = append([]string{"dasel", "query"}, args...) | ||
|
||
_, err := cli.Run(stdIn, stdOut, stdErr) | ||
|
||
return stdOut.Bytes(), stdErr.Bytes(), err | ||
} | ||
|
||
type testCase struct { | ||
args []string | ||
in []byte | ||
stdout []byte | ||
stderr []byte | ||
err error | ||
} | ||
|
||
func runTest(tc testCase) func(t *testing.T) { | ||
return func(t *testing.T) { | ||
if tc.stdout == nil { | ||
tc.stdout = []byte{} | ||
} | ||
if tc.stderr == nil { | ||
tc.stderr = []byte{} | ||
} | ||
|
||
gotStdOut, gotStdErr, gotErr := runDasel(tc.args, tc.in) | ||
if !errors.Is(gotErr, tc.err) && !errors.Is(tc.err, gotErr) { | ||
t.Errorf("expected error %v, got %v", tc.err, gotErr) | ||
return | ||
} | ||
|
||
if !reflect.DeepEqual(tc.stderr, gotStdErr) { | ||
t.Errorf("expected stderr %s, got %s", string(tc.stderr), string(gotStdErr)) | ||
} | ||
|
||
if !reflect.DeepEqual(tc.stdout, gotStdOut) { | ||
t.Errorf("expected stdout %s, got %s", string(tc.stdout), string(gotStdOut)) | ||
} | ||
} | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
t.Run("complex set", func(t *testing.T) { | ||
t.Run("set nested with spread", runTest(testCase{ | ||
args: []string{"-i", "json", "-o", "json", "--root", `user = {user..., name: {"first": $this.user.name, "last": "Doe"}}`}, | ||
in: []byte(`{"user": {"name": "John"}}`), | ||
stdout: []byte(`{ | ||
"user": { | ||
"name": { | ||
"first": "John", | ||
"last": "Doe" | ||
} | ||
} | ||
} | ||
`), | ||
stderr: nil, | ||
err: nil, | ||
})) | ||
t.Run("set nested", runTest(testCase{ | ||
args: []string{"-i", "json", "-o", "json", "--root", `user.name = {"first": user.name, "last": "Doe"}`}, | ||
in: []byte(`{"user": {"name": "John"}}`), | ||
stdout: []byte(`{ | ||
"user": { | ||
"name": { | ||
"first": "John", | ||
"last": "Doe" | ||
} | ||
} | ||
} | ||
`), | ||
stderr: nil, | ||
err: nil, | ||
})) | ||
t.Run("set nested with localised group", runTest(testCase{ | ||
args: []string{"-i", "json", "-o", "json", "--root", `user.(name = {"first": name, "last": "Doe"})`}, | ||
in: []byte(`{"user": {"name": "John"}}`), | ||
stdout: []byte(`{ | ||
"user": { | ||
"name": { | ||
"first": "John", | ||
"last": "Doe" | ||
} | ||
} | ||
} | ||
`), | ||
stderr: nil, | ||
err: nil, | ||
})) | ||
}) | ||
} |
Oops, something went wrong.