Skip to content

Commit

Permalink
clear the filter stack in commands that alter the input
Browse files Browse the repository at this point in the history
passing -k to such commands can circumvent this behavior
  • Loading branch information
bmatsuo committed Jul 13, 2014
1 parent 61d325d commit f76890f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
34 changes: 26 additions & 8 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ func cmdPeek(jq *JQShell, flags *CmdFlags) error {

err = cmdWrite(jq, Flags("write", nil))

for _ = range make([]struct{}, len(filters)) {
jq.Stack.Pop()
}

jq.Stack.Pop(len(filters))
if err != nil {
return fmt.Errorf("invalid filter: %v", err)
}
Expand All @@ -218,11 +215,11 @@ func cmdPush(jq *JQShell, flags *CmdFlags) error {
}
err = testFilter(jq)
if err != nil {
jq.Stack.Pop()
jq.Stack.Pop(1)
return err
}
if !*quiet {
return cmdWrite(jq, Flags("write", nil))
return cmdWrite(jq, Flags("write", []string{}))
}
return nil
}
Expand All @@ -249,6 +246,17 @@ func testFilter(jq *JQShell) error {
close(stop)
return fmt.Errorf("jq timed out processing the filter")
}
}

func cmdPopAll(jq *JQShell, flags *CmdFlags) error {
err := flags.Parse(nil)
if IsHelp(err) {
return nil
}
if err != nil {
return err
}
jq.Stack.PopAll()
return nil
}

Expand Down Expand Up @@ -279,8 +287,9 @@ func cmdPop(jq *JQShell, flags *CmdFlags) error {
if n < 0 {
return fmt.Errorf("argument must be positive")
}
for i := 0; i < n; i++ {
jq.Stack.Pop()
_, err = jq.Stack.Pop(n)
if err != nil {
return err
}
if !*quiet {
return cmdWrite(jq, Flags("write", nil))
Expand All @@ -291,6 +300,7 @@ func cmdPop(jq *JQShell, flags *CmdFlags) error {
func cmdLoad(jq *JQShell, flags *CmdFlags) error {
flags.ArgSet("filename")
quiet := flags.Bool("q", false, "quiet -- no implicit :write after setting input")
keepStack := flags.Bool("k", false, "keep the current filter stack after setting input")
err := flags.Parse(nil)
if IsHelp(err) {
return nil
Expand All @@ -313,6 +323,9 @@ func cmdLoad(jq *JQShell, flags *CmdFlags) error {
}
jq.filename = args[0]
jq.istmp = false
if !*keepStack {
jq.Stack.PopAll()
}
if !*quiet {
return cmdWrite(jq, Flags("write", nil))
}
Expand Down Expand Up @@ -494,6 +507,7 @@ func cmdRaw(jq *JQShell, flags *CmdFlags) error {
func cmdExec(jq *JQShell, flags *CmdFlags) error {
flags.ArgSet("name", "arg", "...")
quiet := flags.Bool("q", false, "quiet -- no implicit :write after setting input")
keepStack := flags.Bool("k", false, "keep the current filter stack after setting input")
ignore := flags.Bool("ignore", false, "ignore process exit status")
filename := flags.String("o", "", "a json file produced by the command")
pfilename := flags.String("O", "", "like -O but the file will not be deleted by jqsh")
Expand Down Expand Up @@ -555,6 +569,10 @@ func cmdExec(jq *JQShell, flags *CmdFlags) error {

jq.SetInputFile(path, istmp)

if !*keepStack {
jq.Stack.PopAll()
}

if !*quiet {
return cmdWrite(jq, Flags("write", nil))
}
Expand Down
22 changes: 18 additions & 4 deletions jq.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,26 @@ func (s *JQStack) Push(cmd Filter) {
s.pipe = append(s.pipe, cmd)
}

func (s *JQStack) Pop() (Filter, error) {
func (s *JQStack) Pop(n int) ([]Filter, error) {
if len(s.pipe) == 0 {
return nil, ErrStackEmpty
}
n := len(s.pipe)
filt := s.pipe[n-1]
s.pipe = s.pipe[:n-1]
m := len(s.pipe)
if m < n {
n = m
}
filt := make([]Filter, n)
copy(filt, s.pipe[m-n:])
for i := m - n; i < m; i++ {
s.pipe[i] = nil
}
s.pipe = s.pipe[:m-n]
return filt, nil
}

func (s *JQStack) PopAll() {
for i := range s.pipe {
s.pipe[i] = nil
}
s.pipe = s.pipe[:0]
}
8 changes: 4 additions & 4 deletions jq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,25 @@ func TestJQStack(t *testing.T) {
t.Fatalf("incorrect filter stack: %v", fs)
}

s.Pop()
s.Pop(1)
fs = s.JQFilter()
if !reflect.DeepEqual(fs, []string{"hello"}) {
t.Fatalf("incorrect filter stack: %v", fs)
}

s.Pop()
s.Pop(1)
fs = s.JQFilter()
if !reflect.DeepEqual(fs, []string{"hello"}) {
t.Fatalf("incorrect filter stack: %v", fs)
}

s.Pop()
s.Pop(1)
fs = s.JQFilter()
if len(fs) != 0 {
t.Fatalf("incorrect filter stack: %v", fs)
}

s.Pop()
s.Pop(1)
fs = s.JQFilter()
if len(fs) != 0 {
t.Fatalf("incorrect filter stack: %v", fs)
Expand Down
1 change: 1 addition & 0 deletions jqsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func NewJQShell(bin string, sh ShellReader) *JQShell {
jq.lib.Register("push", JQShellCommandFunc(cmdPush))
jq.lib.Register("peek", JQShellCommandFunc(cmdPeek))
jq.lib.Register("pop", JQShellCommandFunc(cmdPop))
jq.lib.Register("popall", JQShellCommandFunc(cmdPopAll))
jq.lib.Register("filter", JQShellCommandFunc(cmdFilter))
jq.lib.Register("script", JQShellCommandFunc(cmdScript))
jq.lib.Register("load", JQShellCommandFunc(cmdLoad))
Expand Down

0 comments on commit f76890f

Please sign in to comment.