Skip to content

Commit

Permalink
Add option to show value and type
Browse files Browse the repository at this point in the history
  • Loading branch information
jastBytes committed Aug 10, 2020
1 parent 90170cc commit cb0ebc7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/jp
/jp*
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ all: lint clean build
.PHONY: build lint clean

clean:
rm -f $(CMD)
rm -f $(CMD).linux
rm -f $(CMD).darwin

lint:
$(LINTER) run -v --no-config --deadline=5m

$(CMD):
CGO_ENABLED=0 GOOS=linux $(GO) build -o $(CMD) -a $(BUILDFLAGS) $(LDFLAGS) $(CMD_SRC)
CGO_ENABLED=0 GOOS=linux $(GO) build -o $(CMD).linux -a $(BUILDFLAGS) $(LDFLAGS) $(CMD_SRC)
CGO_ENABLED=0 GOOS=darwin $(GO) build -o $(CMD).darwin -a $(BUILDFLAGS) $(LDFLAGS) $(CMD_SRC)

build: $(CMD)
build: $(CMD)
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Usage:
Flags:
-h, --help help for jp
-k, --key string Key to search for
-t, --show-type If enabled, will show the type of the value for found path
-v, --show-value If enabled, will show the value for found path
```

Expand All @@ -20,7 +21,11 @@ Flags:
$ kubectl get node node01 -ojson | jp -k osImage
.status.nodeInfo.osImage
$ kubectl get node node01 -ojson | jp -k osImage -v
.status.nodeInfo.osImage [Flatcar Container Linux by Kinvolk 2512.2.0 (Oklo)]
.status.nodeInfo.osImage:Flatcar Container Linux by Kinvolk 2512.2.0 (Oklo)
$ kubectl get node node01 -ojson | jp -k osImage -v -t
.status.nodeInfo.osImage:Flatcar Container Linux by Kinvolk 2512.2.0 (Oklo):string
$ kubectl get node node01 -ojson | jp -k osImage -t
.status.nodeInfo.osImage:string
```

## Acknowledgements
Expand Down
13 changes: 9 additions & 4 deletions cmd/jp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
var (
key string
showValue bool
showType bool
)

var jpCmd = &cobra.Command{
Use: "jp action [flags]",
Use: "jp [flags]",
Short: "JsonPath finds paths to a key in nested JSON structures.",
RunE: func(cmd *cobra.Command, args []string) error {
if key == "" {
Expand Down Expand Up @@ -53,6 +54,7 @@ func init() {
flags.AddGoFlagSet(flag.CommandLine)
flags.StringVarP(&key, "key", "k", "", "Key to search for")
flags.BoolVarP(&showValue, "show-value", "v", false, "If enabled, will show the value for found path")
flags.BoolVarP(&showType, "show-type", "t", false, "If enabled, will show the type of the value for found path")
}

func printPaths(v interface{}, key, path string) {
Expand All @@ -61,11 +63,14 @@ func printPaths(v interface{}, key, path string) {
for mk, mv := range v {
p := path + "." + mk
if mk == key {
fmt.Print(p)
if showValue {
fmt.Printf("%v [%v]\n", p, mv)
} else {
fmt.Println(p)
fmt.Printf(":%v", mv)
}
if showType {
fmt.Printf(":%T", mv)
}
fmt.Print("\n")
}
printPaths(mv, key, p)
}
Expand Down

0 comments on commit cb0ebc7

Please sign in to comment.