Skip to content

Commit c925c16

Browse files
author
Jesse Bouwman
committed
Work in progress: experimentation with console output
Several open issues mention problems with interaction of the global `--encoding=` flag and the Encoders and PostRun fields of command structs. This branch contains experimental refactors that explore approaches to consistent command execution patterns across offline, online and http modes. Specific tickets: - ipfs/kubo#7050 json encoding for `ls` - ipfs/kubo#1121 json encoding for `add` - ipfs/kubo#5594 json encoding for `stats bw` - ipfs#115 postrun design Possibly related: - ipfs/kubo#6640 global flags on subcommands Incomplete PRs: - ipfs/kubo#5620 json for 'stat'
1 parent 744f3b3 commit c925c16

File tree

5 files changed

+29
-22
lines changed

5 files changed

+29
-22
lines changed

cli/parse.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ func Parse(ctx context.Context, input []string, stdin *os.File, root *cmds.Comma
4545
return req, err
4646
}
4747

48-
// if no encoding was specified by user, default to plaintext encoding
49-
// (if command doesn't support plaintext, use JSON instead)
50-
if enc := req.Options[cmds.EncLong]; enc == "" {
51-
if req.Command.Encoders != nil && req.Command.Encoders[cmds.Text] != nil {
52-
req.SetOption(cmds.EncLong, cmds.Text)
53-
} else {
54-
req.SetOption(cmds.EncLong, cmds.JSON)
55-
}
56-
}
57-
5848
return req, nil
5949
}
6050

cli/run.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ func Run(ctx context.Context, root *cmds.Command,
102102
return nil
103103
}
104104

105-
cmd := req.Command
106-
107105
env, err := buildEnv(req.Context, req)
108106
if err != nil {
109107
printErr(err)
@@ -119,14 +117,6 @@ func Run(ctx context.Context, root *cmds.Command,
119117
return err
120118
}
121119

122-
encTypeStr, _ := req.Options[cmds.EncLong].(string)
123-
encType := cmds.EncodingType(encTypeStr)
124-
125-
// use JSON if text was requested but the command doesn't have a text-encoder
126-
if _, ok := cmd.Encoders[encType]; encType == cmds.Text && !ok {
127-
req.Options[cmds.EncLong] = cmds.JSON
128-
}
129-
130120
re, err := NewResponseEmitter(stdout, stderr, req)
131121
if err != nil {
132122
printErr(err)

command.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package cmds
1111
import (
1212
"errors"
1313
"fmt"
14+
"io"
1415
"strings"
1516

1617
files "github.com/ipfs/go-ipfs-files"
@@ -30,6 +31,10 @@ type Function func(*Request, ResponseEmitter, Environment) error
3031
// PostRunMap is the map used in Command.PostRun.
3132
type PostRunMap map[PostRunType]func(Response, ResponseEmitter) error
3233

34+
// DisplayCLI supplies an function for console output in local
35+
// processes in combination with a text Encoder.
36+
type DisplayCLI func(res Response, stdout, stderr io.Writer) error
37+
3338
// Command is a runnable command, with input arguments and options (flags).
3439
// It can also have Subcommands, to group units of work into sets.
3540
type Command struct {
@@ -62,6 +67,10 @@ type Command struct {
6267
// the local process.
6368
PostRun PostRunMap
6469

70+
// DisplayCLI is for text output in local process in
71+
// combination with 'text' Encoder.
72+
DisplayCLI DisplayCLI
73+
6574
// Encoders encode results from Run (and/or PostRun) in the desired
6675
// encoding.
6776
Encoders EncoderMap

executor.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmds
22

33
import (
44
"context"
5+
"os"
56
)
67

78
type Executor interface {
@@ -51,9 +52,20 @@ func (x *executor) Execute(req *Request, re ResponseEmitter, env Environment) er
5152
}
5253
}
5354

54-
// contains the error returned by PostRun
55+
// contains the error returned by DisplayCLI or PostRun
5556
errCh := make(chan error, 1)
56-
if cmd.PostRun != nil {
57+
if cmd.DisplayCLI != nil && GetEncoding(req, "json") == "text" {
58+
var (
59+
res Response
60+
)
61+
62+
re, res = NewChanResponsePair(req)
63+
64+
go func() {
65+
defer close(errCh)
66+
errCh <- cmd.DisplayCLI(res, os.Stdout, os.Stderr)
67+
}()
68+
} else if cmd.PostRun != nil {
5769
if typer, ok := re.(interface {
5870
Type() PostRunType
5971
}); ok && cmd.PostRun[typer.Type()] != nil {

http/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"net/http"
99
"net/url"
10+
"os"
1011
"strings"
1112

1213
"github.com/ipfs/go-ipfs-cmds"
@@ -132,6 +133,11 @@ func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.En
132133
}
133134
}
134135

136+
if cmd.DisplayCLI != nil &&
137+
cmds.GetEncoding(req, cmds.Undefined) == cmds.Text {
138+
return cmd.DisplayCLI(res, os.Stdout, os.Stderr)
139+
}
140+
135141
return cmds.Copy(re, res)
136142
}
137143

0 commit comments

Comments
 (0)