Skip to content

Commit 038cd71

Browse files
First pass at new eval command
1 parent bbe9dcc commit 038cd71

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

cmd/jsonpath/eval.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2021, SailPoint Technologies, Inc. All rights reserved.
2+
package jsonpath
3+
4+
import (
5+
"fmt"
6+
"os"
7+
8+
"github.com/bhmj/jsonslice"
9+
"github.com/charmbracelet/log"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func newEvalCommand() *cobra.Command {
14+
var filepath string
15+
var path string
16+
17+
cmd := &cobra.Command{
18+
Use: "eval",
19+
Short: "Evaluate a jsonpath against a json file",
20+
Long: "\nEvaluate a jsonpath against a json file\n\n",
21+
Example: "sail jsonpath eval | sail jsonpath e",
22+
Aliases: []string{"e"},
23+
Args: cobra.OnlyValidArgs,
24+
RunE: func(cmd *cobra.Command, args []string) error {
25+
26+
var data []byte
27+
var err error
28+
29+
if filepath != "" {
30+
data, err = os.ReadFile(filepath)
31+
if err != nil {
32+
return err
33+
}
34+
} else {
35+
log.Error("You must provide a file to preview")
36+
}
37+
38+
result, err := jsonslice.Get(data, path)
39+
40+
if err != nil {
41+
return err
42+
}
43+
44+
fmt.Print(string(result))
45+
46+
return nil
47+
48+
},
49+
}
50+
51+
cmd.Flags().StringVarP(&filepath, "file", "f", "", "The path to the json you wish to evaluate")
52+
cmd.Flags().StringVarP(&path, "path", "p", "", "The json path to evaluate against the file provided")
53+
54+
return cmd
55+
}

cmd/jsonpath/jsonpath.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func NewJSONPathCmd() *cobra.Command {
2525
},
2626
}
2727

28+
cmd.AddCommand(
29+
newEvalCommand(),
30+
)
31+
2832
// log.Info("Hello World!")
2933

3034
return cmd

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/sailpoint-oss/sailpoint-cli
33
go 1.21
44

55
require (
6+
github.com/bhmj/jsonslice v1.1.3
67
github.com/charmbracelet/bubbles v0.16.1
78
github.com/charmbracelet/bubbletea v0.24.2
89
github.com/charmbracelet/glamour v0.6.0
@@ -45,6 +46,7 @@ require (
4546
github.com/atotto/clipboard v0.1.4 // indirect
4647
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
4748
github.com/aymerick/douceur v0.2.0 // indirect
49+
github.com/bhmj/xpression v0.9.1 // indirect
4850
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
4951
github.com/danieljoos/wincred v1.2.0 // indirect
5052
github.com/dlclark/regexp2 v1.10.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
1313
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
1414
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
1515
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
16+
github.com/bhmj/jsonslice v1.1.3 h1:aVYmtuQ8Gg1L3mAOFq3NMtfip8teUvC+DNArhv7IRDY=
17+
github.com/bhmj/jsonslice v1.1.3/go.mod h1:O3ZoA0zdEefdbk1dkU5aWPOA36zQhhS/HV6RQFLTlnU=
18+
github.com/bhmj/xpression v0.9.1 h1:N7bX/nWx9oFi/zsiMTx2ehoRApTDAWdQadq/5o2wMGk=
19+
github.com/bhmj/xpression v0.9.1/go.mod h1:j9oYmEXJjeL9mrgW1+ZDBKJXnbupsCPGhlO9J5YhS1Q=
1620
github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY=
1721
github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc=
1822
github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06RaW2cx/SY=

0 commit comments

Comments
 (0)