osgigogo-shell

gogoshell own commands piping


I'm trying to use gogo-shell to add some console commands For example I'm create commands add and show

public void add(CommandSession commandSession, int i) {
    List<Integer> il = commandSession.get("list");
    if (il == null) {
        il = new ArrayList<Integer>();
        il.add(i);
        commandSession.put("list",il)
    } else {
        il.add(i)
    }
}
public void show(CommandSession commandSession) {
    List<Integer> il = commandSession.get("list");
    il.foreach(System.out::println);
}

and when i use them like

add 1 | add 2 | add 3 | add 4 | show

I'm geting something like

null pointer Exception

or

1
3
4
2

I think this happens because pipes (add) runs in parallel. So how can I write command where piping will be sequential.


Solution

  • Pipelines in gogo (like in bash) expect to consume data from standard input and produce data on standard output. Each element in the pipeline runs concurrently, as a separate thread.

    The 'add' command in your example does not consume or produce data on standard in/out and is thus not suitable to run in a pipeline.

    If you just want the commands to run sequentially, then use the ';' command separator:

    g! add 1; add 2; add 3; add 4; show