updated multiaddr + multiaddr-net
I broke out multiaddr/net to its own package so that multiaddr parsing could remain lean. multiaddr-net will vendor special implementations of protocols (like utp)
Juan Batiz-Benet committed
Nov 20, 2014 at 00:58 UTC
43452fdeb763da66a30610d2449dd25ef7aa12fa
47 files changed
+312
-2121
Godeps/Godeps.json
+7
-11
@@ -1,6 +1,6 @@
1
{
2
"ImportPath": "github.com/jbenet/go-ipfs",
3
- "GoVersion": "go1.3.3",
3
+ "GoVersion": "go1.3",
4
"Packages": [
5
"./..."
6
],
@@ -73,10 +73,6 @@
73
"ImportPath": "github.com/facebookgo/stackerr",
74
"Rev": "060fbf9364c89acd41bf710e9e92915a90e7a5b5"
75
},
76
- {
77
- "ImportPath": "github.com/gonuts/flag",
78
- "Rev": "741a6cbd37a30dedc93f817e7de6aaf0ca38a493"
79
- },
76
{
77
"ImportPath": "github.com/gorilla/context",
78
"Rev": "14f550f51af52180c2eefed15e5fd18d63c0a64a"
@@ -89,10 +85,6 @@
85
"ImportPath": "github.com/inconshreveable/go-update",
86
"Rev": "221d034a558b4c21b0624b2a450c076913854a57"
87
},
92
- {
93
- "ImportPath": "github.com/jbenet/commander",
94
- "Rev": "e0cf317891f0ab6f1ac64dfcb754b4fb5e69f7df"
95
- },
88
{
89
"ImportPath": "github.com/jbenet/go-base58",
90
"Rev": "568a28d73fd97651d3442392036a658b6976eed5"
@@ -115,8 +107,12 @@
107
},
108
{
109
"ImportPath": "github.com/jbenet/go-multiaddr",
118
- "Comment": "0.1.2-27-g62a88e0",
119
- "Rev": "62a88e015e1bf5d6aaca29aec1aba0722f21c8d3"
110
+ "Comment": "0.1.2-30-g99cf3ed",
111
+ "Rev": "99cf3edc711751cf7b43505fac0e3913f6b9a75c"
112
+ },
113
+ {
114
+ "ImportPath": "github.com/jbenet/go-multiaddr-net",
115
+ "Rev": "625fac6e5073702ac9cd6028b9dc2457fd9cbf9d"
116
},
117
{
118
"ImportPath": "github.com/jbenet/go-multihash",
Godeps/_workspace/src/github.com/gonuts/flag/LICENSE
deleted
-27
@@ -1,27 +0,0 @@
1
-Copyright (c) 2012 The Go Authors. All rights reserved.
2
-
3
-Redistribution and use in source and binary forms, with or without
4
-modification, are permitted provided that the following conditions are
5
-met:
6
-
7
- * Redistributions of source code must retain the above copyright
8
-notice, this list of conditions and the following disclaimer.
9
- * Redistributions in binary form must reproduce the above
10
-copyright notice, this list of conditions and the following disclaimer
11
-in the documentation and/or other materials provided with the
12
-distribution.
13
- * Neither the name of Google Inc. nor the names of its
14
-contributors may be used to endorse or promote products derived from
15
-this software without specific prior written permission.
16
-
17
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
-A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
-OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
-SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
-LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Godeps/_workspace/src/github.com/gonuts/flag/README.md
deleted
-6
@@ -1,6 +0,0 @@
1
-flag
2
-=======
3
-
4
-[](https://drone.io/github.com/gonuts/flag/latest)
5
-
6
-A fork of the official "flag" package but with the flag.Value interface extended to provide a ``Get() interface{}`` method.
Godeps/_workspace/src/github.com/gonuts/flag/example_test.go
deleted
-83
@@ -1,83 +0,0 @@
1
-// Copyright 2012 The Go Authors. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-// These examples demonstrate more intricate uses of the flag package.
6
-package flag_test
7
-
8
-import (
9
- "errors"
10
- "flag"
11
- "fmt"
12
- "strings"
13
- "time"
14
-)
15
-
16
-// Example 1: A single string flag called "species" with default value "gopher".
17
-var species = flag.String("species", "gopher", "the species we are studying")
18
-
19
-// Example 2: Two flags sharing a variable, so we can have a shorthand.
20
-// The order of initialization is undefined, so make sure both use the
21
-// same default value. They must be set up with an init function.
22
-var gopherType string
23
-
24
-func init() {
25
- const (
26
- defaultGopher = "pocket"
27
- usage = "the variety of gopher"
28
- )
29
- flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
30
- flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")
31
-}
32
-
33
-// Example 3: A user-defined flag type, a slice of durations.
34
-type interval []time.Duration
35
-
36
-// String is the method to format the flag's value, part of the flag.Value interface.
37
-// The String method's output will be used in diagnostics.
38
-func (i *interval) String() string {
39
- return fmt.Sprint(*i)
40
-}
41
-
42
-// Set is the method to set the flag value, part of the flag.Value interface.
43
-// Set's argument is a string to be parsed to set the flag.
44
-// It's a comma-separated list, so we split it.
45
-func (i *interval) Set(value string) error {
46
- // If we wanted to allow the flag to be set multiple times,
47
- // accumulating values, we would delete this if statement.
48
- // That would permit usages such as
49
- // -deltaT 10s -deltaT 15s
50
- // and other combinations.
51
- if len(*i) > 0 {
52
- return errors.New("interval flag already set")
53
- }
54
- for _, dt := range strings.Split(value, ",") {
55
- duration, err := time.ParseDuration(dt)
56
- if err != nil {
57
- return err
58
- }
59
- *i = append(*i, duration)
60
- }
61
- return nil
62
-}
63
-
64
-// Define a flag to accumulate durations. Because it has a special type,
65
-// we need to use the Var function and therefore create the flag during
66
-// init.
67
-
68
-var intervalFlag interval
69
-
70
-func init() {
71
- // Tie the command-line flag to the intervalFlag variable and
72
- // set a usage message.
73
- flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
74
-}
75
-
76
-func Example() {
77
- // All the interesting pieces are with the variables declared above, but
78
- // to enable the flag package to see the flags defined there, one must
79
- // execute, typically at the start of main (not init!):
80
- // flag.Parse()
81
- // We don't run it here because this is not a main function and
82
- // the testing suite has already parsed the flags.
83
-}
Godeps/_workspace/src/github.com/gonuts/flag/export_test.go
deleted
-22
@@ -1,22 +0,0 @@
1
-// Copyright 2010 The Go Authors. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package flag
6
-
7
-import "os"
8
-
9
-// Additional routines compiled into the package only during testing.
10
-
11
-// ResetForTesting clears all flag state and sets the usage function as directed.
12
-// After calling ResetForTesting, parse errors in flag handling will not
13
-// exit the program.
14
-func ResetForTesting(usage func()) {
15
- commandLine = NewFlagSet(os.Args[0], ContinueOnError)
16
- Usage = usage
17
-}
18
-
19
-// CommandLine returns the default FlagSet.
20
-func CommandLine() *FlagSet {
21
- return commandLine
22
-}
Godeps/_workspace/src/github.com/gonuts/flag/flag.go
deleted
-816
@@ -1,816 +0,0 @@
1
-// Copyright 2009 The Go Authors. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-/*
6
- Package flag implements command-line flag parsing.
7
-
8
- Usage:
9
-
10
- Define flags using flag.String(), Bool(), Int(), etc.
11
-
12
- This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
13
- import "flag"
14
- var ip = flag.Int("flagname", 1234, "help message for flagname")
15
- If you like, you can bind the flag to a variable using the Var() functions.
16
- var flagvar int
17
- func init() {
18
- flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
19
- }
20
- Or you can create custom flags that satisfy the Value interface (with
21
- pointer receivers) and couple them to flag parsing by
22
- flag.Var(&flagVal, "name", "help message for flagname")
23
- For such flags, the default value is just the initial value of the variable.
24
-
25
- After all flags are defined, call
26
- flag.Parse()
27
- to parse the command line into the defined flags.
28
-
29
- Flags may then be used directly. If you're using the flags themselves,
30
- they are all pointers; if you bind to variables, they're values.
31
- fmt.Println("ip has value ", *ip)
32
- fmt.Println("flagvar has value ", flagvar)
33
-
34
- After parsing, the arguments after the flag are available as the
35
- slice flag.Args() or individually as flag.Arg(i).
36
- The arguments are indexed from 0 up to flag.NArg().
37
-
38
- Command line flag syntax:
39
- -flag
40
- -flag=x
41
- -flag x // non-boolean flags only
42
- One or two minus signs may be used; they are equivalent.
43
- The last form is not permitted for boolean flags because the
44
- meaning of the command
45
- cmd -x *
46
- will change if there is a file called 0, false, etc. You must
47
- use the -flag=false form to turn off a boolean flag.
48
-
49
- Flag parsing stops just before the first non-flag argument
50
- ("-" is a non-flag argument) or after the terminator "--".
51
-
52
- Integer flags accept 1234, 0664, 0x1234 and may be negative.
53
- Boolean flags may be 1, 0, t, f, true, false, TRUE, FALSE, True, False.
54
- Duration flags accept any input valid for time.ParseDuration.
55
-
56
- The default set of command-line flags is controlled by
57
- top-level functions. The FlagSet type allows one to define
58
- independent sets of flags, such as to implement subcommands
59
- in a command-line interface. The methods of FlagSet are
60
- analogous to the top-level functions for the command-line
61
- flag set.
62
-*/
63
-package flag
64
-
65
-import (
66
- "errors"
67
- "fmt"
68
- "io"
69
- "os"
70
- "sort"
71
- "strconv"
72
- "time"
73
-)
74
-
75
-// ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
76
-var ErrHelp = errors.New("flag: help requested")
77
-
78
-// -- bool Value
79
-type boolValue bool
80
-
81
-func newBoolValue(val bool, p *bool) *boolValue {
82
- *p = val
83
- return (*boolValue)(p)
84
-}
85
-
86
-func (b *boolValue) Set(s string) error {
87
- v, err := strconv.ParseBool(s)
88
- *b = boolValue(v)
89
- return err
90
-}
91
-
92
-func (b *boolValue) Get() interface{} { return bool(*b) }
93
-
94
-func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
95
-
96
-// -- int Value
97
-type intValue int
98
-
99
-func newIntValue(val int, p *int) *intValue {
100
- *p = val
101
- return (*intValue)(p)
102
-}
103
-
104
-func (i *intValue) Set(s string) error {
105
- v, err := strconv.ParseInt(s, 0, 64)
106
- *i = intValue(v)
107
- return err
108
-}
109
-
110
-func (i *intValue) Get() interface{} { return int(*i) }
111
-
112
-func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
113
-
114
-// -- int64 Value
115
-type int64Value int64
116
-
117
-func newInt64Value(val int64, p *int64) *int64Value {
118
- *p = val
119
- return (*int64Value)(p)
120
-}
121
-
122
-func (i *int64Value) Set(s string) error {
123
- v, err := strconv.ParseInt(s, 0, 64)
124
- *i = int64Value(v)
125
- return err
126
-}
127
-
128
-func (i *int64Value) Get() interface{} { return int64(*i) }
129
-
130
-func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
131
-
132
-// -- uint Value
133
-type uintValue uint
134
-
135
-func newUintValue(val uint, p *uint) *uintValue {
136
- *p = val
137
- return (*uintValue)(p)
138
-}
139
-
140
-func (i *uintValue) Set(s string) error {
141
- v, err := strconv.ParseUint(s, 0, 64)
142
- *i = uintValue(v)
143
- return err
144
-}
145
-
146
-func (i *uintValue) Get() interface{} { return uint(*i) }
147
-
148
-func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
149
-
150
-// -- uint64 Value
151
-type uint64Value uint64
152
-
153
-func newUint64Value(val uint64, p *uint64) *uint64Value {
154
- *p = val
155
- return (*uint64Value)(p)
156
-}
157
-
158
-func (i *uint64Value) Set(s string) error {
159
- v, err := strconv.ParseUint(s, 0, 64)
160
- *i = uint64Value(v)
161
- return err
162
-}
163
-
164
-func (i *uint64Value) Get() interface{} { return uint64(*i) }
165
-
166
-func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
167
-
168
-// -- string Value
169
-type stringValue string
170
-
171
-func newStringValue(val string, p *string) *stringValue {
172
- *p = val
173
- return (*stringValue)(p)
174
-}
175
-
176
-func (s *stringValue) Set(val string) error {
177
- *s = stringValue(val)
178
- return nil
179
-}
180
-
181
-func (s *stringValue) Get() interface{} { return s.String() }
182
-
183
-func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
184
-
185
-// -- float64 Value
186
-type float64Value float64
187
-
188
-func newFloat64Value(val float64, p *float64) *float64Value {
189
- *p = val
190
- return (*float64Value)(p)
191
-}
192
-
193
-func (f *float64Value) Set(s string) error {
194
- v, err := strconv.ParseFloat(s, 64)
195
- *f = float64Value(v)
196
- return err
197
-}
198
-
199
-func (f *float64Value) Get() interface{} { return float64(*f) }
200
-
201
-func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
202
-
203
-// -- time.Duration Value
204
-type durationValue time.Duration
205
-
206
-func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
207
- *p = val
208
- return (*durationValue)(p)
209
-}
210
-
211
-func (d *durationValue) Set(s string) error {
212
- v, err := time.ParseDuration(s)
213
- *d = durationValue(v)
214
- return err
215
-}
216
-
217
-func (d *durationValue) Get() interface{} { return *(*time.Duration)(d) }
218
-
219
-func (d *durationValue) String() string { return (*time.Duration)(d).String() }
220
-
221
-// Value is the interface to the dynamic value stored in a flag.
222
-// (The default value is represented as a string.)
223
-type Value interface {
224
- String() string
225
- Set(string) error
226
- Get() interface{}
227
-}
228
-
229
-// ErrorHandling defines how to handle flag parsing errors.
230
-type ErrorHandling int
231
-
232
-const (
233
- ContinueOnError ErrorHandling = iota
234
- ExitOnError
235
- PanicOnError
236
-)
237
-
238
-// A FlagSet represents a set of defined flags.
239
-type FlagSet struct {
240
- // Usage is the function called when an error occurs while parsing flags.
241
- // The field is a function (not a method) that may be changed to point to
242
- // a custom error handler.
243
- Usage func()
244
-
245
- name string
246
- parsed bool
247
- actual map[string]*Flag
248
- formal map[string]*Flag
249
- args []string // arguments after flags
250
- exitOnError bool // does the program exit if there's an error?
251
- errorHandling ErrorHandling
252
- output io.Writer // nil means stderr; use out() accessor
253
-}
254
-
255
-// A Flag represents the state of a flag.
256
-type Flag struct {
257
- Name string // name as it appears on command line
258
- Usage string // help message
259
- Value Value // value as set
260
- DefValue string // default value (as text); for usage message
261
-}
262
-
263
-// sortFlags returns the flags as a slice in lexicographical sorted order.
264
-func sortFlags(flags map[string]*Flag) []*Flag {
265
- list := make(sort.StringSlice, len(flags))
266
- i := 0
267
- for _, f := range flags {
268
- list[i] = f.Name
269
- i++
270
- }
271
- list.Sort()
272
- result := make([]*Flag, len(list))
273
- for i, name := range list {
274
- result[i] = flags[name]
275
- }
276
- return result
277
-}
278
-
279
-func (f *FlagSet) out() io.Writer {
280
- if f.output == nil {
281
- return os.Stderr
282
- }
283
- return f.output
284
-}
285
-
286
-// SetOutput sets the destination for usage and error messages.
287
-// If output is nil, os.Stderr is used.
288
-func (f *FlagSet) SetOutput(output io.Writer) {
289
- f.output = output
290
-}
291
-
292
-// VisitAll visits the flags in lexicographical order, calling fn for each.
293
-// It visits all flags, even those not set.
294
-func (f *FlagSet) VisitAll(fn func(*Flag)) {
295
- for _, flag := range sortFlags(f.formal) {
296
- fn(flag)
297
- }
298
-}
299
-
300
-// VisitAll visits the command-line flags in lexicographical order, calling
301
-// fn for each. It visits all flags, even those not set.
302
-func VisitAll(fn func(*Flag)) {
303
- commandLine.VisitAll(fn)
304
-}
305
-
306
-// Visit visits the flags in lexicographical order, calling fn for each.
307
-// It visits only those flags that have been set.
308
-func (f *FlagSet) Visit(fn func(*Flag)) {
309
- for _, flag := range sortFlags(f.actual) {
310
- fn(flag)
311
- }
312
-}
313
-
314
-// Visit visits the command-line flags in lexicographical order, calling fn
315
-// for each. It visits only those flags that have been set.
316
-func Visit(fn func(*Flag)) {
317
- commandLine.Visit(fn)
318
-}
319
-
320
-// Lookup returns the Flag structure of the named flag, returning nil if none exists.
321
-func (f *FlagSet) Lookup(name string) *Flag {
322
- return f.formal[name]
323
-}
324
-
325
-// Lookup returns the Flag structure of the named command-line flag,
326
-// returning nil if none exists.
327
-func Lookup(name string) *Flag {
328
- return commandLine.formal[name]
329
-}
330
-
331
-// Set sets the value of the named flag.
332
-func (f *FlagSet) Set(name, value string) error {
333
- flag, ok := f.formal[name]
334
- if !ok {
335
- return fmt.Errorf("no such flag -%v", name)
336
- }
337
- err := flag.Value.Set(value)
338
- if err != nil {
339
- return err
340
- }
341
- if f.actual == nil {
342
- f.actual = make(map[string]*Flag)
343
- }
344
- f.actual[name] = flag
345
- return nil
346
-}
347
-
348
-// Set sets the value of the named command-line flag.
349
-func Set(name, value string) error {
350
- return commandLine.Set(name, value)
351
-}
352
-
353
-// PrintDefaults prints, to standard error unless configured
354
-// otherwise, the default values of all defined flags in the set.
355
-func (f *FlagSet) PrintDefaults() {
356
- f.VisitAll(func(flag *Flag) {
357
- format := " -%s=%s: %s\n"
358
- if _, ok := flag.Value.(*stringValue); ok {
359
- // put quotes on the value
360
- format = " -%s=%q: %s\n"
361
- }
362
- fmt.Fprintf(f.out(), format, flag.Name, flag.DefValue, flag.Usage)
363
- })
364
-}
365
-
366
-// PrintDefaults prints to standard error the default values of all defined command-line flags.
367
-func PrintDefaults() {
368
- commandLine.PrintDefaults()
369
-}
370
-
371
-// defaultUsage is the default function to print a usage message.
372
-func defaultUsage(f *FlagSet) {
373
- fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
374
- f.PrintDefaults()
375
-}
376
-
377
-// NOTE: Usage is not just defaultUsage(commandLine)
378
-// because it serves (via godoc flag Usage) as the example
379
-// for how to write your own usage function.
380
-
381
-// Usage prints to standard error a usage message documenting all defined command-line flags.
382
-// The function is a variable that may be changed to point to a custom function.
383
-var Usage = func() {
384
- fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
385
- PrintDefaults()
386
-}
387
-
388
-// NFlag returns the number of flags that have been set.
389
-func (f *FlagSet) NFlag() int { return len(f.actual) }
390
-
391
-// NFlag returns the number of command-line flags that have been set.
392
-func NFlag() int { return len(commandLine.actual) }
393
-
394
-// Arg returns the i'th argument. Arg(0) is the first remaining argument
395
-// after flags have been processed.
396
-func (f *FlagSet) Arg(i int) string {
397
- if i < 0 || i >= len(f.args) {
398
- return ""
399
- }
400
- return f.args[i]
401
-}
402
-
403
-// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
404
-// after flags have been processed.
405
-func Arg(i int) string {
406
- return commandLine.Arg(i)
407
-}
408
-
409
-// NArg is the number of arguments remaining after flags have been processed.
410
-func (f *FlagSet) NArg() int { return len(f.args) }
411
-
412
-// NArg is the number of arguments remaining after flags have been processed.
413
-func NArg() int { return len(commandLine.args) }
414
-
415
-// Args returns the non-flag arguments.
416
-func (f *FlagSet) Args() []string { return f.args }
417
-
418
-// Args returns the non-flag command-line arguments.
419
-func Args() []string { return commandLine.args }
420
-
421
-// BoolVar defines a bool flag with specified name, default value, and usage string.
422
-// The argument p points to a bool variable in which to store the value of the flag.
423
-func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
424
- f.Var(newBoolValue(value, p), name, usage)
425
-}
426
-
427
-// BoolVar defines a bool flag with specified name, default value, and usage string.
428
-// The argument p points to a bool variable in which to store the value of the flag.
429
-func BoolVar(p *bool, name string, value bool, usage string) {
430
- commandLine.Var(newBoolValue(value, p), name, usage)
431
-}
432
-
433
-// Bool defines a bool flag with specified name, default value, and usage string.
434
-// The return value is the address of a bool variable that stores the value of the flag.
435
-func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
436
- p := new(bool)
437
- f.BoolVar(p, name, value, usage)
438
- return p
439
-}
440
-
441
-// Bool defines a bool flag with specified name, default value, and usage string.
442
-// The return value is the address of a bool variable that stores the value of the flag.
443
-func Bool(name string, value bool, usage string) *bool {
444
- return commandLine.Bool(name, value, usage)
445
-}
446
-
447
-// IntVar defines an int flag with specified name, default value, and usage string.
448
-// The argument p points to an int variable in which to store the value of the flag.
449
-func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
450
- f.Var(newIntValue(value, p), name, usage)
451
-}
452
-
453
-// IntVar defines an int flag with specified name, default value, and usage string.
454
-// The argument p points to an int variable in which to store the value of the flag.
455
-func IntVar(p *int, name string, value int, usage string) {
456
- commandLine.Var(newIntValue(value, p), name, usage)
457
-}
458
-
459
-// Int defines an int flag with specified name, default value, and usage string.
460
-// The return value is the address of an int variable that stores the value of the flag.
461
-func (f *FlagSet) Int(name string, value int, usage string) *int {
462
- p := new(int)
463
- f.IntVar(p, name, value, usage)
464
- return p
465
-}
466
-
467
-// Int defines an int flag with specified name, default value, and usage string.
468
-// The return value is the address of an int variable that stores the value of the flag.
469
-func Int(name string, value int, usage string) *int {
470
- return commandLine.Int(name, value, usage)
471
-}
472
-
473
-// Int64Var defines an int64 flag with specified name, default value, and usage string.
474
-// The argument p points to an int64 variable in which to store the value of the flag.
475
-func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
476
- f.Var(newInt64Value(value, p), name, usage)
477
-}
478
-
479
-// Int64Var defines an int64 flag with specified name, default value, and usage string.
480
-// The argument p points to an int64 variable in which to store the value of the flag.
481
-func Int64Var(p *int64, name string, value int64, usage string) {
482
- commandLine.Var(newInt64Value(value, p), name, usage)
483
-}
484
-
485
-// Int64 defines an int64 flag with specified name, default value, and usage string.
486
-// The return value is the address of an int64 variable that stores the value of the flag.
487
-func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
488
- p := new(int64)
489
- f.Int64Var(p, name, value, usage)
490
- return p
491
-}
492
-
493
-// Int64 defines an int64 flag with specified name, default value, and usage string.
494
-// The return value is the address of an int64 variable that stores the value of the flag.
495
-func Int64(name string, value int64, usage string) *int64 {
496
- return commandLine.Int64(name, value, usage)
497
-}
498
-
499
-// UintVar defines a uint flag with specified name, default value, and usage string.
500
-// The argument p points to a uint variable in which to store the value of the flag.
501
-func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
502
- f.Var(newUintValue(value, p), name, usage)
503
-}
504
-
505
-// UintVar defines a uint flag with specified name, default value, and usage string.
506
-// The argument p points to a uint variable in which to store the value of the flag.
507
-func UintVar(p *uint, name string, value uint, usage string) {
508
- commandLine.Var(newUintValue(value, p), name, usage)
509
-}
510
-
511
-// Uint defines a uint flag with specified name, default value, and usage string.
512
-// The return value is the address of a uint variable that stores the value of the flag.
513
-func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
514
- p := new(uint)
515
- f.UintVar(p, name, value, usage)
516
- return p
517
-}
518
-
519
-// Uint defines a uint flag with specified name, default value, and usage string.
520
-// The return value is the address of a uint variable that stores the value of the flag.
521
-func Uint(name string, value uint, usage string) *uint {
522
- return commandLine.Uint(name, value, usage)
523
-}
524
-
525
-// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
526
-// The argument p points to a uint64 variable in which to store the value of the flag.
527
-func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) {
528
- f.Var(newUint64Value(value, p), name, usage)
529
-}
530
-
531
-// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
532
-// The argument p points to a uint64 variable in which to store the value of the flag.
533
-func Uint64Var(p *uint64, name string, value uint64, usage string) {
534
- commandLine.Var(newUint64Value(value, p), name, usage)
535
-}
536
-
537
-// Uint64 defines a uint64 flag with specified name, default value, and usage string.
538
-// The return value is the address of a uint64 variable that stores the value of the flag.
539
-func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 {
540
- p := new(uint64)
541
- f.Uint64Var(p, name, value, usage)
542
- return p
543
-}
544
-
545
-// Uint64 defines a uint64 flag with specified name, default value, and usage string.
546
-// The return value is the address of a uint64 variable that stores the value of the flag.
547
-func Uint64(name string, value uint64, usage string) *uint64 {
548
- return commandLine.Uint64(name, value, usage)
549
-}
550
-
551
-// StringVar defines a string flag with specified name, default value, and usage string.
552
-// The argument p points to a string variable in which to store the value of the flag.
553
-func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
554
- f.Var(newStringValue(value, p), name, usage)
555
-}
556
-
557
-// StringVar defines a string flag with specified name, default value, and usage string.
558
-// The argument p points to a string variable in which to store the value of the flag.
559
-func StringVar(p *string, name string, value string, usage string) {
560
- commandLine.Var(newStringValue(value, p), name, usage)
561
-}
562
-
563
-// String defines a string flag with specified name, default value, and usage string.
564
-// The return value is the address of a string variable that stores the value of the flag.
565
-func (f *FlagSet) String(name string, value string, usage string) *string {
566
- p := new(string)
567
- f.StringVar(p, name, value, usage)
568
- return p
569
-}
570
-
571
-// String defines a string flag with specified name, default value, and usage string.
572
-// The return value is the address of a string variable that stores the value of the flag.
573
-func String(name string, value string, usage string) *string {
574
- return commandLine.String(name, value, usage)
575
-}
576
-
577
-// Float64Var defines a float64 flag with specified name, default value, and usage string.
578
-// The argument p points to a float64 variable in which to store the value of the flag.
579
-func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
580
- f.Var(newFloat64Value(value, p), name, usage)
581
-}
582
-
583
-// Float64Var defines a float64 flag with specified name, default value, and usage string.
584
-// The argument p points to a float64 variable in which to store the value of the flag.
585
-func Float64Var(p *float64, name string, value float64, usage string) {
586
- commandLine.Var(newFloat64Value(value, p), name, usage)
587
-}
588
-
589
-// Float64 defines a float64 flag with specified name, default value, and usage string.
590
-// The return value is the address of a float64 variable that stores the value of the flag.
591
-func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
592
- p := new(float64)
593
- f.Float64Var(p, name, value, usage)
594
- return p
595
-}
596
-
597
-// Float64 defines a float64 flag with specified name, default value, and usage string.
598
-// The return value is the address of a float64 variable that stores the value of the flag.
599
-func Float64(name string, value float64, usage string) *float64 {
600
- return commandLine.Float64(name, value, usage)
601
-}
602
-
603
-// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
604
-// The argument p points to a time.Duration variable in which to store the value of the flag.
605
-func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
606
- f.Var(newDurationValue(value, p), name, usage)
607
-}
608
-
609
-// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
610
-// The argument p points to a time.Duration variable in which to store the value of the flag.
611
-func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
612
- commandLine.Var(newDurationValue(value, p), name, usage)
613
-}
614
-
615
-// Duration defines a time.Duration flag with specified name, default value, and usage string.
616
-// The return value is the address of a time.Duration variable that stores the value of the flag.
617
-func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
618
- p := new(time.Duration)
619
- f.DurationVar(p, name, value, usage)
620
- return p
621
-}
622
-
623
-// Duration defines a time.Duration flag with specified name, default value, and usage string.
624
-// The return value is the address of a time.Duration variable that stores the value of the flag.
625
-func Duration(name string, value time.Duration, usage string) *time.Duration {
626
- return commandLine.Duration(name, value, usage)
627
-}
628
-
629
-// Var defines a flag with the specified name and usage string. The type and
630
-// value of the flag are represented by the first argument, of type Value, which
631
-// typically holds a user-defined implementation of Value. For instance, the
632
-// caller could create a flag that turns a comma-separated string into a slice
633
-// of strings by giving the slice the methods of Value; in particular, Set would
634
-// decompose the comma-separated string into the slice.
635
-func (f *FlagSet) Var(value Value, name string, usage string) {
636
- // Remember the default value as a string; it won't change.
637
- flag := &Flag{name, usage, value, value.String()}
638
- _, alreadythere := f.formal[name]
639
- if alreadythere {
640
- msg := fmt.Sprintf("%s flag redefined: %s", f.name, name)
641
- fmt.Fprintln(f.out(), msg)
642
- panic(msg) // Happens only if flags are declared with identical names
643
- }
644
- if f.formal == nil {
645
- f.formal = make(map[string]*Flag)
646
- }
647
- f.formal[name] = flag
648
-}
649
-
650
-// Var defines a flag with the specified name and usage string. The type and
651
-// value of the flag are represented by the first argument, of type Value, which
652
-// typically holds a user-defined implementation of Value. For instance, the
653
-// caller could create a flag that turns a comma-separated string into a slice
654
-// of strings by giving the slice the methods of Value; in particular, Set would
655
-// decompose the comma-separated string into the slice.
656
-func Var(value Value, name string, usage string) {
657
- commandLine.Var(value, name, usage)
658
-}
659
-
660
-// failf prints to standard error a formatted error and usage message and
661
-// returns the error.
662
-func (f *FlagSet) failf(format string, a ...interface{}) error {
663
- err := fmt.Errorf(format, a...)
664
- fmt.Fprintln(f.out(), err)
665
- f.usage()
666
- return err
667
-}
668
-
669
-// usage calls the Usage method for the flag set, or the usage function if
670
-// the flag set is commandLine.
671
-func (f *FlagSet) usage() {
672
- if f == commandLine {
673
- Usage()
674
- } else if f.Usage == nil {
675
- defaultUsage(f)
676
- } else {
677
- f.Usage()
678
- }
679
-}
680
-
681
-// parseOne parses one flag. It returns whether a flag was seen.
682
-func (f *FlagSet) parseOne() (bool, error) {
683
- if len(f.args) == 0 {
684
- return false, nil
685
- }
686
- s := f.args[0]
687
- if len(s) == 0 || s[0] != '-' || len(s) == 1 {
688
- return false, nil
689
- }
690
- num_minuses := 1
691
- if s[1] == '-' {
692
- num_minuses++
693
- if len(s) == 2 { // "--" terminates the flags
694
- f.args = f.args[1:]
695
- return false, nil
696
- }
697
- }
698
- name := s[num_minuses:]
699
- if len(name) == 0 || name[0] == '-' || name[0] == '=' {
700
- return false, f.failf("bad flag syntax: %s", s)
701
- }
702
-
703
- // it's a flag. does it have an argument?
704
- f.args = f.args[1:]
705
- has_value := false
706
- value := ""
707
- for i := 1; i < len(name); i++ { // equals cannot be first
708
- if name[i] == '=' {
709
- value = name[i+1:]
710
- has_value = true
711
- name = name[0:i]
712
- break
713
- }
714
- }
715
- m := f.formal
716
- flag, alreadythere := m[name] // BUG
717
- if !alreadythere {
718
- if name == "help" || name == "h" { // special case for nice help message.
719
- f.usage()
720
- return false, ErrHelp
721
- }
722
- return false, f.failf("flag provided but not defined: -%s", name)
723
- }
724
- if fv, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
725
- if has_value {
726
- if err := fv.Set(value); err != nil {
727
- f.failf("invalid boolean value %q for -%s: %v", value, name, err)
728
- }
729
- } else {
730
- fv.Set("true")
731
- }
732
- } else {
733
- // It must have a value, which might be the next argument.
734
- if !has_value && len(f.args) > 0 {
735
- // value is the next arg
736
- has_value = true
737
- value, f.args = f.args[0], f.args[1:]
738
- }
739
- if !has_value {
740
- return false, f.failf("flag needs an argument: -%s", name)
741
- }
742
- if err := flag.Value.Set(value); err != nil {
743
- return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
744
- }
745
- }
746
- if f.actual == nil {
747
- f.actual = make(map[string]*Flag)
748
- }
749
- f.actual[name] = flag
750
- return true, nil
751
-}
752
-
753
-// Parse parses flag definitions from the argument list, which should not
754
-// include the command name. Must be called after all flags in the FlagSet
755
-// are defined and before flags are accessed by the program.
756
-// The return value will be ErrHelp if -help was set but not defined.
757
-func (f *FlagSet) Parse(arguments []string) error {
758
- f.parsed = true
759
- f.args = arguments
760
- for {
761
- seen, err := f.parseOne()
762
- if seen {
763
- continue
764
- }
765
- if err == nil {
766
- break
767
- }
768
- switch f.errorHandling {
769
- case ContinueOnError:
770
- return err
771
- case ExitOnError:
772
- os.Exit(2)
773
- case PanicOnError:
774
- panic(err)
775
- }
776
- }
777
- return nil
778
-}
779
-
780
-// Parsed reports whether f.Parse has been called.
781
-func (f *FlagSet) Parsed() bool {
782
- return f.parsed
783
-}
784
-
785
-// Parse parses the command-line flags from os.Args[1:]. Must be called
786
-// after all flags are defined and before flags are accessed by the program.
787
-func Parse() {
788
- // Ignore errors; commandLine is set for ExitOnError.
789
- commandLine.Parse(os.Args[1:])
790
-}
791
-
792
-// Parsed returns true if the command-line flags have been parsed.
793
-func Parsed() bool {
794
- return commandLine.Parsed()
795
-}
796
-
797
-// The default set of command-line flags, parsed from os.Args.
798
-var commandLine = NewFlagSet(os.Args[0], ExitOnError)
799
-
800
-// NewFlagSet returns a new, empty flag set with the specified name and
801
-// error handling property.
802
-func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
803
- f := &FlagSet{
804
- name: name,
805
- errorHandling: errorHandling,
806
- }
807
- return f
808
-}
809
-
810
-// Init sets the name and error handling property for a flag set.
811
-// By default, the zero FlagSet uses an empty name and the
812
-// ContinueOnError error handling policy.
813
-func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
814
- f.name = name
815
- f.errorHandling = errorHandling
816
-}
Godeps/_workspace/src/github.com/gonuts/flag/flag_test.go
deleted
-288
@@ -1,288 +0,0 @@
1
-// Copyright 2009 The Go Authors. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package flag_test
6
-
7
-import (
8
- "bytes"
9
- "fmt"
10
- "os"
11
- "sort"
12
- "strings"
13
- "testing"
14
- "time"
15
-
16
- . "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
17
-)
18
-
19
-var (
20
- test_bool = Bool("test_bool", false, "bool value")
21
- test_int = Int("test_int", 0, "int value")
22
- test_int64 = Int64("test_int64", 0, "int64 value")
23
- test_uint = Uint("test_uint", 0, "uint value")
24
- test_uint64 = Uint64("test_uint64", 0, "uint64 value")
25
- test_string = String("test_string", "0", "string value")
26
- test_float64 = Float64("test_float64", 0, "float64 value")
27
- test_duration = Duration("test_duration", 0, "time.Duration value")
28
-)
29
-
30
-func boolString(s string) string {
31
- if s == "0" {
32
- return "false"
33
- }
34
- return "true"
35
-}
36
-
37
-func TestEverything(t *testing.T) {
38
- m := make(map[string]*Flag)
39
- desired := "0"
40
- visitor := func(f *Flag) {
41
- if len(f.Name) > 5 && f.Name[0:5] == "test_" {
42
- m[f.Name] = f
43
- ok := false
44
- switch {
45
- case f.Value.String() == desired:
46
- ok = true
47
- case f.Name == "test_bool" && f.Value.String() == boolString(desired):
48
- ok = true
49
- case f.Name == "test_duration" && f.Value.String() == desired+"s":
50
- ok = true
51
- }
52
- if !ok {
53
- t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
54
- }
55
- }
56
- }
57
- VisitAll(visitor)
58
- if len(m) != 8 {
59
- t.Error("VisitAll misses some flags")
60
- for k, v := range m {
61
- t.Log(k, *v)
62
- }
63
- }
64
- m = make(map[string]*Flag)
65
- Visit(visitor)
66
- if len(m) != 0 {
67
- t.Errorf("Visit sees unset flags")
68
- for k, v := range m {
69
- t.Log(k, *v)
70
- }
71
- }
72
- // Now set all flags
73
- Set("test_bool", "true")
74
- Set("test_int", "1")
75
- Set("test_int64", "1")
76
- Set("test_uint", "1")
77
- Set("test_uint64", "1")
78
- Set("test_string", "1")
79
- Set("test_float64", "1")
80
- Set("test_duration", "1s")
81
- desired = "1"
82
- Visit(visitor)
83
- if len(m) != 8 {
84
- t.Error("Visit fails after set")
85
- for k, v := range m {
86
- t.Log(k, *v)
87
- }
88
- }
89
- // Now test they're visited in sort order.
90
- var flagNames []string
91
- Visit(func(f *Flag) { flagNames = append(flagNames, f.Name) })
92
- if !sort.StringsAreSorted(flagNames) {
93
- t.Errorf("flag names not sorted: %v", flagNames)
94
- }
95
-}
96
-
97
-func TestUsage(t *testing.T) {
98
- called := false
99
- ResetForTesting(func() { called = true })
100
- if CommandLine().Parse([]string{"-x"}) == nil {
101
- t.Error("parse did not fail for unknown flag")
102
- }
103
- if !called {
104
- t.Error("did not call Usage for unknown flag")
105
- }
106
-}
107
-
108
-func testParse(f *FlagSet, t *testing.T) {
109
- if f.Parsed() {
110
- t.Error("f.Parse() = true before Parse")
111
- }
112
- boolFlag := f.Bool("bool", false, "bool value")
113
- bool2Flag := f.Bool("bool2", false, "bool2 value")
114
- intFlag := f.Int("int", 0, "int value")
115
- int64Flag := f.Int64("int64", 0, "int64 value")
116
- uintFlag := f.Uint("uint", 0, "uint value")
117
- uint64Flag := f.Uint64("uint64", 0, "uint64 value")
118
- stringFlag := f.String("string", "0", "string value")
119
- float64Flag := f.Float64("float64", 0, "float64 value")
120
- durationFlag := f.Duration("duration", 5*time.Second, "time.Duration value")
121
- extra := "one-extra-argument"
122
- args := []string{
123
- "-bool",
124
- "-bool2=true",
125
- "--int", "22",
126
- "--int64", "0x23",
127
- "-uint", "24",
128
- "--uint64", "25",
129
- "-string", "hello",
130
- "-float64", "2718e28",
131
- "-duration", "2m",
132
- extra,
133
- }
134
- if err := f.Parse(args); err != nil {
135
- t.Fatal(err)
136
- }
137
- if !f.Parsed() {
138
- t.Error("f.Parse() = false after Parse")
139
- }
140
- if *boolFlag != true {
141
- t.Error("bool flag should be true, is ", *boolFlag)
142
- }
143
- if *bool2Flag != true {
144
- t.Error("bool2 flag should be true, is ", *bool2Flag)
145
- }
146
- if *intFlag != 22 {
147
- t.Error("int flag should be 22, is ", *intFlag)
148
- }
149
- if *int64Flag != 0x23 {
150
- t.Error("int64 flag should be 0x23, is ", *int64Flag)
151
- }
152
- if *uintFlag != 24 {
153
- t.Error("uint flag should be 24, is ", *uintFlag)
154
- }
155
- if *uint64Flag != 25 {
156
- t.Error("uint64 flag should be 25, is ", *uint64Flag)
157
- }
158
- if *stringFlag != "hello" {
159
- t.Error("string flag should be `hello`, is ", *stringFlag)
160
- }
161
- if *float64Flag != 2718e28 {
162
- t.Error("float64 flag should be 2718e28, is ", *float64Flag)
163
- }
164
- if *durationFlag != 2*time.Minute {
165
- t.Error("duration flag should be 2m, is ", *durationFlag)
166
- }
167
- if len(f.Args()) != 1 {
168
- t.Error("expected one argument, got", len(f.Args()))
169
- } else if f.Args()[0] != extra {
170
- t.Errorf("expected argument %q got %q", extra, f.Args()[0])
171
- }
172
-}
173
-
174
-func TestParse(t *testing.T) {
175
- ResetForTesting(func() { t.Error("bad parse") })
176
- testParse(CommandLine(), t)
177
-}
178
-
179
-func TestFlagSetParse(t *testing.T) {
180
- testParse(NewFlagSet("test", ContinueOnError), t)
181
-}
182
-
183
-// Declare a user-defined flag type.
184
-type flagVar []string
185
-
186
-func (f *flagVar) String() string {
187
- return fmt.Sprint([]string(*f))
188
-}
189
-
190
-func (f *flagVar) Set(value string) error {
191
- *f = append(*f, value)
192
- return nil
193
-}
194
-
195
-func (f *flagVar) Get() interface{} { return []string(*f) }
196
-
197
-func TestUserDefined(t *testing.T) {
198
- var flags FlagSet
199
- flags.Init("test", ContinueOnError)
200
- var v flagVar
201
- flags.Var(&v, "v", "usage")
202
- if err := flags.Parse([]string{"-v", "1", "-v", "2", "-v=3"}); err != nil {
203
- t.Error(err)
204
- }
205
- if len(v) != 3 {
206
- t.Fatal("expected 3 args; got ", len(v))
207
- }
208
- expect := "[1 2 3]"
209
- if v.String() != expect {
210
- t.Errorf("expected value %q got %q", expect, v.String())
211
- }
212
-}
213
-
214
-func TestSetOutput(t *testing.T) {
215
- var flags FlagSet
216
- var buf bytes.Buffer
217
- flags.SetOutput(&buf)
218
- flags.Init("test", ContinueOnError)
219
- flags.Parse([]string{"-unknown"})
220
- if out := buf.String(); !strings.Contains(out, "-unknown") {
221
- t.Logf("expected output mentioning unknown; got %q", out)
222
- }
223
-}
224
-
225
-// This tests that one can reset the flags. This still works but not well, and is
226
-// superseded by FlagSet.
227
-func TestChangingArgs(t *testing.T) {
228
- ResetForTesting(func() { t.Fatal("bad parse") })
229
- oldArgs := os.Args
230
- defer func() { os.Args = oldArgs }()
231
- os.Args = []string{"cmd", "-before", "subcmd", "-after", "args"}
232
- before := Bool("before", false, "")
233
- if err := CommandLine().Parse(os.Args[1:]); err != nil {
234
- t.Fatal(err)
235
- }
236
- cmd := Arg(0)
237
- os.Args = Args()
238
- after := Bool("after", false, "")
239
- Parse()
240
- args := Args()
241
-
242
- if !*before || cmd != "subcmd" || !*after || len(args) != 1 || args[0] != "args" {
243
- t.Fatalf("expected true subcmd true [args] got %v %v %v %v", *before, cmd, *after, args)
244
- }
245
-}
246
-
247
-// Test that -help invokes the usage message and returns ErrHelp.
248
-func TestHelp(t *testing.T) {
249
- var helpCalled = false
250
- fs := NewFlagSet("help test", ContinueOnError)
251
- fs.Usage = func() { helpCalled = true }
252
- var flag bool
253
- fs.BoolVar(&flag, "flag", false, "regular flag")
254
- // Regular flag invocation should work
255
- err := fs.Parse([]string{"-flag=true"})
256
- if err != nil {
257
- t.Fatal("expected no error; got ", err)
258
- }
259
- if !flag {
260
- t.Error("flag was not set by -flag")
261
- }
262
- if helpCalled {
263
- t.Error("help called for regular flag")
264
- helpCalled = false // reset for next test
265
- }
266
- // Help flag should work as expected.
267
- err = fs.Parse([]string{"-help"})
268
- if err == nil {
269
- t.Fatal("error expected")
270
- }
271
- if err != ErrHelp {
272
- t.Fatal("expected ErrHelp; got ", err)
273
- }
274
- if !helpCalled {
275
- t.Fatal("help was not called")
276
- }
277
- // If we define a help flag, that should override.
278
- var help bool
279
- fs.BoolVar(&help, "help", false, "help flag")
280
- helpCalled = false
281
- err = fs.Parse([]string{"-help"})
282
- if err != nil {
283
- t.Fatal("expected no error for defined -help; got ", err)
284
- }
285
- if helpCalled {
286
- t.Fatal("help was called; should not have been for defined help flag")
287
- }
288
-}
Godeps/_workspace/src/github.com/jbenet/commander/AUTHORS
deleted
-11
@@ -1,11 +0,0 @@
1
-# This is the official list of Go-Commander authors for copyright purposes.
2
-# This file is distinct from the CONTRIBUTORS files.
3
-# See the latter for an explanation.
4
-
5
-# Names should be added to this file as
6
-# Name or Organization <email address>
7
-# The email address is not required for organizations.
8
-
9
-# Please keep the list sorted.
10
-
11
-Google Inc
Godeps/_workspace/src/github.com/jbenet/commander/CONTRIBUTORS
deleted
-31
@@ -1,31 +0,0 @@
1
-# This is the official list of people who can contribute
2
-# (and typically have contributed) code to the Go-Commander repository.
3
-# The AUTHORS file lists the copyright holders; this file
4
-# lists people. For example, Google employees are listed here
5
-# but not in AUTHORS, because Google holds the copyright.
6
-#
7
-# The submission process automatically checks to make sure
8
-# that people submitting code are listed in this file (by email address).
9
-#
10
-# Names should be added to this file only after verifying that
11
-# the individual or the individual's organization has agreed to
12
-# the appropriate Contributor License Agreement, found here:
13
-#
14
-# http://code.google.com/legal/individual-cla-v1.0.html
15
-# http://code.google.com/legal/corporate-cla-v1.0.html
16
-#
17
-# The agreement for individuals can be filled out on the web.
18
-#
19
-# When adding J Random Contributor's name to this file,
20
-# either J's name or J's organization's name should be
21
-# added to the AUTHORS file, depending on whether the
22
-# individual or corporate CLA was used.
23
-
24
-# Names should be added to this file like so:
25
-# Name <email address>
26
-
27
-# Please keep the list sorted.
28
-
29
-Juan Batiz-Benet <juan@benet.ai>
30
-Sebastien Binet <seb.binet@gmail.com>
31
-Yves Junqueira <yves.junqueira@gmail.com>
Godeps/_workspace/src/github.com/jbenet/commander/LICENSE
deleted
-27
@@ -1,27 +0,0 @@
1
-Copyright (c) 2012 The Go-Commander Authors. All rights reserved.
2
-
3
-Redistribution and use in source and binary forms, with or without
4
-modification, are permitted provided that the following conditions are
5
-met:
6
-
7
- * Redistributions of source code must retain the above copyright
8
-notice, this list of conditions and the following disclaimer.
9
- * Redistributions in binary form must reproduce the above
10
-copyright notice, this list of conditions and the following disclaimer
11
-in the documentation and/or other materials provided with the
12
-distribution.
13
- * Neither the name of Google Inc. nor the names of its
14
-contributors may be used to endorse or promote products derived from
15
-this software without specific prior written permission.
16
-
17
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
-A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
-OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
-SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
-LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Godeps/_workspace/src/github.com/jbenet/commander/README.md
deleted
-107
@@ -1,107 +0,0 @@
1
-commander
2
-============
3
-
4
-[](https://drone.io/github.com/gonuts/commander/latest)
5
-
6
-``commander`` is a spin off of [golang](http://golang.org) ``go tool`` infrastructure to provide commands and sub-commands.
7
-
8
-A ``commander.Command`` has a ``Subcommands`` field holding ``[]*commander.Command`` subcommands, referenced by name from the command line.
9
-
10
-So a ``Command`` can have sub commands.
11
-
12
-So you can have, _e.g._:
13
-```sh
14
-$ mycmd action1 [options...]
15
-$ mycmd subcmd1 action1 [options...]
16
-```
17
-
18
-Example provided by:
19
-- [hwaf](https://github.com/hwaf/hwaf)
20
-- [examples/my-cmd](examples/my-cmd)
21
-
22
-## Documentation
23
-Is available on [godoc](http://godoc.org/github.com/gonuts/commander)
24
-
25
-## Installation
26
-Is performed with the usual:
27
-```sh
28
-$ go get github.com/gonuts/commander
29
-```
30
-
31
-## Example
32
-
33
-See the simple ``my-cmd`` example command for how this all hangs
34
-together [there](http://github.com/gonuts/commander/blob/master/examples/my-cmd/main.go):
35
-
36
-```sh
37
-$ my-cmd cmd1
38
-my-cmd-cmd1: hello from cmd1 (quiet=true)
39
-
40
-$ my-cmd cmd1 -q
41
-my-cmd-cmd1: hello from cmd1 (quiet=true)
42
-
43
-$ my-cmd cmd1 -q=0
44
-my-cmd-cmd1: hello from cmd1 (quiet=false)
45
-
46
-$ my-cmd cmd2
47
-my-cmd-cmd2: hello from cmd2 (quiet=true)
48
-
49
-$ my-cmd subcmd1 cmd1
50
-my-cmd-subcmd1-cmd1: hello from subcmd1-cmd1 (quiet=true)
51
-
52
-$ my-cmd subcmd1 cmd2
53
-my-cmd-subcmd1-cmd2: hello from subcmd1-cmd2 (quiet=true)
54
-
55
-$ my-cmd subcmd2 cmd1
56
-my-cmd-subcmd2-cmd1: hello from subcmd2-cmd1 (quiet=true)
57
-
58
-$ my-cmd subcmd2 cmd2
59
-my-cmd-subcmd2-cmd2: hello from subcmd2-cmd2 (quiet=true)
60
-
61
-$ my-cmd help
62
-Usage:
63
-
64
- my-cmd command [arguments]
65
-
66
-The commands are:
67
-
68
- cmd1 runs cmd1 and exits
69
- cmd2 runs cmd2 and exits
70
- subcmd1 subcmd1 subcommand. does subcmd1 thingies
71
- subcmd2 subcmd2 subcommand. does subcmd2 thingies
72
-
73
-Use "my-cmd help [command]" for more information about a command.
74
-
75
-Additional help topics:
76
-
77
-
78
-Use "my-cmd help [topic]" for more information about that topic.
79
-
80
-
81
-$ my-cmd help subcmd1
82
-Usage:
83
-
84
- subcmd1 command [arguments]
85
-
86
-The commands are:
87
-
88
- cmd1 runs cmd1 and exits
89
- cmd2 runs cmd2 and exits
90
-
91
-
92
-Use "subcmd1 help [command]" for more information about a command.
93
-
94
-Additional help topics:
95
-
96
-
97
-Use "subcmd1 help [topic]" for more information about that topic.
98
-
99
-```
100
-
101
-
102
-## TODO
103
-
104
-- automatically generate the bash/zsh/csh autocompletion lists
105
-- automatically generate Readme examples text
106
-- test cases
107
-
Godeps/_workspace/src/github.com/jbenet/commander/commands.go
deleted
-358
@@ -1,358 +0,0 @@
1
-// Copyright 2012 The Go-Commander Authors. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-//
5
-// Based on the original work by The Go Authors:
6
-// Copyright 2011 The Go Authors. All rights reserved.
7
-
8
-// commander helps creating command line programs whose arguments are flags,
9
-// commands and subcommands.
10
-package commander
11
-
12
-import (
13
- "bytes"
14
- "fmt"
15
- "io"
16
- "os"
17
- "os/exec"
18
- "sort"
19
- "strings"
20
- "text/template"
21
-
22
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
23
-)
24
-
25
-// UsageSection differentiates between sections in the usage text.
26
-type Listing int
27
-
28
-const (
29
- CommandsList = iota
30
- HelpTopicsList
31
- Unlisted
32
-)
33
-
34
-// A Command is an implementation of a subcommand.
35
-type Command struct {
36
-
37
- // UsageLine is the short usage message.
38
- // The first word in the line is taken to be the command name.
39
- UsageLine string
40
-
41
- // Short is the short description line shown in command lists.
42
- Short string
43
-
44
- // Long is the long description shown in the 'help <this-command>' output.
45
- Long string
46
-
47
- // List reports which list to show this command in Usage and Help.
48
- // Choose between {CommandsList (default), HelpTopicsList, Unlisted}
49
- List Listing
50
-
51
- // Run runs the command.
52
- // The args are the arguments after the command name.
53
- Run func(cmd *Command, args []string) error
54
-
55
- // Flag is a set of flags specific to this command.
56
- Flag flag.FlagSet
57
-
58
- // CustomFlags indicates that the command will do its own
59
- // flag parsing.
60
- CustomFlags bool
61
-
62
- // Subcommands are dispatched from this command
63
- Subcommands []*Command
64
-
65
- // Parent command, nil for root.
66
- Parent *Command
67
-
68
- // UsageTemplate formats the usage (short) information displayed to the user
69
- // (leave empty for default)
70
- UsageTemplate string
71
-
72
- // HelpTemplate formats the help (long) information displayed to the user
73
- // (leave empty for default)
74
- HelpTemplate string
75
-
76
- // Stdout and Stderr by default are os.Stdout and os.Stderr, but you can
77
- // point them at any io.Writer
78
- Stdout io.Writer
79
- Stderr io.Writer
80
-}
81
-
82
-// Name returns the command's name: the first word in the usage line.
83
-func (c *Command) Name() string {
84
- name := c.UsageLine
85
- i := strings.Index(name, " ")
86
- if i >= 0 {
87
- name = name[:i]
88
- }
89
- return name
90
-}
91
-
92
-// Usage prints the usage details to the standard error output.
93
-func (c *Command) Usage() {
94
- c.usage()
95
-}
96
-
97
-// FlagOptions returns the flag's options as a string
98
-func (c *Command) FlagOptions() string {
99
- var buf bytes.Buffer
100
- c.Flag.SetOutput(&buf)
101
- c.Flag.PrintDefaults()
102
-
103
- str := string(buf.Bytes())
104
- if len(str) > 0 {
105
- return fmt.Sprintf("\nOptions:\n%s", str)
106
- }
107
- return ""
108
-}
109
-
110
-// Runnable reports whether the command can be run; otherwise
111
-// it is a documentation pseudo-command such as importpath.
112
-func (c *Command) Runnable() bool {
113
- return c.Run != nil
114
-}
115
-
116
-// Type to allow us to use sort.Sort on a slice of Commands
117
-type CommandSlice []*Command
118
-
119
-func (c CommandSlice) Len() int {
120
- return len(c)
121
-}
122
-
123
-func (c CommandSlice) Less(i, j int) bool {
124
- return c[i].Name() < c[j].Name()
125
-}
126
-
127
-func (c CommandSlice) Swap(i, j int) {
128
- c[i], c[j] = c[j], c[i]
129
-}
130
-
131
-// Sort the commands
132
-func (c *Command) SortCommands() {
133
- sort.Sort(CommandSlice(c.Subcommands))
134
-}
135
-
136
-// Init the command
137
-func (c *Command) init() {
138
- if c.Parent != nil {
139
- return // already initialized.
140
- }
141
-
142
- // setup strings
143
- if len(c.UsageLine) < 1 {
144
- c.UsageLine = Defaults.UsageLine
145
- }
146
- if len(c.UsageTemplate) < 1 {
147
- c.UsageTemplate = Defaults.UsageTemplate
148
- }
149
- if len(c.HelpTemplate) < 1 {
150
- c.HelpTemplate = Defaults.HelpTemplate
151
- }
152
-
153
- if c.Stderr == nil {
154
- c.Stderr = os.Stderr
155
- }
156
- if c.Stdout == nil {
157
- c.Stdout = os.Stdout
158
- }
159
-
160
- // init subcommands
161
- for _, cmd := range c.Subcommands {
162
- cmd.init()
163
- }
164
-
165
- // init hierarchy...
166
- for _, cmd := range c.Subcommands {
167
- cmd.Parent = c
168
- }
169
-}
170
-
171
-// Dispatch executes the command using the provided arguments.
172
-// If a subcommand exists matching the first argument, it is dispatched.
173
-// Otherwise, the command's Run function is called.
174
-func (c *Command) Dispatch(args []string) error {
175
- if c == nil {
176
- return fmt.Errorf("Called Run() on a nil Command")
177
- }
178
-
179
- // Ensure command is initialized.
180
- c.init()
181
-
182
- // First, try a sub-command
183
- if len(args) > 0 {
184
- for _, cmd := range c.Subcommands {
185
- n := cmd.Name()
186
- if n == args[0] {
187
- return cmd.Dispatch(args[1:])
188
- }
189
- }
190
-
191
- // help is builtin (but after, to allow overriding)
192
- if args[0] == "help" {
193
- return c.help(args[1:])
194
- }
195
-
196
- // then, try out an external binary (git-style)
197
- bin, err := exec.LookPath(c.FullName() + "-" + args[0])
198
- if err == nil {
199
- cmd := exec.Command(bin, args[1:]...)
200
- cmd.Stdin = os.Stdin
201
- cmd.Stdout = c.Stdout
202
- cmd.Stderr = c.Stderr
203
- return cmd.Run()
204
- }
205
- }
206
-
207
- // then, try running this command
208
- if c.Runnable() {
209
- if !c.CustomFlags {
210
- var err = error(nil)
211
- c.Flag.Usage = func() {
212
- c.Usage()
213
- err = fmt.Errorf("Failed to parse flags.")
214
- }
215
- c.Flag.Parse(args)
216
- if err != nil {
217
- return err
218
- }
219
- args = c.Flag.Args()
220
- }
221
- return c.Run(c, args)
222
- }
223
-
224
- // TODO: try an alias
225
- //...
226
-
227
- // Last, print usage
228
- if err := c.usage(); err != nil {
229
- return err
230
- }
231
- return nil
232
-}
233
-
234
-func (c *Command) usage() error {
235
- // c.SortCommands()
236
- err := tmpl(c.Stderr, c.UsageTemplate, c)
237
- if err != nil {
238
- fmt.Println(err)
239
- }
240
- return err
241
-}
242
-
243
-// help implements the 'help' command.
244
-func (c *Command) help(args []string) error {
245
-
246
- // help exactly for this command?
247
- if len(args) == 0 {
248
- if len(c.Long) > 0 {
249
- return tmpl(c.Stdout, c.HelpTemplate, c)
250
- } else {
251
- return c.usage()
252
- }
253
- }
254
-
255
- arg := args[0]
256
-
257
- // is this help for a subcommand?
258
- for _, cmd := range c.Subcommands {
259
- n := cmd.Name()
260
- // strip out "<parent>-"" name
261
- if strings.HasPrefix(n, c.Name()+"-") {
262
- n = n[len(c.Name()+"-"):]
263
- }
264
- if n == arg {
265
- return cmd.help(args[1:])
266
- }
267
- }
268
-
269
- return fmt.Errorf("Unknown help topic %#q. Run '%v help'.\n", arg, c.Name())
270
-}
271
-
272
-func (c *Command) MaxLen() (res int) {
273
- res = 0
274
- for _, cmd := range c.Subcommands {
275
- i := len(cmd.Name())
276
- if i > res {
277
- res = i
278
- }
279
- }
280
- return
281
-}
282
-
283
-// ColFormat returns the column header size format for printing in the template
284
-func (c *Command) ColFormat() string {
285
- sz := c.MaxLen()
286
- if sz < 11 {
287
- sz = 11
288
- }
289
- return fmt.Sprintf("%%-%ds", sz)
290
-}
291
-
292
-// FullName returns the full name of the command, prefixed with parent commands
293
-func (c *Command) FullName() string {
294
- n := c.Name()
295
- if c.Parent != nil {
296
- n = c.Parent.FullName() + "-" + n
297
- }
298
- return n
299
-}
300
-
301
-// FullSpacedName returns the full name of the command, with ' ' instead of '-'
302
-func (c *Command) FullSpacedName() string {
303
- n := c.Name()
304
- if c.Parent != nil {
305
- n = c.Parent.FullSpacedName() + " " + n
306
- }
307
- return n
308
-}
309
-
310
-func (c *Command) SubcommandList(list Listing) []*Command {
311
- var cmds []*Command
312
- for _, cmd := range c.Subcommands {
313
- if cmd.List == list {
314
- cmds = append(cmds, cmd)
315
- }
316
- }
317
- return cmds
318
-}
319
-
320
-var Defaults = Command{
321
- UsageTemplate: `{{if .Runnable}}Usage: {{if .Parent}}{{.Parent.FullSpacedName}}{{end}} {{.UsageLine}}
322
-
323
-{{else}}{{.FullSpacedName}} - {{end}}{{.Short}}
324
-
325
-{{if commandList}}Commands:
326
-{{range commandList}}
327
- {{.Name | printf (colfmt)}} {{.Short}}{{end}}
328
-
329
-Use "{{.Name}} help <command>" for more information about a command.
330
-
331
-{{end}}{{.FlagOptions}}{{if helpList}}
332
-Additional help topics:
333
-{{range helpList}}
334
- {{.Name | printf (colfmt)}} {{.Short}}{{end}}
335
-
336
-Use "{{.Name}} help <topic>" for more information about that topic.
337
-
338
-{{end}}`,
339
-
340
- HelpTemplate: `{{if .Runnable}}Usage: {{if .Parent}}{{.Parent.FullSpacedName}}{{end}} {{.UsageLine}}
341
-
342
-{{end}}{{.Long | trim}}
343
-{{.FlagOptions}}
344
-`,
345
-}
346
-
347
-// tmpl executes the given template text on data, writing the result to w.
348
-func tmpl(w io.Writer, text string, data interface{}) error {
349
- t := template.New("top")
350
- t.Funcs(template.FuncMap{
351
- "trim": strings.TrimSpace,
352
- "colfmt": func() string { return data.(*Command).ColFormat() },
353
- "commandList": func() []*Command { return data.(*Command).SubcommandList(CommandsList) },
354
- "helpList": func() []*Command { return data.(*Command).SubcommandList(HelpTopicsList) },
355
- })
356
- template.Must(t.Parse(text))
357
- return t.Execute(w, data)
358
-}
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/cmd_cmd1.go
deleted
-34
@@ -1,34 +0,0 @@
1
-package main
2
-
3
-import (
4
- "fmt"
5
-
6
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
7
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8
-)
9
-
10
-var cmd_cmd1 = &commander.Command{
11
- Run: ex_run_cmd_cmd1,
12
- UsageLine: "cmd1 [options]",
13
- Short: "runs cmd1 and exits",
14
- Long: `
15
-runs cmd1 and exits.
16
-
17
-ex:
18
-$ my-cmd cmd1
19
-`,
20
- Flag: *flag.NewFlagSet("my-cmd-cmd1", flag.ExitOnError),
21
-}
22
-
23
-func init() {
24
- cmd_cmd1.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
25
-}
26
-
27
-func ex_run_cmd_cmd1(cmd *commander.Command, args []string) error {
28
- name := "my-cmd-" + cmd.Name()
29
- quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
30
- fmt.Printf("%s: hello from cmd1 (quiet=%v)\n", name, quiet)
31
- return nil
32
-}
33
-
34
-// EOF
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/cmd_cmd2.go
deleted
-34
@@ -1,34 +0,0 @@
1
-package main
2
-
3
-import (
4
- "fmt"
5
-
6
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
7
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8
-)
9
-
10
-func ex_make_cmd_cmd2() *commander.Command {
11
- cmd := &commander.Command{
12
- Run: ex_run_cmd_cmd2,
13
- UsageLine: "cmd2 [options]",
14
- Short: "runs cmd2 and exits",
15
- Long: `
16
-runs cmd2 and exits.
17
-
18
-ex:
19
- $ my-cmd cmd2
20
-`,
21
- Flag: *flag.NewFlagSet("my-cmd-cmd2", flag.ExitOnError),
22
- }
23
- cmd.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
24
- return cmd
25
-}
26
-
27
-func ex_run_cmd_cmd2(cmd *commander.Command, args []string) error {
28
- name := "my-cmd-" + cmd.Name()
29
- quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
30
- fmt.Printf("%s: hello from cmd2 (quiet=%v)\n", name, quiet)
31
- return nil
32
-}
33
-
34
-// EOF
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/cmd_subcmd1.go
deleted
-18
@@ -1,18 +0,0 @@
1
-package main
2
-
3
-import (
4
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
5
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
6
-)
7
-
8
-var cmd_subcmd1 = &commander.Command{
9
- UsageLine: "subcmd1 <command>",
10
- Short: "subcmd1 subcommand. does subcmd1 thingies",
11
- Subcommands: []*commander.Command{
12
- cmd_subcmd1_cmd1,
13
- cmd_subcmd1_cmd2,
14
- },
15
- Flag: *flag.NewFlagSet("my-cmd-subcmd1", flag.ExitOnError),
16
-}
17
-
18
-// EOF
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/cmd_subcmd1_cmd1.go
deleted
-34
@@ -1,34 +0,0 @@
1
-package main
2
-
3
-import (
4
- "fmt"
5
-
6
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
7
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8
-)
9
-
10
-var cmd_subcmd1_cmd1 = &commander.Command{
11
- Run: ex_run_cmd_subcmd1_cmd1,
12
- UsageLine: "cmd1 [options]",
13
- Short: "runs cmd1 and exits",
14
- Long: `
15
-runs cmd1 and exits.
16
-
17
-ex:
18
-$ my-cmd subcmd1 cmd1
19
-`,
20
- Flag: *flag.NewFlagSet("my-cmd-subcmd1-cmd1", flag.ExitOnError),
21
-}
22
-
23
-func init() {
24
- cmd_subcmd1_cmd1.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
25
-}
26
-
27
-func ex_run_cmd_subcmd1_cmd1(cmd *commander.Command, args []string) error {
28
- name := "my-cmd-subcmd1-" + cmd.Name()
29
- quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
30
- fmt.Printf("%s: hello from subcmd1-cmd1 (quiet=%v)\n", name, quiet)
31
- return nil
32
-}
33
-
34
-// EOF
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/cmd_subcmd1_cmd2.go
deleted
-34
@@ -1,34 +0,0 @@
1
-package main
2
-
3
-import (
4
- "fmt"
5
-
6
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
7
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8
-)
9
-
10
-var cmd_subcmd1_cmd2 = &commander.Command{
11
- Run: ex_run_cmd_subcmd1_cmd2,
12
- UsageLine: "cmd2 [options]",
13
- Short: "runs cmd2 and exits",
14
- Long: `
15
-runs cmd2 and exits.
16
-
17
-ex:
18
-$ my-cmd subcmd1 cmd2
19
-`,
20
- Flag: *flag.NewFlagSet("my-cmd-subcmd1-cmd2", flag.ExitOnError),
21
-}
22
-
23
-func init() {
24
- cmd_subcmd1_cmd2.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
25
-}
26
-
27
-func ex_run_cmd_subcmd1_cmd2(cmd *commander.Command, args []string) error {
28
- name := "my-cmd-subcmd1-" + cmd.Name()
29
- quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
30
- fmt.Printf("%s: hello from subcmd1-cmd2 (quiet=%v)\n", name, quiet)
31
- return nil
32
-}
33
-
34
-// EOF
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/cmd_subcmd2.go
deleted
-22
@@ -1,22 +0,0 @@
1
-package main
2
-
3
-import (
4
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
5
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
6
-)
7
-
8
-func ex_make_cmd_subcmd2() *commander.Command {
9
- cmd := &commander.Command{
10
- UsageLine: "subcmd2",
11
- Short: "subcmd2 subcommand. does subcmd2 thingies (help list)",
12
- List: commander.HelpTopicsList,
13
- Subcommands: []*commander.Command{
14
- ex_make_cmd_subcmd2_cmd1(),
15
- ex_make_cmd_subcmd2_cmd2(),
16
- },
17
- Flag: *flag.NewFlagSet("my-cmd-subcmd2", flag.ExitOnError),
18
- }
19
- return cmd
20
-}
21
-
22
-// EOF
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/cmd_subcmd2_cmd1.go
deleted
-34
@@ -1,34 +0,0 @@
1
-package main
2
-
3
-import (
4
- "fmt"
5
-
6
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
7
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8
-)
9
-
10
-func ex_make_cmd_subcmd2_cmd1() *commander.Command {
11
- cmd := &commander.Command{
12
- Run: ex_run_cmd_subcmd2_cmd1,
13
- UsageLine: "cmd1 [options]",
14
- Short: "runs cmd1 and exits",
15
- Long: `
16
-runs cmd1 and exits.
17
-
18
-ex:
19
- $ my-cmd subcmd2 cmd1
20
-`,
21
- Flag: *flag.NewFlagSet("my-cmd-subcmd2-cmd1", flag.ExitOnError),
22
- }
23
- cmd.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
24
- return cmd
25
-}
26
-
27
-func ex_run_cmd_subcmd2_cmd1(cmd *commander.Command, args []string) error {
28
- name := "my-cmd-subcmd2-" + cmd.Name()
29
- quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
30
- fmt.Printf("%s: hello from subcmd2-cmd1 (quiet=%v)\n", name, quiet)
31
- return nil
32
-}
33
-
34
-// EOF
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/cmd_subcmd2_cmd2.go
deleted
-34
@@ -1,34 +0,0 @@
1
-package main
2
-
3
-import (
4
- "fmt"
5
-
6
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
7
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8
-)
9
-
10
-func ex_make_cmd_subcmd2_cmd2() *commander.Command {
11
- cmd := &commander.Command{
12
- Run: ex_run_cmd_subcmd2_cmd2,
13
- UsageLine: "cmd2 [options]",
14
- Short: "runs cmd2 and exits",
15
- Long: `
16
-runs cmd2 and exits.
17
-
18
-ex:
19
- $ my-cmd subcmd2 cmd2
20
-`,
21
- Flag: *flag.NewFlagSet("my-cmd-subcmd2-cmd2", flag.ExitOnError),
22
- }
23
- cmd.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
24
- return cmd
25
-}
26
-
27
-func ex_run_cmd_subcmd2_cmd2(cmd *commander.Command, args []string) error {
28
- name := "my-cmd-subcmd2-" + cmd.Name()
29
- quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
30
- fmt.Printf("%s: hello from subcmd2-cmd2 (quiet=%v)\n", name, quiet)
31
- return nil
32
-}
33
-
34
-// EOF
Godeps/_workspace/src/github.com/jbenet/commander/examples/my-cmd/main.go
deleted
-33
@@ -1,33 +0,0 @@
1
-package main
2
-
3
-import (
4
- "fmt"
5
- "os"
6
-
7
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8
-)
9
-
10
-var g_cmd = &commander.Command{
11
- UsageLine: os.Args[0] + " does cool things",
12
-}
13
-
14
-func init() {
15
- g_cmd.Subcommands = []*commander.Command{
16
- cmd_cmd1,
17
- ex_make_cmd_cmd2(),
18
- cmd_subcmd1,
19
- ex_make_cmd_subcmd2(),
20
- }
21
-}
22
-
23
-func main() {
24
- err := g_cmd.Dispatch(os.Args[1:])
25
- if err != nil {
26
- fmt.Printf("%v\n", err)
27
- os.Exit(1)
28
- }
29
-
30
- return
31
-}
32
-
33
-// EOF
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/Godeps/Godeps.json
new
+18
@@ -0,0 +1,18 @@
1
+{
2
+ "ImportPath": "github.com/jbenet/go-multiaddr-net",
3
+ "GoVersion": "go1.3",
4
+ "Packages": [
5
+ "./..."
6
+ ],
7
+ "Deps": [
8
+ {
9
+ "ImportPath": "github.com/h2so5/utp",
10
+ "Rev": "654d875bb65e96729678180215cf080fe2810371"
11
+ },
12
+ {
13
+ "ImportPath": "github.com/jbenet/go-multiaddr",
14
+ "Comment": "0.1.2-30-g99cf3ed",
15
+ "Rev": "99cf3edc711751cf7b43505fac0e3913f6b9a75c"
16
+ }
17
+ ]
18
+}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/Godeps/Readme
new
+5
@@ -0,0 +1,5 @@
1
+This directory tree is generated automatically by godep.
2
+
3
+Please do not edit.
4
+
5
+See https://github.com/tools/godep for more information.
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/LICENSE
new
+21
@@ -0,0 +1,21 @@
1
+The MIT License (MIT)
2
+
3
+Copyright (c) 2014 Juan Batiz-Benet
4
+
5
+Permission is hereby granted, free of charge, to any person obtaining a copy
6
+of this software and associated documentation files (the "Software"), to deal
7
+in the Software without restriction, including without limitation the rights
8
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+copies of the Software, and to permit persons to whom the Software is
10
+furnished to do so, subject to the following conditions:
11
+
12
+The above copyright notice and this permission notice shall be included in
13
+all copies or substantial portions of the Software.
14
+
15
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+THE SOFTWARE.
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/Makefile
new
+16
@@ -0,0 +1,16 @@
1
+all: install
2
+
3
+godep:
4
+ go get github.com/tools/godep
5
+
6
+# saves/vendors third-party dependencies to Godeps/_workspace
7
+# -r flag rewrites import paths to use the vendored path
8
+# ./... performs operation on all packages in tree
9
+vendor: godep
10
+ godep save -r ./...
11
+
12
+install: dep
13
+ cd multiaddr && go install
14
+
15
+dep:
16
+ cd multiaddr && go get ./...
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/README.md
renamed
+2
-2
@@ -1,11 +1,11 @@
1
# multiaddr/net - Multiaddr friendly net
2
3
-Package multiaddr/net provides Multiaddr specific versions of common
3
+Package multiaddr/net provides [Multiaddr](http://github.com/jbenet/go-multiaddr) specific versions of common
4
functions in stdlib's net package. This means wrappers of
5
standard net symbols like net.Dial and net.Listen, as well
6
as conversion to/from net.Addr.
7
8
Docs:
9
10
-- `multiaddr/net`: https://godoc.org/github.com/jbenet/go-multiaddr/net
10
+- `multiaddr/net`: https://godoc.org/github.com/jbenet/go-multiaddr-net
11
- `multiaddr`: https://godoc.org/github.com/jbenet/go-multiaddr
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/convert.go
renamed
+33
@@ -5,6 +5,7 @@ import (
5
"net"
6
"strings"
7
8
+ utp "github.com/h2so5/utp"
9
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10
)
11
@@ -55,6 +56,33 @@ func FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
56
// Encapsulate
57
return ipm.Encapsulate(udpm), nil
58
59
+ case "utp", "utp4", "utp6":
60
+ acc, ok := a.(*utp.UTPAddr)
61
+ if !ok {
62
+ return nil, errIncorrectNetAddr
63
+ }
64
+
65
+ // Get UDP Addr
66
+ ac, ok := acc.Addr.(*net.UDPAddr)
67
+ if !ok {
68
+ return nil, errIncorrectNetAddr
69
+ }
70
+
71
+ // Get IP Addr
72
+ ipm, err := FromIP(ac.IP)
73
+ if err != nil {
74
+ return nil, errIncorrectNetAddr
75
+ }
76
+
77
+ // Get UDP Addr
78
+ utpm, err := ma.NewMultiaddr(fmt.Sprintf("/udp/%d/utp", ac.Port))
79
+ if err != nil {
80
+ return nil, errIncorrectNetAddr
81
+ }
82
+
83
+ // Encapsulate
84
+ return ipm.Encapsulate(utpm), nil
85
+
86
case "ip", "ip4", "ip6":
87
ac, ok := a.(*net.IPAddr)
88
if !ok {
@@ -88,6 +116,8 @@ func ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
116
return net.ResolveTCPAddr(network, host)
117
case "udp":
118
return net.ResolveUDPAddr(network, host)
119
+ case "utp":
120
+ return utp.ResolveUTPAddr(network, host)
121
case "ip":
122
return net.ResolveIPAddr(network, host)
123
}
@@ -121,6 +151,9 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
151
}
152
153
network := parts[2]
154
+ if parts[2] == "udp" && len(parts) > 4 && parts[4] == "utp" {
155
+ network = parts[4]
156
+ }
157
var host string
158
switch parts[0] {
159
case "ip4":
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/convert_test.go
renamed
+34
-13
@@ -4,6 +4,7 @@ import (
4
"net"
5
"testing"
6
7
+ utp "github.com/h2so5/utp"
8
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9
)
10
@@ -88,17 +89,31 @@ func TestFromUDP(t *testing.T) {
89
})
90
}
91
92
+func TestFromUTP(t *testing.T) {
93
+ testConvert(t, "/ip4/10.20.30.40/udp/1234/utp", func() (ma.Multiaddr, error) {
94
+ return FromNetAddr(&utp.UTPAddr{
95
+ Addr: &net.UDPAddr{
96
+ IP: net.ParseIP("10.20.30.40"),
97
+ Port: 1234,
98
+ },
99
+ })
100
+ })
101
+}
102
+
103
func TestThinWaist(t *testing.T) {
104
addrs := map[string]bool{
105
"/ip4/127.0.0.1/udp/1234": true,
106
"/ip4/127.0.0.1/tcp/1234": true,
107
+ "/ip4/127.0.0.1/udp/1234/utp": true,
108
"/ip4/127.0.0.1/udp/1234/tcp/1234": true,
109
"/ip4/127.0.0.1/tcp/12345/ip4/1.2.3.4": true,
110
"/ip6/::1/tcp/80": true,
111
"/ip6/::1/udp/80": true,
112
"/ip6/::1": true,
113
+ "/ip6/::1/utp": false,
114
"/tcp/1234/ip4/1.2.3.4": false,
115
"/tcp/1234": false,
116
+ "/tcp/1234/utp": false,
117
"/tcp/1234/udp/1234": false,
118
"/ip4/1.2.3.4/ip4/2.3.4.5": true,
119
"/ip6/::1/ip4/2.3.4.5": true,
@@ -117,21 +132,27 @@ func TestThinWaist(t *testing.T) {
132
}
133
134
func TestDialArgs(t *testing.T) {
120
- m, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
121
- if err != nil {
122
- t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234")
123
- }
135
+ test := func(e_maddr, e_nw, e_host string) {
136
+ m, err := ma.NewMultiaddr(e_maddr)
137
+ if err != nil {
138
+ t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234", e_maddr)
139
+ }
140
125
- nw, host, err := DialArgs(m)
126
- if err != nil {
127
- t.Fatal("failed to get dial args", "/ip4/127.0.0.1/udp/1234", err)
128
- }
141
+ nw, host, err := DialArgs(m)
142
+ if err != nil {
143
+ t.Fatal("failed to get dial args", e_maddr, m, err)
144
+ }
145
130
- if nw != "udp" {
131
- t.Error("failed to get udp network Dial Arg")
132
- }
146
+ if nw != e_nw {
147
+ t.Error("failed to get udp network Dial Arg", e_nw, nw)
148
+ }
149
134
- if host != "127.0.0.1:1234" {
135
- t.Error("failed to get host:port Dial Arg")
150
+ if host != e_host {
151
+ t.Error("failed to get host:port Dial Arg", e_host, host)
152
+ }
153
}
154
+
155
+ test("/ip4/127.0.0.1/udp/1234", "udp", "127.0.0.1:1234")
156
+ test("/ip4/127.0.0.1/tcp/4321", "tcp", "127.0.0.1:4321")
157
+ test("/ip4/127.0.0.1/udp/1234/utp", "utp", "127.0.0.1:1234")
158
}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/doc.go
renamed
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/ip.go
renamed
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/multiaddr/multiaddr
Binary files /dev/null and b/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/multiaddr/multiaddr differ
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/multiaddr/multiaddr.go
renamed
+1
-1
@@ -7,7 +7,7 @@ import (
7
"os"
8
9
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10
- manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
10
+ manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
11
)
12
13
// flags
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/net.go
renamed
+32
-7
@@ -4,6 +4,7 @@ import (
4
"fmt"
5
"net"
6
7
+ utp "github.com/h2so5/utp"
8
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9
)
10
@@ -68,7 +69,7 @@ func (c *maConn) RemoteMultiaddr() ma.Multiaddr {
69
// and RemoteAddr options are Multiaddrs, instead of net.Addrs.
70
type Dialer struct {
71
71
- // Dialer is just an embed net.Dialer, with all its options.
72
+ // Dialer is just an embedded net.Dialer, with all its options.
73
net.Dialer
74
75
// LocalAddr is the local address to use when dialing an
@@ -103,9 +104,24 @@ func (d *Dialer) Dial(remote ma.Multiaddr) (Conn, error) {
104
}
105
106
// ok, Dial!
106
- nconn, err := d.Dialer.Dial(rnet, rnaddr)
107
- if err != nil {
108
- return nil, err
107
+ var nconn net.Conn
108
+ switch rnet {
109
+ case "tcp":
110
+ nconn, err = d.Dialer.Dial(rnet, rnaddr)
111
+ if err != nil {
112
+ return nil, err
113
+ }
114
+ case "utp":
115
+ // construct utp dialer, with options on our net.Dialer
116
+ utpd := utp.Dialer{
117
+ Timeout: d.Dialer.Timeout,
118
+ LocalAddr: d.Dialer.LocalAddr,
119
+ }
120
+
121
+ nconn, err = utpd.Dial(rnet, rnaddr)
122
+ if err != nil {
123
+ return nil, err
124
+ }
125
}
126
127
// get local address (pre-specified or assigned within net.Conn)
@@ -206,9 +222,18 @@ func Listen(laddr ma.Multiaddr) (Listener, error) {
222
return nil, err
223
}
224
209
- nl, err := net.Listen(lnet, lnaddr)
210
- if err != nil {
211
- return nil, err
225
+ var nl net.Listener
226
+ switch lnet {
227
+ case "utp":
228
+ nl, err = utp.Listen(lnet, lnaddr)
229
+ if err != nil {
230
+ return nil, err
231
+ }
232
+ case "tcp":
233
+ nl, err = net.Listen(lnet, lnaddr)
234
+ if err != nil {
235
+ return nil, err
236
+ }
237
}
238
239
return &maListener{
Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net/net_test.go
renamed
+62
-1
@@ -12,7 +12,7 @@ import (
12
func newMultiaddr(t *testing.T, m string) ma.Multiaddr {
13
maddr, err := ma.NewMultiaddr(m)
14
if err != nil {
15
- t.Fatalf("failed to construct multiaddr: %s", m)
15
+ t.Fatal("failed to construct multiaddr:", m, err)
16
}
17
return maddr
18
}
@@ -199,6 +199,67 @@ func TestListenAndDial(t *testing.T) {
199
wg.Wait()
200
}
201
202
+func TestListenAndDialUTP(t *testing.T) {
203
+
204
+ maddr := newMultiaddr(t, "/ip4/127.0.0.1/udp/4323/utp")
205
+ listener, err := Listen(maddr)
206
+ if err != nil {
207
+ t.Fatal("failed to listen")
208
+ }
209
+
210
+ var wg sync.WaitGroup
211
+ wg.Add(1)
212
+ go func() {
213
+
214
+ cB, err := listener.Accept()
215
+ if err != nil {
216
+ t.Fatal("failed to accept")
217
+ }
218
+
219
+ if !cB.LocalMultiaddr().Equal(maddr) {
220
+ t.Fatal("local multiaddr not equal:", maddr, cB.LocalMultiaddr())
221
+ }
222
+
223
+ // echo out
224
+ buf := make([]byte, 1024)
225
+ for {
226
+ _, err := cB.Read(buf)
227
+ if err != nil {
228
+ break
229
+ }
230
+ cB.Write(buf)
231
+ }
232
+
233
+ wg.Done()
234
+ }()
235
+
236
+ cA, err := Dial(newMultiaddr(t, "/ip4/127.0.0.1/udp/4323/utp"))
237
+ if err != nil {
238
+ t.Fatal("failed to dial", err)
239
+ }
240
+
241
+ buf := make([]byte, 1024)
242
+ if _, err := cA.Write([]byte("beep boop")); err != nil {
243
+ t.Fatal("failed to write:", err)
244
+ }
245
+
246
+ if _, err := cA.Read(buf); err != nil {
247
+ t.Fatal("failed to read:", buf, err)
248
+ }
249
+
250
+ if !bytes.Equal(buf[:9], []byte("beep boop")) {
251
+ t.Fatal("failed to echo:", buf)
252
+ }
253
+
254
+ maddr2 := cA.RemoteMultiaddr()
255
+ if !maddr2.Equal(maddr) {
256
+ t.Fatal("remote multiaddr not equal:", maddr, maddr2)
257
+ }
258
+
259
+ cA.Close()
260
+ wg.Wait()
261
+}
262
+
263
func TestIPLoopback(t *testing.T) {
264
if IP4Loopback.String() != "/ip4/127.0.0.1" {
265
t.Error("IP4Loopback incorrect:", IP4Loopback)
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/codec.go
+21
-14
@@ -28,12 +28,14 @@ func stringToBytes(s string) ([]byte, error) {
28
if p == nil {
29
return nil, fmt.Errorf("no protocol with name %s", sp[0])
30
}
31
- b = append(b, byte(p.Code))
31
+ b = append(b, CodeToVarint(p.Code)...)
32
+ sp = sp[1:]
33
33
- a := addressStringToBytes(p, sp[1])
34
- b = append(b, a...)
35
-
36
- sp = sp[2:]
34
+ if p.Size > 0 {
35
+ a := addressStringToBytes(p, sp[0])
36
+ b = append(b, a...)
37
+ sp = sp[1:]
38
+ }
39
}
40
return b, nil
41
}
@@ -50,18 +52,22 @@ func bytesToString(b []byte) (ret string, err error) {
52
s := ""
53
54
for len(b) > 0 {
53
- p := ProtocolWithCode(int(b[0]))
55
+
56
+ code, n := ReadVarintCode(b)
57
+ b = b[n:]
58
+ p := ProtocolWithCode(code)
59
if p == nil {
55
- return "", fmt.Errorf("no protocol with code %d", b[0])
60
+ return "", fmt.Errorf("no protocol with code %d", code)
61
}
62
s = strings.Join([]string{s, "/", p.Name}, "")
58
- b = b[1:]
63
60
- a := addressBytesToString(p, b[:(p.Size/8)])
61
- if len(a) > 0 {
62
- s = strings.Join([]string{s, "/", a}, "")
64
+ if p.Size > 0 {
65
+ a := addressBytesToString(p, b[:(p.Size/8)])
66
+ if len(a) > 0 {
67
+ s = strings.Join([]string{s, "/", a}, "")
68
+ }
69
+ b = b[(p.Size / 8):]
70
}
64
- b = b[(p.Size / 8):]
71
}
72
73
return s, nil
@@ -78,12 +84,13 @@ func bytesSplit(b []byte) (ret [][]byte, err error) {
84
85
ret = [][]byte{}
86
for len(b) > 0 {
81
- p := ProtocolWithCode(int(b[0]))
87
+ code, n := ReadVarintCode(b)
88
+ p := ProtocolWithCode(code)
89
if p == nil {
90
return [][]byte{}, fmt.Errorf("no protocol with code %d", b[0])
91
}
92
86
- length := 1 + (p.Size / 8)
93
+ length := n + (p.Size / 8)
94
ret = append(ret, b[:length])
95
b = b[length:]
96
}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/multiaddr.go
+3
-2
@@ -67,14 +67,15 @@ func (m *multiaddr) Protocols() []*Protocol {
67
ps := []*Protocol{}
68
b := m.bytes[:]
69
for len(b) > 0 {
70
- p := ProtocolWithCode(int(b[0]))
70
+ code, n := ReadVarintCode(b)
71
+ p := ProtocolWithCode(code)
72
if p == nil {
73
// this is a panic (and not returning err) because this should've been
74
// caught on constructing the Multiaddr
75
panic(fmt.Errorf("no protocol with code %d", b[0]))
76
}
77
ps = append(ps, p)
77
- b = b[1+(p.Size/8):]
78
+ b = b[n+(p.Size/8):]
79
}
80
return ps
81
}
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/multiaddr/multiaddr
Binary files a/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/multiaddr/multiaddr and /dev/null differ
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/multiaddr_test.go
+7
-1
@@ -68,6 +68,8 @@ func TestStringToBytes(t *testing.T) {
68
}
69
70
testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
71
+ testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
72
+ testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1")
73
}
74
75
func TestBytesToString(t *testing.T) {
@@ -89,6 +91,8 @@ func TestBytesToString(t *testing.T) {
91
}
92
93
testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
94
+ testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
95
+ testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1")
96
}
97
98
func TestBytesSplitAndJoin(t *testing.T) {
@@ -96,7 +100,7 @@ func TestBytesSplitAndJoin(t *testing.T) {
100
testString := func(s string, res []string) {
101
m, err := NewMultiaddr(s)
102
if err != nil {
99
- t.Error("failed to convert", s)
103
+ t.Fatal("failed to convert", s, err)
104
}
105
106
split := Split(m)
@@ -132,6 +136,8 @@ func TestBytesSplitAndJoin(t *testing.T) {
136
testString("/ip4/1.2.3.4/udp/1234", []string{"/ip4/1.2.3.4", "/udp/1234"})
137
testString("/ip4/1.2.3.4/tcp/1/ip4/2.3.4.5/udp/2",
138
[]string{"/ip4/1.2.3.4", "/tcp/1", "/ip4/2.3.4.5", "/udp/2"})
139
+ testString("/ip4/1.2.3.4/utp/ip4/2.3.4.5/udp/2/udt",
140
+ []string{"/ip4/1.2.3.4", "/utp", "/ip4/2.3.4.5", "/udp/2", "/udt"})
141
}
142
143
func TestProtocols(t *testing.T) {
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/protocols.csv
+2
@@ -5,5 +5,7 @@ code size name
5
33 16 dccp
6
41 128 ip6
7
132 16 sctp
8
+301 0 udt
9
+302 0 utp
10
480 0 http
11
443 0 https
Godeps/_workspace/src/github.com/jbenet/go-multiaddr/protocols.go
+41
-9
@@ -1,10 +1,15 @@
1
package multiaddr
2
3
+import (
4
+ "encoding/binary"
5
+)
6
+
7
// Protocol is a Multiaddr protocol description structure.
8
type Protocol struct {
5
- Code int
6
- Size int
7
- Name string
9
+ Code int
10
+ Size int
11
+ Name string
12
+ VCode []byte
13
}
14
15
// replicating table here to:
@@ -18,17 +23,21 @@ const (
23
P_DCCP = 33
24
P_IP6 = 41
25
P_SCTP = 132
26
+ P_UTP = 301
27
+ P_UDT = 302
28
)
29
30
// Protocols is the list of multiaddr protocols supported by this module.
31
var Protocols = []*Protocol{
25
- &Protocol{P_IP4, 32, "ip4"},
26
- &Protocol{P_TCP, 16, "tcp"},
27
- &Protocol{P_UDP, 16, "udp"},
28
- &Protocol{P_DCCP, 16, "dccp"},
29
- &Protocol{P_IP6, 128, "ip6"},
32
+ &Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4)},
33
+ &Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP)},
34
+ &Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP)},
35
+ &Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)},
36
+ &Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)},
37
// these require varint:
31
- &Protocol{P_SCTP, 16, "sctp"},
38
+ &Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
39
+ &Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
40
+ &Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
41
// {480, 0, "http"},
42
// {443, 0, "https"},
43
}
@@ -52,3 +61,26 @@ func ProtocolWithCode(c int) *Protocol {
61
}
62
return nil
63
}
64
+
65
+// CodeToVarint converts an integer to a varint-encoded []byte
66
+func CodeToVarint(num int) []byte {
67
+ buf := make([]byte, (num/7)+1) // varint package is uint64
68
+ n := binary.PutUvarint(buf, uint64(num))
69
+ return buf[:n]
70
+}
71
+
72
+// VarintToCode converts a varint-encoded []byte to an integer protocol code
73
+func VarintToCode(buf []byte) int {
74
+ num, _ := ReadVarintCode(buf)
75
+ return num
76
+}
77
+
78
+// ReadVarintCode reads a varint code from the beginning of buf.
79
+// returns the code, and the number of bytes read.
80
+func ReadVarintCode(buf []byte) (int, int) {
81
+ num, n := binary.Uvarint(buf)
82
+ if n < 0 {
83
+ panic("varints larger than uint64 not yet supported")
84
+ }
85
+ return int(num), n
86
+}
cmd/ipfs/daemon.go
+1
-1
@@ -6,7 +6,7 @@ import (
6
7
manners "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/braintree/manners"
8
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9
- manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
9
+ manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
10
cmds "github.com/jbenet/go-ipfs/commands"
11
cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
12
core "github.com/jbenet/go-ipfs/core"
cmd/ipfs/main.go
+1
-1
@@ -12,7 +12,7 @@ import (
12
// TODO rm direct reference to go-logging
13
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
14
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
15
- manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
15
+ manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
16
17
cmds "github.com/jbenet/go-ipfs/commands"
18
cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
net/conn/conn.go
+1
-1
@@ -8,7 +8,7 @@ import (
8
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
msgio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
10
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
11
- manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
11
+ manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
12
13
peer "github.com/jbenet/go-ipfs/peer"
14
u "github.com/jbenet/go-ipfs/util"
net/conn/dial.go
+1
-1
@@ -5,7 +5,7 @@ import (
5
6
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
8
- manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
8
+ manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
9
10
peer "github.com/jbenet/go-ipfs/peer"
11
)
net/conn/listen.go
+1
-1
@@ -3,7 +3,7 @@ package conn
3
import (
4
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
6
- manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
6
+ manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
7
8
peer "github.com/jbenet/go-ipfs/peer"
9
ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
net/swarm/addrs.go
+1
-1
@@ -2,7 +2,7 @@ package swarm
2
3
import (
4
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
5
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
6
"github.com/jbenet/go-ipfs/util/eventlog"
7
8
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
server/http/http.go
+1
-1
@@ -8,7 +8,7 @@ import (
8
9
mux "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
10
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
11
- manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
11
+ manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
12
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
13
14
core "github.com/jbenet/go-ipfs/core"