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

cmd: Rename jx to jnx and split out flag version #36

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ clean:: ## Remove generated files

build: | $(O) ## Build binaries of directories in ./cmd to out/
go build -o $(O) ./cmd/...
go build -tags flag -o $(O)/jx-flag ./cmd/jx

install: ## Build and install binaries in $GOBIN or $GOPATH/bin
go install ./cmd/...

$(O)/jx: build
$(O)/jnx: build

.PHONY: build install

Expand All @@ -33,8 +32,8 @@ test: test-go test-jsonnet ## Run tests and generate a coverage file
test-go: | $(O)
go test -coverprofile=$(COVERFILE) ./...

test-jsonnet: $(O)/jx
$(O)/jx -J $(JSONNET_UNIT) lib/jx_test.jsonnet
test-jsonnet: $(O)/jnx
$(O)/jnx -J $(JSONNET_UNIT) lib/jnx_test.jsonnet

check-coverage: test ## Check that test coverage meets the required level
@go tool cover -func=$(COVERFILE) | $(CHECK_COVERAGE) || $(FAIL_COVERAGE)
Expand Down
4 changes: 2 additions & 2 deletions cmd/jx/doc.go → cmd/jnx/doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// jx evaluates a jsonnet file and outputs it as JSON.
// jnx evaluates a jsonnet file and outputs it as JSON.
//
// Usage: jx [<filename>]
// Usage: jnx [<filename>]
//
// Arguments:
// [<filename>] File to evaluate. stdin is used if omitted or "-"
Expand Down
88 changes: 88 additions & 0 deletions cmd/jnx/jnxflag/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// jnxflag evaluates a jsonnet file and outputs it as JSON.
//
// Usage of ./jnxflag:
// -A var[=str]
// Add top-level arg var[=str] (from environment if <str> is omitted)
// -J dir
// Add a library search dir
// -V var[=str]
// Add extVar var[=str] (from environment if <str> is omitted)
// -ext-code var[=code]
// Add extVar var[=code] (from environment if <code> is omitted)
// -ext-code-file var=file
// Add extVar var=file code from a file
// -ext-str var[=str]
// Add extVar var[=str] (from environment if <str> is omitted)
// -ext-str-file var=file
// Add extVar var=file string from a file
// -jpath dir
// Add a library search dir
// -tla-code var[=code]
// Add top-level arg var[=code] (from environment if <code> is omitted)
// -tla-code-file var=file
// Add top-level arg var=file code from a file
// -tla-str var=[=str]
// Add top-level arg var=[=str] (from environment if <str> is omitted)
// -tla-str-file var=file
// Add top-level arg var=file string from a file
//
// This program exists just to implement the standard Go flag package parsing.
// The full jnx program uses the kong library and has more features.

package main

import (
"flag"
"fmt"
"os"

"foxygo.at/jsonnext"
jsonnet "github.com/google/go-jsonnet"
)

type config struct {
jsonnext.Config
Filename string `arg:"" optional:"" help:"File to evaluate. stdin is used if omitted or \"-\""`
}

func main() {
cli := parseCLI()
vm := cli.Config.MakeVM("JNXPATH")

out, err := run(vm, cli.Filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Print(out)
}

// Parse CLI using Go's flag package and the helpers in jsonnext.
func parseCLI() *config {
c := &config{}
c.Config = *jsonnext.ConfigFlags(flag.CommandLine)

flag.Parse()
if flag.NArg() > 1 {
flag.Usage()
os.Exit(1)
} else if flag.NArg() == 1 {
c.Filename = flag.Args()[0]
}

return c
}

func run(vm *jsonnet.VM, filename string) (string, error) {
node, _, err := vm.ImportAST("", filename)
if err != nil {
return "", err
}

out, err := vm.Evaluate(node)
if err != nil {
return "", err
}

return out, nil
}
12 changes: 7 additions & 5 deletions cmd/jx/main.go → cmd/jnx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"fmt"
"os"

jxkong "foxygo.at/jsonnext/kong"
jnxkong "foxygo.at/jsonnext/kong"
"github.com/alecthomas/kong"
jsonnet "github.com/google/go-jsonnet"
)

type config struct {
jxkong.Config
jnxkong.Config
Filename string `arg:"" optional:"" help:"File to evaluate. stdin is used if omitted or \"-\""`
}

func main() {
cli := parseCLI()
vm := cli.Config.MakeVM("JXPATH")
c := &config{Config: *jnxkong.NewConfig()}
kong.Parse(c)
vm := c.Config.MakeVM("JNXPATH")

out, err := run(vm, cli.Filename)
out, err := run(vm, c.Filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
26 changes: 0 additions & 26 deletions cmd/jx/cli_flag.go

This file was deleted.

15 changes: 0 additions & 15 deletions cmd/jx/cli_kong.go

This file was deleted.

8 changes: 4 additions & 4 deletions kong/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import (
"fmt"
"os"

jxkong "foxygo.at/jsonnext/kong"
jnxkong "foxygo.at/jsonnext/kong"
"github.com/alecthomas/kong"
)

func Example() {
// Define kong CLI struct embedding jxkong.Config, adding your own
// Define kong CLI struct embedding jnxkong.Config, adding your own
// application-specific flags and args.
cli := struct {
jxkong.Config
jnxkong.Config
Verbose bool
Filename string `arg:""`
}{
Config: *jxkong.NewConfig(), // foxygo.at/jsonnext/kong imported as jxkong
Config: *jnxkong.NewConfig(), // foxygo.at/jsonnext/kong imported as jnxkong
}

// Simulate command line arguments
Expand Down
4 changes: 2 additions & 2 deletions kong/kong_conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

"foxygo.at/jsonnext"
"foxygo.at/jsonnext/conformance"
jxkong "foxygo.at/jsonnext/kong"
jnxkong "foxygo.at/jsonnext/kong"
)

type suite struct{}

func (s *suite) Parse(t *testing.T, args []string) (*jsonnext.Config, error) {
kcfg := jxkong.NewConfig()
kcfg := jnxkong.NewConfig()
parser, err := kong.New(kcfg)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion lib/array_test.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S jx -J //github.com/yugui/jsonnetunit/raw/master
#!/usr/bin/env -S jnx -J //github.com/yugui/jsonnetunit/raw/master

local array = import 'array.jsonnet';
local test = import 'jsonnetunit/test.libsonnet';
Expand Down
9 changes: 9 additions & 0 deletions lib/jnx.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Package jnx is a top-level package to import the subpackages of the
// jnx library.
{
array:: import 'array.jsonnet',
object:: import 'object.jsonnet',
op:: import 'op.jsonnet',
string:: import 'string.jsonnet',
value:: import 'value.jsonnet',
}
12 changes: 6 additions & 6 deletions lib/jx_test.jsonnet → lib/jnx_test.jsonnet
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#!/usr/bin/env -S jx -J //github.com/yugui/jsonnetunit/raw/master
#!/usr/bin/env -S jnx -J //github.com/yugui/jsonnetunit/raw/master
//
// Roll-up all the tests in this directory
//

local jnx = import 'jnx.jsonnet';
local test = import 'jsonnetunit/test.libsonnet';
local jx = import 'jx.jsonnet';

local jx_test = { name: 'jx.jsonnet test' } + test.suite({
local jnx_test = { name: 'jnx.jsonnet test' } + test.suite({
testImport: {
// Force evaluation of imports in `jx.jsonnet` by using std.prune over
// Force evaluation of imports in `jnx.jsonnet` by using std.prune over
// all fields. This will cause a failure if a file cannot be imported.
actual: std.prune(std.objectValuesAll(jx)),
actual: std.prune(std.objectValuesAll(jnx)),
expect: [], // empty because all fields are hidden and thus pruned.
},
});

[
jx_test,
jnx_test,
import 'array_test.jsonnet',
import 'object_test.jsonnet',
import 'op_test.jsonnet',
Expand Down
11 changes: 2 additions & 9 deletions lib/jx.jsonnet
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
// Package jx is a top-level package to import the subpackages of the
// jx library.
{
array:: import 'array.jsonnet',
object:: import 'object.jsonnet',
op:: import 'op.jsonnet',
string:: import 'string.jsonnet',
value:: import 'value.jsonnet',
}
// Kept for backwards compatibilty for now. Will be removed in the future.
import 'jnx.jsonnet'
2 changes: 1 addition & 1 deletion lib/object_test.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S jx -J //github.com/yugui/jsonnetunit/raw/master
#!/usr/bin/env -S jnx -J //github.com/yugui/jsonnetunit/raw/master

local test = import 'jsonnetunit/test.libsonnet';
local object = import 'object.jsonnet';
Expand Down
2 changes: 1 addition & 1 deletion lib/op_test.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S jx -J //github.com/yugui/jsonnetunit/raw/master
#!/usr/bin/env -S jnx -J //github.com/yugui/jsonnetunit/raw/master

local test = import 'jsonnetunit/test.libsonnet';
local op = import 'op.jsonnet';
Expand Down
2 changes: 1 addition & 1 deletion lib/string_test.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S jx -J //github.com/yugui/jsonnetunit/raw/master
#!/usr/bin/env -S jnx -J //github.com/yugui/jsonnetunit/raw/master

local test = import 'jsonnetunit/test.libsonnet';
local string = import 'string.jsonnet';
Expand Down
2 changes: 1 addition & 1 deletion lib/value_test.jsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S jx -J //github.com/yugui/jsonnetunit/raw/master
#!/usr/bin/env -S jnx -J //github.com/yugui/jsonnetunit/raw/master

local test = import 'jsonnetunit/test.libsonnet';
local value = import 'value.jsonnet';
Expand Down