forked from ipfs/kubo
/
commands.go
191 lines (162 loc) · 4.28 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Package commands implements the ipfs command interface
//
// Using github.com/ipfs/go-ipfs/commands to define the command line and HTTP
// APIs. This is the interface available to folks using IPFS from outside of
// the Go language.
package commands
import (
"fmt"
"io"
"os"
"sort"
"strings"
e "github.com/ipfs/go-ipfs/core/commands/e"
cmds "gx/ipfs/QmQkW9fnCsg9SLHdViiAh6qfBppodsPZVpU92dZLqYtEfs/go-ipfs-cmds"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
type commandEncoder struct {
w io.Writer
}
func (e *commandEncoder) Encode(v interface{}) error {
var (
cmd *Command
ok bool
)
if cmd, ok = v.(*Command); !ok {
return fmt.Errorf(`core/commands: uenxpected type %T, expected *"core/commands".Command`, v)
}
for _, s := range cmdPathStrings(cmd, cmd.showOpts) {
_, err := e.w.Write([]byte(s + "\n"))
if err != nil {
return err
}
}
return nil
}
type Command struct {
Name string
Subcommands []Command
Options []Option
showOpts bool
}
type Option struct {
Names []string
}
const (
flagsOptionName = "flags"
)
// CommandsCmd takes in a root command,
// and returns a command that lists the subcommands in that root
func CommandsCmd(root *cmds.Command) *cmds.Command {
return &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List all available commands.",
ShortDescription: `Lists all available commands (and subcommands) and exits.`,
},
Options: []cmdkit.Option{
cmdkit.BoolOption(flagsOptionName, "f", "Show command flags"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
rootCmd := cmd2outputCmd("ipfs", root)
rootCmd.showOpts, _ = req.Options[flagsOptionName].(bool)
return cmds.EmitOnce(res, &rootCmd)
},
Encoders: cmds.EncoderMap{
cmds.Text: func(req *cmds.Request) func(io.Writer) cmds.Encoder {
return func(w io.Writer) cmds.Encoder { return &commandEncoder{w} }
},
},
Type: Command{},
}
}
func cmd2outputCmd(name string, cmd *cmds.Command) Command {
opts := make([]Option, len(cmd.Options))
for i, opt := range cmd.Options {
opts[i] = Option{opt.Names()}
}
output := Command{
Name: name,
Subcommands: make([]Command, 0, len(cmd.Subcommands)),
Options: opts,
}
for name, sub := range cmd.Subcommands {
output.Subcommands = append(output.Subcommands, cmd2outputCmd(name, sub))
}
return output
}
func cmdPathStrings(cmd *Command, showOptions bool) []string {
var cmds []string
var recurse func(prefix string, cmd *Command)
recurse = func(prefix string, cmd *Command) {
newPrefix := prefix + cmd.Name
cmds = append(cmds, newPrefix)
if prefix != "" && showOptions {
for _, options := range cmd.Options {
var cmdOpts []string
for _, flag := range options.Names {
if len(flag) == 1 {
flag = "-" + flag
} else {
flag = "--" + flag
}
cmdOpts = append(cmdOpts, newPrefix+" "+flag)
}
cmds = append(cmds, strings.Join(cmdOpts, " / "))
}
}
for _, sub := range cmd.Subcommands {
recurse(newPrefix+" ", &sub)
}
}
recurse("", cmd)
sort.Sort(sort.StringSlice(cmds))
return cmds
}
// changes here will also need to be applied at
// - ./dag/dag.go
// - ./object/object.go
// - ./files/files.go
// - ./unixfs/unixfs.go
func unwrapOutput(i interface{}) (interface{}, error) {
var (
ch <-chan interface{}
ok bool
)
if ch, ok = i.(<-chan interface{}); !ok {
return nil, e.TypeErr(ch, i)
}
return <-ch, nil
}
type nonFatalError string
// streamResult is a helper function to stream results that possibly
// contain non-fatal errors. The helper function is allowed to panic
// on internal errors.
func streamResult(procVal func(interface{}, io.Writer) nonFatalError) func(cmds.Response, cmds.ResponseEmitter) error {
return func(res cmds.Response, re cmds.ResponseEmitter) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("internal error: %v", r)
}
re.Close()
}()
var errors bool
for {
v, err := res.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
errorMsg := procVal(v, os.Stdout)
if errorMsg != "" {
errors = true
fmt.Fprintf(os.Stderr, "%s\n", errorMsg)
}
}
if errors {
return fmt.Errorf("errors while displaying some entries")
}
return nil
}
}