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)
This commit is contained in:
Juan Batiz-Benet 2014-11-20 00:58:47 -08:00
parent e1be96133c
commit 43452fdeb7
47 changed files with 314 additions and 2123 deletions

18
Godeps/Godeps.json generated
View File

@ -1,6 +1,6 @@
{
"ImportPath": "github.com/jbenet/go-ipfs",
"GoVersion": "go1.3.3",
"GoVersion": "go1.3",
"Packages": [
"./..."
],
@ -73,10 +73,6 @@
"ImportPath": "github.com/facebookgo/stackerr",
"Rev": "060fbf9364c89acd41bf710e9e92915a90e7a5b5"
},
{
"ImportPath": "github.com/gonuts/flag",
"Rev": "741a6cbd37a30dedc93f817e7de6aaf0ca38a493"
},
{
"ImportPath": "github.com/gorilla/context",
"Rev": "14f550f51af52180c2eefed15e5fd18d63c0a64a"
@ -89,10 +85,6 @@
"ImportPath": "github.com/inconshreveable/go-update",
"Rev": "221d034a558b4c21b0624b2a450c076913854a57"
},
{
"ImportPath": "github.com/jbenet/commander",
"Rev": "e0cf317891f0ab6f1ac64dfcb754b4fb5e69f7df"
},
{
"ImportPath": "github.com/jbenet/go-base58",
"Rev": "568a28d73fd97651d3442392036a658b6976eed5"
@ -115,8 +107,12 @@
},
{
"ImportPath": "github.com/jbenet/go-multiaddr",
"Comment": "0.1.2-27-g62a88e0",
"Rev": "62a88e015e1bf5d6aaca29aec1aba0722f21c8d3"
"Comment": "0.1.2-30-g99cf3ed",
"Rev": "99cf3edc711751cf7b43505fac0e3913f6b9a75c"
},
{
"ImportPath": "github.com/jbenet/go-multiaddr-net",
"Rev": "625fac6e5073702ac9cd6028b9dc2457fd9cbf9d"
},
{
"ImportPath": "github.com/jbenet/go-multihash",

View File

@ -1,27 +0,0 @@
Copyright (c) 2012 The Go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,6 +0,0 @@
flag
=======
[![Build Status](https://drone.io/github.com/gonuts/flag/status.png)](https://drone.io/github.com/gonuts/flag/latest)
A fork of the official "flag" package but with the flag.Value interface extended to provide a ``Get() interface{}`` method.

View File

@ -1,83 +0,0 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// These examples demonstrate more intricate uses of the flag package.
package flag_test
import (
"errors"
"flag"
"fmt"
"strings"
"time"
)
// Example 1: A single string flag called "species" with default value "gopher".
var species = flag.String("species", "gopher", "the species we are studying")
// Example 2: Two flags sharing a variable, so we can have a shorthand.
// The order of initialization is undefined, so make sure both use the
// same default value. They must be set up with an init function.
var gopherType string
func init() {
const (
defaultGopher = "pocket"
usage = "the variety of gopher"
)
flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")
}
// Example 3: A user-defined flag type, a slice of durations.
type interval []time.Duration
// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (i *interval) String() string {
return fmt.Sprint(*i)
}
// Set is the method to set the flag value, part of the flag.Value interface.
// Set's argument is a string to be parsed to set the flag.
// It's a comma-separated list, so we split it.
func (i *interval) Set(value string) error {
// If we wanted to allow the flag to be set multiple times,
// accumulating values, we would delete this if statement.
// That would permit usages such as
// -deltaT 10s -deltaT 15s
// and other combinations.
if len(*i) > 0 {
return errors.New("interval flag already set")
}
for _, dt := range strings.Split(value, ",") {
duration, err := time.ParseDuration(dt)
if err != nil {
return err
}
*i = append(*i, duration)
}
return nil
}
// Define a flag to accumulate durations. Because it has a special type,
// we need to use the Var function and therefore create the flag during
// init.
var intervalFlag interval
func init() {
// Tie the command-line flag to the intervalFlag variable and
// set a usage message.
flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
}
func Example() {
// All the interesting pieces are with the variables declared above, but
// to enable the flag package to see the flags defined there, one must
// execute, typically at the start of main (not init!):
// flag.Parse()
// We don't run it here because this is not a main function and
// the testing suite has already parsed the flags.
}

View File

@ -1,22 +0,0 @@
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flag
import "os"
// Additional routines compiled into the package only during testing.
// ResetForTesting clears all flag state and sets the usage function as directed.
// After calling ResetForTesting, parse errors in flag handling will not
// exit the program.
func ResetForTesting(usage func()) {
commandLine = NewFlagSet(os.Args[0], ContinueOnError)
Usage = usage
}
// CommandLine returns the default FlagSet.
func CommandLine() *FlagSet {
return commandLine
}

View File

@ -1,816 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package flag implements command-line flag parsing.
Usage:
Define flags using flag.String(), Bool(), Int(), etc.
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
import "flag"
var ip = flag.Int("flagname", 1234, "help message for flagname")
If you like, you can bind the flag to a variable using the Var() functions.
var flagvar int
func init() {
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}
Or you can create custom flags that satisfy the Value interface (with
pointer receivers) and couple them to flag parsing by
flag.Var(&flagVal, "name", "help message for flagname")
For such flags, the default value is just the initial value of the variable.
After all flags are defined, call
flag.Parse()
to parse the command line into the defined flags.
Flags may then be used directly. If you're using the flags themselves,
they are all pointers; if you bind to variables, they're values.
fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)
After parsing, the arguments after the flag are available as the
slice flag.Args() or individually as flag.Arg(i).
The arguments are indexed from 0 up to flag.NArg().
Command line flag syntax:
-flag
-flag=x
-flag x // non-boolean flags only
One or two minus signs may be used; they are equivalent.
The last form is not permitted for boolean flags because the
meaning of the command
cmd -x *
will change if there is a file called 0, false, etc. You must
use the -flag=false form to turn off a boolean flag.
Flag parsing stops just before the first non-flag argument
("-" is a non-flag argument) or after the terminator "--".
Integer flags accept 1234, 0664, 0x1234 and may be negative.
Boolean flags may be 1, 0, t, f, true, false, TRUE, FALSE, True, False.
Duration flags accept any input valid for time.ParseDuration.
The default set of command-line flags is controlled by
top-level functions. The FlagSet type allows one to define
independent sets of flags, such as to implement subcommands
in a command-line interface. The methods of FlagSet are
analogous to the top-level functions for the command-line
flag set.
*/
package flag
import (
"errors"
"fmt"
"io"
"os"
"sort"
"strconv"
"time"
)
// ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
var ErrHelp = errors.New("flag: help requested")
// -- bool Value
type boolValue bool
func newBoolValue(val bool, p *bool) *boolValue {
*p = val
return (*boolValue)(p)
}
func (b *boolValue) Set(s string) error {
v, err := strconv.ParseBool(s)
*b = boolValue(v)
return err
}
func (b *boolValue) Get() interface{} { return bool(*b) }
func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
// -- int Value
type intValue int
func newIntValue(val int, p *int) *intValue {
*p = val
return (*intValue)(p)
}
func (i *intValue) Set(s string) error {
v, err := strconv.ParseInt(s, 0, 64)
*i = intValue(v)
return err
}
func (i *intValue) Get() interface{} { return int(*i) }
func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
// -- int64 Value
type int64Value int64
func newInt64Value(val int64, p *int64) *int64Value {
*p = val
return (*int64Value)(p)
}
func (i *int64Value) Set(s string) error {
v, err := strconv.ParseInt(s, 0, 64)
*i = int64Value(v)
return err
}
func (i *int64Value) Get() interface{} { return int64(*i) }
func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
// -- uint Value
type uintValue uint
func newUintValue(val uint, p *uint) *uintValue {
*p = val
return (*uintValue)(p)
}
func (i *uintValue) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 64)
*i = uintValue(v)
return err
}
func (i *uintValue) Get() interface{} { return uint(*i) }
func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
// -- uint64 Value
type uint64Value uint64
func newUint64Value(val uint64, p *uint64) *uint64Value {
*p = val
return (*uint64Value)(p)
}
func (i *uint64Value) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 64)
*i = uint64Value(v)
return err
}
func (i *uint64Value) Get() interface{} { return uint64(*i) }
func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
// -- string Value
type stringValue string
func newStringValue(val string, p *string) *stringValue {
*p = val
return (*stringValue)(p)
}
func (s *stringValue) Set(val string) error {
*s = stringValue(val)
return nil
}
func (s *stringValue) Get() interface{} { return s.String() }
func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
// -- float64 Value
type float64Value float64
func newFloat64Value(val float64, p *float64) *float64Value {
*p = val
return (*float64Value)(p)
}
func (f *float64Value) Set(s string) error {
v, err := strconv.ParseFloat(s, 64)
*f = float64Value(v)
return err
}
func (f *float64Value) Get() interface{} { return float64(*f) }
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
// -- time.Duration Value
type durationValue time.Duration
func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
*p = val
return (*durationValue)(p)
}
func (d *durationValue) Set(s string) error {
v, err := time.ParseDuration(s)
*d = durationValue(v)
return err
}
func (d *durationValue) Get() interface{} { return *(*time.Duration)(d) }
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
// Value is the interface to the dynamic value stored in a flag.
// (The default value is represented as a string.)
type Value interface {
String() string
Set(string) error
Get() interface{}
}
// ErrorHandling defines how to handle flag parsing errors.
type ErrorHandling int
const (
ContinueOnError ErrorHandling = iota
ExitOnError
PanicOnError
)
// A FlagSet represents a set of defined flags.
type FlagSet struct {
// Usage is the function called when an error occurs while parsing flags.
// The field is a function (not a method) that may be changed to point to
// a custom error handler.
Usage func()
name string
parsed bool
actual map[string]*Flag
formal map[string]*Flag
args []string // arguments after flags
exitOnError bool // does the program exit if there's an error?
errorHandling ErrorHandling
output io.Writer // nil means stderr; use out() accessor
}
// A Flag represents the state of a flag.
type Flag struct {
Name string // name as it appears on command line
Usage string // help message
Value Value // value as set
DefValue string // default value (as text); for usage message
}
// sortFlags returns the flags as a slice in lexicographical sorted order.
func sortFlags(flags map[string]*Flag) []*Flag {
list := make(sort.StringSlice, len(flags))
i := 0
for _, f := range flags {
list[i] = f.Name
i++
}
list.Sort()
result := make([]*Flag, len(list))
for i, name := range list {
result[i] = flags[name]
}
return result
}
func (f *FlagSet) out() io.Writer {
if f.output == nil {
return os.Stderr
}
return f.output
}
// SetOutput sets the destination for usage and error messages.
// If output is nil, os.Stderr is used.
func (f *FlagSet) SetOutput(output io.Writer) {
f.output = output
}
// VisitAll visits the flags in lexicographical order, calling fn for each.
// It visits all flags, even those not set.
func (f *FlagSet) VisitAll(fn func(*Flag)) {
for _, flag := range sortFlags(f.formal) {
fn(flag)
}
}
// VisitAll visits the command-line flags in lexicographical order, calling
// fn for each. It visits all flags, even those not set.
func VisitAll(fn func(*Flag)) {
commandLine.VisitAll(fn)
}
// Visit visits the flags in lexicographical order, calling fn for each.
// It visits only those flags that have been set.
func (f *FlagSet) Visit(fn func(*Flag)) {
for _, flag := range sortFlags(f.actual) {
fn(flag)
}
}
// Visit visits the command-line flags in lexicographical order, calling fn
// for each. It visits only those flags that have been set.
func Visit(fn func(*Flag)) {
commandLine.Visit(fn)
}
// Lookup returns the Flag structure of the named flag, returning nil if none exists.
func (f *FlagSet) Lookup(name string) *Flag {
return f.formal[name]
}
// Lookup returns the Flag structure of the named command-line flag,
// returning nil if none exists.
func Lookup(name string) *Flag {
return commandLine.formal[name]
}
// Set sets the value of the named flag.
func (f *FlagSet) Set(name, value string) error {
flag, ok := f.formal[name]
if !ok {
return fmt.Errorf("no such flag -%v", name)
}
err := flag.Value.Set(value)
if err != nil {
return err
}
if f.actual == nil {
f.actual = make(map[string]*Flag)
}
f.actual[name] = flag
return nil
}
// Set sets the value of the named command-line flag.
func Set(name, value string) error {
return commandLine.Set(name, value)
}
// PrintDefaults prints, to standard error unless configured
// otherwise, the default values of all defined flags in the set.
func (f *FlagSet) PrintDefaults() {
f.VisitAll(func(flag *Flag) {
format := " -%s=%s: %s\n"
if _, ok := flag.Value.(*stringValue); ok {
// put quotes on the value
format = " -%s=%q: %s\n"
}
fmt.Fprintf(f.out(), format, flag.Name, flag.DefValue, flag.Usage)
})
}
// PrintDefaults prints to standard error the default values of all defined command-line flags.
func PrintDefaults() {
commandLine.PrintDefaults()
}
// defaultUsage is the default function to print a usage message.
func defaultUsage(f *FlagSet) {
fmt.Fprintf(f.out(), "Usage of %s:\n", f.name)
f.PrintDefaults()
}
// NOTE: Usage is not just defaultUsage(commandLine)
// because it serves (via godoc flag Usage) as the example
// for how to write your own usage function.
// Usage prints to standard error a usage message documenting all defined command-line flags.
// The function is a variable that may be changed to point to a custom function.
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
PrintDefaults()
}
// NFlag returns the number of flags that have been set.
func (f *FlagSet) NFlag() int { return len(f.actual) }
// NFlag returns the number of command-line flags that have been set.
func NFlag() int { return len(commandLine.actual) }
// Arg returns the i'th argument. Arg(0) is the first remaining argument
// after flags have been processed.
func (f *FlagSet) Arg(i int) string {
if i < 0 || i >= len(f.args) {
return ""
}
return f.args[i]
}
// Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
// after flags have been processed.
func Arg(i int) string {
return commandLine.Arg(i)
}
// NArg is the number of arguments remaining after flags have been processed.
func (f *FlagSet) NArg() int { return len(f.args) }
// NArg is the number of arguments remaining after flags have been processed.
func NArg() int { return len(commandLine.args) }
// Args returns the non-flag arguments.
func (f *FlagSet) Args() []string { return f.args }
// Args returns the non-flag command-line arguments.
func Args() []string { return commandLine.args }
// BoolVar defines a bool flag with specified name, default value, and usage string.
// The argument p points to a bool variable in which to store the value of the flag.
func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
f.Var(newBoolValue(value, p), name, usage)
}
// BoolVar defines a bool flag with specified name, default value, and usage string.
// The argument p points to a bool variable in which to store the value of the flag.
func BoolVar(p *bool, name string, value bool, usage string) {
commandLine.Var(newBoolValue(value, p), name, usage)
}
// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
p := new(bool)
f.BoolVar(p, name, value, usage)
return p
}
// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func Bool(name string, value bool, usage string) *bool {
return commandLine.Bool(name, value, usage)
}
// IntVar defines an int flag with specified name, default value, and usage string.
// The argument p points to an int variable in which to store the value of the flag.
func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
f.Var(newIntValue(value, p), name, usage)
}
// IntVar defines an int flag with specified name, default value, and usage string.
// The argument p points to an int variable in which to store the value of the flag.
func IntVar(p *int, name string, value int, usage string) {
commandLine.Var(newIntValue(value, p), name, usage)
}
// Int defines an int flag with specified name, default value, and usage string.
// The return value is the address of an int variable that stores the value of the flag.
func (f *FlagSet) Int(name string, value int, usage string) *int {
p := new(int)
f.IntVar(p, name, value, usage)
return p
}
// Int defines an int flag with specified name, default value, and usage string.
// The return value is the address of an int variable that stores the value of the flag.
func Int(name string, value int, usage string) *int {
return commandLine.Int(name, value, usage)
}
// Int64Var defines an int64 flag with specified name, default value, and usage string.
// The argument p points to an int64 variable in which to store the value of the flag.
func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
f.Var(newInt64Value(value, p), name, usage)
}
// Int64Var defines an int64 flag with specified name, default value, and usage string.
// The argument p points to an int64 variable in which to store the value of the flag.
func Int64Var(p *int64, name string, value int64, usage string) {
commandLine.Var(newInt64Value(value, p), name, usage)
}
// Int64 defines an int64 flag with specified name, default value, and usage string.
// The return value is the address of an int64 variable that stores the value of the flag.
func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
p := new(int64)
f.Int64Var(p, name, value, usage)
return p
}
// Int64 defines an int64 flag with specified name, default value, and usage string.
// The return value is the address of an int64 variable that stores the value of the flag.
func Int64(name string, value int64, usage string) *int64 {
return commandLine.Int64(name, value, usage)
}
// UintVar defines a uint flag with specified name, default value, and usage string.
// The argument p points to a uint variable in which to store the value of the flag.
func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
f.Var(newUintValue(value, p), name, usage)
}
// UintVar defines a uint flag with specified name, default value, and usage string.
// The argument p points to a uint variable in which to store the value of the flag.
func UintVar(p *uint, name string, value uint, usage string) {
commandLine.Var(newUintValue(value, p), name, usage)
}
// Uint defines a uint flag with specified name, default value, and usage string.
// The return value is the address of a uint variable that stores the value of the flag.
func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
p := new(uint)
f.UintVar(p, name, value, usage)
return p
}
// Uint defines a uint flag with specified name, default value, and usage string.
// The return value is the address of a uint variable that stores the value of the flag.
func Uint(name string, value uint, usage string) *uint {
return commandLine.Uint(name, value, usage)
}
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
// The argument p points to a uint64 variable in which to store the value of the flag.
func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) {
f.Var(newUint64Value(value, p), name, usage)
}
// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
// The argument p points to a uint64 variable in which to store the value of the flag.
func Uint64Var(p *uint64, name string, value uint64, usage string) {
commandLine.Var(newUint64Value(value, p), name, usage)
}
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
// The return value is the address of a uint64 variable that stores the value of the flag.
func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 {
p := new(uint64)
f.Uint64Var(p, name, value, usage)
return p
}
// Uint64 defines a uint64 flag with specified name, default value, and usage string.
// The return value is the address of a uint64 variable that stores the value of the flag.
func Uint64(name string, value uint64, usage string) *uint64 {
return commandLine.Uint64(name, value, usage)
}
// StringVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string variable in which to store the value of the flag.
func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
f.Var(newStringValue(value, p), name, usage)
}
// StringVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string variable in which to store the value of the flag.
func StringVar(p *string, name string, value string, usage string) {
commandLine.Var(newStringValue(value, p), name, usage)
}
// String defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string variable that stores the value of the flag.
func (f *FlagSet) String(name string, value string, usage string) *string {
p := new(string)
f.StringVar(p, name, value, usage)
return p
}
// String defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string variable that stores the value of the flag.
func String(name string, value string, usage string) *string {
return commandLine.String(name, value, usage)
}
// Float64Var defines a float64 flag with specified name, default value, and usage string.
// The argument p points to a float64 variable in which to store the value of the flag.
func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) {
f.Var(newFloat64Value(value, p), name, usage)
}
// Float64Var defines a float64 flag with specified name, default value, and usage string.
// The argument p points to a float64 variable in which to store the value of the flag.
func Float64Var(p *float64, name string, value float64, usage string) {
commandLine.Var(newFloat64Value(value, p), name, usage)
}
// Float64 defines a float64 flag with specified name, default value, and usage string.
// The return value is the address of a float64 variable that stores the value of the flag.
func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
p := new(float64)
f.Float64Var(p, name, value, usage)
return p
}
// Float64 defines a float64 flag with specified name, default value, and usage string.
// The return value is the address of a float64 variable that stores the value of the flag.
func Float64(name string, value float64, usage string) *float64 {
return commandLine.Float64(name, value, usage)
}
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
// The argument p points to a time.Duration variable in which to store the value of the flag.
func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
f.Var(newDurationValue(value, p), name, usage)
}
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
// The argument p points to a time.Duration variable in which to store the value of the flag.
func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
commandLine.Var(newDurationValue(value, p), name, usage)
}
// Duration defines a time.Duration flag with specified name, default value, and usage string.
// The return value is the address of a time.Duration variable that stores the value of the flag.
func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
p := new(time.Duration)
f.DurationVar(p, name, value, usage)
return p
}
// Duration defines a time.Duration flag with specified name, default value, and usage string.
// The return value is the address of a time.Duration variable that stores the value of the flag.
func Duration(name string, value time.Duration, usage string) *time.Duration {
return commandLine.Duration(name, value, usage)
}
// Var defines a flag with the specified name and usage string. The type and
// value of the flag are represented by the first argument, of type Value, which
// typically holds a user-defined implementation of Value. For instance, the
// caller could create a flag that turns a comma-separated string into a slice
// of strings by giving the slice the methods of Value; in particular, Set would
// decompose the comma-separated string into the slice.
func (f *FlagSet) Var(value Value, name string, usage string) {
// Remember the default value as a string; it won't change.
flag := &Flag{name, usage, value, value.String()}
_, alreadythere := f.formal[name]
if alreadythere {
msg := fmt.Sprintf("%s flag redefined: %s", f.name, name)
fmt.Fprintln(f.out(), msg)
panic(msg) // Happens only if flags are declared with identical names
}
if f.formal == nil {
f.formal = make(map[string]*Flag)
}
f.formal[name] = flag
}
// Var defines a flag with the specified name and usage string. The type and
// value of the flag are represented by the first argument, of type Value, which
// typically holds a user-defined implementation of Value. For instance, the
// caller could create a flag that turns a comma-separated string into a slice
// of strings by giving the slice the methods of Value; in particular, Set would
// decompose the comma-separated string into the slice.
func Var(value Value, name string, usage string) {
commandLine.Var(value, name, usage)
}
// failf prints to standard error a formatted error and usage message and
// returns the error.
func (f *FlagSet) failf(format string, a ...interface{}) error {
err := fmt.Errorf(format, a...)
fmt.Fprintln(f.out(), err)
f.usage()
return err
}
// usage calls the Usage method for the flag set, or the usage function if
// the flag set is commandLine.
func (f *FlagSet) usage() {
if f == commandLine {
Usage()
} else if f.Usage == nil {
defaultUsage(f)
} else {
f.Usage()
}
}
// parseOne parses one flag. It returns whether a flag was seen.
func (f *FlagSet) parseOne() (bool, error) {
if len(f.args) == 0 {
return false, nil
}
s := f.args[0]
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
return false, nil
}
num_minuses := 1
if s[1] == '-' {
num_minuses++
if len(s) == 2 { // "--" terminates the flags
f.args = f.args[1:]
return false, nil
}
}
name := s[num_minuses:]
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
return false, f.failf("bad flag syntax: %s", s)
}
// it's a flag. does it have an argument?
f.args = f.args[1:]
has_value := false
value := ""
for i := 1; i < len(name); i++ { // equals cannot be first
if name[i] == '=' {
value = name[i+1:]
has_value = true
name = name[0:i]
break
}
}
m := f.formal
flag, alreadythere := m[name] // BUG
if !alreadythere {
if name == "help" || name == "h" { // special case for nice help message.
f.usage()
return false, ErrHelp
}
return false, f.failf("flag provided but not defined: -%s", name)
}
if fv, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
if has_value {
if err := fv.Set(value); err != nil {
f.failf("invalid boolean value %q for -%s: %v", value, name, err)
}
} else {
fv.Set("true")
}
} else {
// It must have a value, which might be the next argument.
if !has_value && len(f.args) > 0 {
// value is the next arg
has_value = true
value, f.args = f.args[0], f.args[1:]
}
if !has_value {
return false, f.failf("flag needs an argument: -%s", name)
}
if err := flag.Value.Set(value); err != nil {
return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
}
}
if f.actual == nil {
f.actual = make(map[string]*Flag)
}
f.actual[name] = flag
return true, nil
}
// Parse parses flag definitions from the argument list, which should not
// include the command name. Must be called after all flags in the FlagSet
// are defined and before flags are accessed by the program.
// The return value will be ErrHelp if -help was set but not defined.
func (f *FlagSet) Parse(arguments []string) error {
f.parsed = true
f.args = arguments
for {
seen, err := f.parseOne()
if seen {
continue
}
if err == nil {
break
}
switch f.errorHandling {
case ContinueOnError:
return err
case ExitOnError:
os.Exit(2)
case PanicOnError:
panic(err)
}
}
return nil
}
// Parsed reports whether f.Parse has been called.
func (f *FlagSet) Parsed() bool {
return f.parsed
}
// Parse parses the command-line flags from os.Args[1:]. Must be called
// after all flags are defined and before flags are accessed by the program.
func Parse() {
// Ignore errors; commandLine is set for ExitOnError.
commandLine.Parse(os.Args[1:])
}
// Parsed returns true if the command-line flags have been parsed.
func Parsed() bool {
return commandLine.Parsed()
}
// The default set of command-line flags, parsed from os.Args.
var commandLine = NewFlagSet(os.Args[0], ExitOnError)
// NewFlagSet returns a new, empty flag set with the specified name and
// error handling property.
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
f := &FlagSet{
name: name,
errorHandling: errorHandling,
}
return f
}
// Init sets the name and error handling property for a flag set.
// By default, the zero FlagSet uses an empty name and the
// ContinueOnError error handling policy.
func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
f.name = name
f.errorHandling = errorHandling
}

View File

@ -1,288 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flag_test
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"testing"
"time"
. "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
)
var (
test_bool = Bool("test_bool", false, "bool value")
test_int = Int("test_int", 0, "int value")
test_int64 = Int64("test_int64", 0, "int64 value")
test_uint = Uint("test_uint", 0, "uint value")
test_uint64 = Uint64("test_uint64", 0, "uint64 value")
test_string = String("test_string", "0", "string value")
test_float64 = Float64("test_float64", 0, "float64 value")
test_duration = Duration("test_duration", 0, "time.Duration value")
)
func boolString(s string) string {
if s == "0" {
return "false"
}
return "true"
}
func TestEverything(t *testing.T) {
m := make(map[string]*Flag)
desired := "0"
visitor := func(f *Flag) {
if len(f.Name) > 5 && f.Name[0:5] == "test_" {
m[f.Name] = f
ok := false
switch {
case f.Value.String() == desired:
ok = true
case f.Name == "test_bool" && f.Value.String() == boolString(desired):
ok = true
case f.Name == "test_duration" && f.Value.String() == desired+"s":
ok = true
}
if !ok {
t.Error("Visit: bad value", f.Value.String(), "for", f.Name)
}
}
}
VisitAll(visitor)
if len(m) != 8 {
t.Error("VisitAll misses some flags")
for k, v := range m {
t.Log(k, *v)
}
}
m = make(map[string]*Flag)
Visit(visitor)
if len(m) != 0 {
t.Errorf("Visit sees unset flags")
for k, v := range m {
t.Log(k, *v)
}
}
// Now set all flags
Set("test_bool", "true")
Set("test_int", "1")
Set("test_int64", "1")
Set("test_uint", "1")
Set("test_uint64", "1")
Set("test_string", "1")
Set("test_float64", "1")
Set("test_duration", "1s")
desired = "1"
Visit(visitor)
if len(m) != 8 {
t.Error("Visit fails after set")
for k, v := range m {
t.Log(k, *v)
}
}
// Now test they're visited in sort order.
var flagNames []string
Visit(func(f *Flag) { flagNames = append(flagNames, f.Name) })
if !sort.StringsAreSorted(flagNames) {
t.Errorf("flag names not sorted: %v", flagNames)
}
}
func TestUsage(t *testing.T) {
called := false
ResetForTesting(func() { called = true })
if CommandLine().Parse([]string{"-x"}) == nil {
t.Error("parse did not fail for unknown flag")
}
if !called {
t.Error("did not call Usage for unknown flag")
}
}
func testParse(f *FlagSet, t *testing.T) {
if f.Parsed() {
t.Error("f.Parse() = true before Parse")
}
boolFlag := f.Bool("bool", false, "bool value")
bool2Flag := f.Bool("bool2", false, "bool2 value")
intFlag := f.Int("int", 0, "int value")
int64Flag := f.Int64("int64", 0, "int64 value")
uintFlag := f.Uint("uint", 0, "uint value")
uint64Flag := f.Uint64("uint64", 0, "uint64 value")
stringFlag := f.String("string", "0", "string value")
float64Flag := f.Float64("float64", 0, "float64 value")
durationFlag := f.Duration("duration", 5*time.Second, "time.Duration value")
extra := "one-extra-argument"
args := []string{
"-bool",
"-bool2=true",
"--int", "22",
"--int64", "0x23",
"-uint", "24",
"--uint64", "25",
"-string", "hello",
"-float64", "2718e28",
"-duration", "2m",
extra,
}
if err := f.Parse(args); err != nil {
t.Fatal(err)
}
if !f.Parsed() {
t.Error("f.Parse() = false after Parse")
}
if *boolFlag != true {
t.Error("bool flag should be true, is ", *boolFlag)
}
if *bool2Flag != true {
t.Error("bool2 flag should be true, is ", *bool2Flag)
}
if *intFlag != 22 {
t.Error("int flag should be 22, is ", *intFlag)
}
if *int64Flag != 0x23 {
t.Error("int64 flag should be 0x23, is ", *int64Flag)
}
if *uintFlag != 24 {
t.Error("uint flag should be 24, is ", *uintFlag)
}
if *uint64Flag != 25 {
t.Error("uint64 flag should be 25, is ", *uint64Flag)
}
if *stringFlag != "hello" {
t.Error("string flag should be `hello`, is ", *stringFlag)
}
if *float64Flag != 2718e28 {
t.Error("float64 flag should be 2718e28, is ", *float64Flag)
}
if *durationFlag != 2*time.Minute {
t.Error("duration flag should be 2m, is ", *durationFlag)
}
if len(f.Args()) != 1 {
t.Error("expected one argument, got", len(f.Args()))
} else if f.Args()[0] != extra {
t.Errorf("expected argument %q got %q", extra, f.Args()[0])
}
}
func TestParse(t *testing.T) {
ResetForTesting(func() { t.Error("bad parse") })
testParse(CommandLine(), t)
}
func TestFlagSetParse(t *testing.T) {
testParse(NewFlagSet("test", ContinueOnError), t)
}
// Declare a user-defined flag type.
type flagVar []string
func (f *flagVar) String() string {
return fmt.Sprint([]string(*f))
}
func (f *flagVar) Set(value string) error {
*f = append(*f, value)
return nil
}
func (f *flagVar) Get() interface{} { return []string(*f) }
func TestUserDefined(t *testing.T) {
var flags FlagSet
flags.Init("test", ContinueOnError)
var v flagVar
flags.Var(&v, "v", "usage")
if err := flags.Parse([]string{"-v", "1", "-v", "2", "-v=3"}); err != nil {
t.Error(err)
}
if len(v) != 3 {
t.Fatal("expected 3 args; got ", len(v))
}
expect := "[1 2 3]"
if v.String() != expect {
t.Errorf("expected value %q got %q", expect, v.String())
}
}
func TestSetOutput(t *testing.T) {
var flags FlagSet
var buf bytes.Buffer
flags.SetOutput(&buf)
flags.Init("test", ContinueOnError)
flags.Parse([]string{"-unknown"})
if out := buf.String(); !strings.Contains(out, "-unknown") {
t.Logf("expected output mentioning unknown; got %q", out)
}
}
// This tests that one can reset the flags. This still works but not well, and is
// superseded by FlagSet.
func TestChangingArgs(t *testing.T) {
ResetForTesting(func() { t.Fatal("bad parse") })
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = []string{"cmd", "-before", "subcmd", "-after", "args"}
before := Bool("before", false, "")
if err := CommandLine().Parse(os.Args[1:]); err != nil {
t.Fatal(err)
}
cmd := Arg(0)
os.Args = Args()
after := Bool("after", false, "")
Parse()
args := Args()
if !*before || cmd != "subcmd" || !*after || len(args) != 1 || args[0] != "args" {
t.Fatalf("expected true subcmd true [args] got %v %v %v %v", *before, cmd, *after, args)
}
}
// Test that -help invokes the usage message and returns ErrHelp.
func TestHelp(t *testing.T) {
var helpCalled = false
fs := NewFlagSet("help test", ContinueOnError)
fs.Usage = func() { helpCalled = true }
var flag bool
fs.BoolVar(&flag, "flag", false, "regular flag")
// Regular flag invocation should work
err := fs.Parse([]string{"-flag=true"})
if err != nil {
t.Fatal("expected no error; got ", err)
}
if !flag {
t.Error("flag was not set by -flag")
}
if helpCalled {
t.Error("help called for regular flag")
helpCalled = false // reset for next test
}
// Help flag should work as expected.
err = fs.Parse([]string{"-help"})
if err == nil {
t.Fatal("error expected")
}
if err != ErrHelp {
t.Fatal("expected ErrHelp; got ", err)
}
if !helpCalled {
t.Fatal("help was not called")
}
// If we define a help flag, that should override.
var help bool
fs.BoolVar(&help, "help", false, "help flag")
helpCalled = false
err = fs.Parse([]string{"-help"})
if err != nil {
t.Fatal("expected no error for defined -help; got ", err)
}
if helpCalled {
t.Fatal("help was called; should not have been for defined help flag")
}
}

View File

@ -1,11 +0,0 @@
# This is the official list of Go-Commander authors for copyright purposes.
# This file is distinct from the CONTRIBUTORS files.
# See the latter for an explanation.
# Names should be added to this file as
# Name or Organization <email address>
# The email address is not required for organizations.
# Please keep the list sorted.
Google Inc

View File

@ -1,31 +0,0 @@
# This is the official list of people who can contribute
# (and typically have contributed) code to the Go-Commander repository.
# The AUTHORS file lists the copyright holders; this file
# lists people. For example, Google employees are listed here
# but not in AUTHORS, because Google holds the copyright.
#
# The submission process automatically checks to make sure
# that people submitting code are listed in this file (by email address).
#
# Names should be added to this file only after verifying that
# the individual or the individual's organization has agreed to
# the appropriate Contributor License Agreement, found here:
#
# http://code.google.com/legal/individual-cla-v1.0.html
# http://code.google.com/legal/corporate-cla-v1.0.html
#
# The agreement for individuals can be filled out on the web.
#
# When adding J Random Contributor's name to this file,
# either J's name or J's organization's name should be
# added to the AUTHORS file, depending on whether the
# individual or corporate CLA was used.
# Names should be added to this file like so:
# Name <email address>
# Please keep the list sorted.
Juan Batiz-Benet <juan@benet.ai>
Sebastien Binet <seb.binet@gmail.com>
Yves Junqueira <yves.junqueira@gmail.com>

View File

@ -1,27 +0,0 @@
Copyright (c) 2012 The Go-Commander Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,107 +0,0 @@
commander
============
[![Build Status](https://drone.io/github.com/gonuts/commander/status.png)](https://drone.io/github.com/gonuts/commander/latest)
``commander`` is a spin off of [golang](http://golang.org) ``go tool`` infrastructure to provide commands and sub-commands.
A ``commander.Command`` has a ``Subcommands`` field holding ``[]*commander.Command`` subcommands, referenced by name from the command line.
So a ``Command`` can have sub commands.
So you can have, _e.g._:
```sh
$ mycmd action1 [options...]
$ mycmd subcmd1 action1 [options...]
```
Example provided by:
- [hwaf](https://github.com/hwaf/hwaf)
- [examples/my-cmd](examples/my-cmd)
## Documentation
Is available on [godoc](http://godoc.org/github.com/gonuts/commander)
## Installation
Is performed with the usual:
```sh
$ go get github.com/gonuts/commander
```
## Example
See the simple ``my-cmd`` example command for how this all hangs
together [there](http://github.com/gonuts/commander/blob/master/examples/my-cmd/main.go):
```sh
$ my-cmd cmd1
my-cmd-cmd1: hello from cmd1 (quiet=true)
$ my-cmd cmd1 -q
my-cmd-cmd1: hello from cmd1 (quiet=true)
$ my-cmd cmd1 -q=0
my-cmd-cmd1: hello from cmd1 (quiet=false)
$ my-cmd cmd2
my-cmd-cmd2: hello from cmd2 (quiet=true)
$ my-cmd subcmd1 cmd1
my-cmd-subcmd1-cmd1: hello from subcmd1-cmd1 (quiet=true)
$ my-cmd subcmd1 cmd2
my-cmd-subcmd1-cmd2: hello from subcmd1-cmd2 (quiet=true)
$ my-cmd subcmd2 cmd1
my-cmd-subcmd2-cmd1: hello from subcmd2-cmd1 (quiet=true)
$ my-cmd subcmd2 cmd2
my-cmd-subcmd2-cmd2: hello from subcmd2-cmd2 (quiet=true)
$ my-cmd help
Usage:
my-cmd command [arguments]
The commands are:
cmd1 runs cmd1 and exits
cmd2 runs cmd2 and exits
subcmd1 subcmd1 subcommand. does subcmd1 thingies
subcmd2 subcmd2 subcommand. does subcmd2 thingies
Use "my-cmd help [command]" for more information about a command.
Additional help topics:
Use "my-cmd help [topic]" for more information about that topic.
$ my-cmd help subcmd1
Usage:
subcmd1 command [arguments]
The commands are:
cmd1 runs cmd1 and exits
cmd2 runs cmd2 and exits
Use "subcmd1 help [command]" for more information about a command.
Additional help topics:
Use "subcmd1 help [topic]" for more information about that topic.
```
## TODO
- automatically generate the bash/zsh/csh autocompletion lists
- automatically generate Readme examples text
- test cases

View File

@ -1,358 +0,0 @@
// Copyright 2012 The Go-Commander Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Based on the original work by The Go Authors:
// Copyright 2011 The Go Authors. All rights reserved.
// commander helps creating command line programs whose arguments are flags,
// commands and subcommands.
package commander
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"sort"
"strings"
"text/template"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
)
// UsageSection differentiates between sections in the usage text.
type Listing int
const (
CommandsList = iota
HelpTopicsList
Unlisted
)
// A Command is an implementation of a subcommand.
type Command struct {
// UsageLine is the short usage message.
// The first word in the line is taken to be the command name.
UsageLine string
// Short is the short description line shown in command lists.
Short string
// Long is the long description shown in the 'help <this-command>' output.
Long string
// List reports which list to show this command in Usage and Help.
// Choose between {CommandsList (default), HelpTopicsList, Unlisted}
List Listing
// Run runs the command.
// The args are the arguments after the command name.
Run func(cmd *Command, args []string) error
// Flag is a set of flags specific to this command.
Flag flag.FlagSet
// CustomFlags indicates that the command will do its own
// flag parsing.
CustomFlags bool
// Subcommands are dispatched from this command
Subcommands []*Command
// Parent command, nil for root.
Parent *Command
// UsageTemplate formats the usage (short) information displayed to the user
// (leave empty for default)
UsageTemplate string
// HelpTemplate formats the help (long) information displayed to the user
// (leave empty for default)
HelpTemplate string
// Stdout and Stderr by default are os.Stdout and os.Stderr, but you can
// point them at any io.Writer
Stdout io.Writer
Stderr io.Writer
}
// Name returns the command's name: the first word in the usage line.
func (c *Command) Name() string {
name := c.UsageLine
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}
// Usage prints the usage details to the standard error output.
func (c *Command) Usage() {
c.usage()
}
// FlagOptions returns the flag's options as a string
func (c *Command) FlagOptions() string {
var buf bytes.Buffer
c.Flag.SetOutput(&buf)
c.Flag.PrintDefaults()
str := string(buf.Bytes())
if len(str) > 0 {
return fmt.Sprintf("\nOptions:\n%s", str)
}
return ""
}
// Runnable reports whether the command can be run; otherwise
// it is a documentation pseudo-command such as importpath.
func (c *Command) Runnable() bool {
return c.Run != nil
}
// Type to allow us to use sort.Sort on a slice of Commands
type CommandSlice []*Command
func (c CommandSlice) Len() int {
return len(c)
}
func (c CommandSlice) Less(i, j int) bool {
return c[i].Name() < c[j].Name()
}
func (c CommandSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
// Sort the commands
func (c *Command) SortCommands() {
sort.Sort(CommandSlice(c.Subcommands))
}
// Init the command
func (c *Command) init() {
if c.Parent != nil {
return // already initialized.
}
// setup strings
if len(c.UsageLine) < 1 {
c.UsageLine = Defaults.UsageLine
}
if len(c.UsageTemplate) < 1 {
c.UsageTemplate = Defaults.UsageTemplate
}
if len(c.HelpTemplate) < 1 {
c.HelpTemplate = Defaults.HelpTemplate
}
if c.Stderr == nil {
c.Stderr = os.Stderr
}
if c.Stdout == nil {
c.Stdout = os.Stdout
}
// init subcommands
for _, cmd := range c.Subcommands {
cmd.init()
}
// init hierarchy...
for _, cmd := range c.Subcommands {
cmd.Parent = c
}
}
// Dispatch executes the command using the provided arguments.
// If a subcommand exists matching the first argument, it is dispatched.
// Otherwise, the command's Run function is called.
func (c *Command) Dispatch(args []string) error {
if c == nil {
return fmt.Errorf("Called Run() on a nil Command")
}
// Ensure command is initialized.
c.init()
// First, try a sub-command
if len(args) > 0 {
for _, cmd := range c.Subcommands {
n := cmd.Name()
if n == args[0] {
return cmd.Dispatch(args[1:])
}
}
// help is builtin (but after, to allow overriding)
if args[0] == "help" {
return c.help(args[1:])
}
// then, try out an external binary (git-style)
bin, err := exec.LookPath(c.FullName() + "-" + args[0])
if err == nil {
cmd := exec.Command(bin, args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = c.Stdout
cmd.Stderr = c.Stderr
return cmd.Run()
}
}
// then, try running this command
if c.Runnable() {
if !c.CustomFlags {
var err = error(nil)
c.Flag.Usage = func() {
c.Usage()
err = fmt.Errorf("Failed to parse flags.")
}
c.Flag.Parse(args)
if err != nil {
return err
}
args = c.Flag.Args()
}
return c.Run(c, args)
}
// TODO: try an alias
//...
// Last, print usage
if err := c.usage(); err != nil {
return err
}
return nil
}
func (c *Command) usage() error {
// c.SortCommands()
err := tmpl(c.Stderr, c.UsageTemplate, c)
if err != nil {
fmt.Println(err)
}
return err
}
// help implements the 'help' command.
func (c *Command) help(args []string) error {
// help exactly for this command?
if len(args) == 0 {
if len(c.Long) > 0 {
return tmpl(c.Stdout, c.HelpTemplate, c)
} else {
return c.usage()
}
}
arg := args[0]
// is this help for a subcommand?
for _, cmd := range c.Subcommands {
n := cmd.Name()
// strip out "<parent>-"" name
if strings.HasPrefix(n, c.Name()+"-") {
n = n[len(c.Name()+"-"):]
}
if n == arg {
return cmd.help(args[1:])
}
}
return fmt.Errorf("Unknown help topic %#q. Run '%v help'.\n", arg, c.Name())
}
func (c *Command) MaxLen() (res int) {
res = 0
for _, cmd := range c.Subcommands {
i := len(cmd.Name())
if i > res {
res = i
}
}
return
}
// ColFormat returns the column header size format for printing in the template
func (c *Command) ColFormat() string {
sz := c.MaxLen()
if sz < 11 {
sz = 11
}
return fmt.Sprintf("%%-%ds", sz)
}
// FullName returns the full name of the command, prefixed with parent commands
func (c *Command) FullName() string {
n := c.Name()
if c.Parent != nil {
n = c.Parent.FullName() + "-" + n
}
return n
}
// FullSpacedName returns the full name of the command, with ' ' instead of '-'
func (c *Command) FullSpacedName() string {
n := c.Name()
if c.Parent != nil {
n = c.Parent.FullSpacedName() + " " + n
}
return n
}
func (c *Command) SubcommandList(list Listing) []*Command {
var cmds []*Command
for _, cmd := range c.Subcommands {
if cmd.List == list {
cmds = append(cmds, cmd)
}
}
return cmds
}
var Defaults = Command{
UsageTemplate: `{{if .Runnable}}Usage: {{if .Parent}}{{.Parent.FullSpacedName}}{{end}} {{.UsageLine}}
{{else}}{{.FullSpacedName}} - {{end}}{{.Short}}
{{if commandList}}Commands:
{{range commandList}}
{{.Name | printf (colfmt)}} {{.Short}}{{end}}
Use "{{.Name}} help <command>" for more information about a command.
{{end}}{{.FlagOptions}}{{if helpList}}
Additional help topics:
{{range helpList}}
{{.Name | printf (colfmt)}} {{.Short}}{{end}}
Use "{{.Name}} help <topic>" for more information about that topic.
{{end}}`,
HelpTemplate: `{{if .Runnable}}Usage: {{if .Parent}}{{.Parent.FullSpacedName}}{{end}} {{.UsageLine}}
{{end}}{{.Long | trim}}
{{.FlagOptions}}
`,
}
// tmpl executes the given template text on data, writing the result to w.
func tmpl(w io.Writer, text string, data interface{}) error {
t := template.New("top")
t.Funcs(template.FuncMap{
"trim": strings.TrimSpace,
"colfmt": func() string { return data.(*Command).ColFormat() },
"commandList": func() []*Command { return data.(*Command).SubcommandList(CommandsList) },
"helpList": func() []*Command { return data.(*Command).SubcommandList(HelpTopicsList) },
})
template.Must(t.Parse(text))
return t.Execute(w, data)
}

View File

@ -1,34 +0,0 @@
package main
import (
"fmt"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
var cmd_cmd1 = &commander.Command{
Run: ex_run_cmd_cmd1,
UsageLine: "cmd1 [options]",
Short: "runs cmd1 and exits",
Long: `
runs cmd1 and exits.
ex:
$ my-cmd cmd1
`,
Flag: *flag.NewFlagSet("my-cmd-cmd1", flag.ExitOnError),
}
func init() {
cmd_cmd1.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
}
func ex_run_cmd_cmd1(cmd *commander.Command, args []string) error {
name := "my-cmd-" + cmd.Name()
quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
fmt.Printf("%s: hello from cmd1 (quiet=%v)\n", name, quiet)
return nil
}
// EOF

View File

@ -1,34 +0,0 @@
package main
import (
"fmt"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
func ex_make_cmd_cmd2() *commander.Command {
cmd := &commander.Command{
Run: ex_run_cmd_cmd2,
UsageLine: "cmd2 [options]",
Short: "runs cmd2 and exits",
Long: `
runs cmd2 and exits.
ex:
$ my-cmd cmd2
`,
Flag: *flag.NewFlagSet("my-cmd-cmd2", flag.ExitOnError),
}
cmd.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
return cmd
}
func ex_run_cmd_cmd2(cmd *commander.Command, args []string) error {
name := "my-cmd-" + cmd.Name()
quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
fmt.Printf("%s: hello from cmd2 (quiet=%v)\n", name, quiet)
return nil
}
// EOF

View File

@ -1,18 +0,0 @@
package main
import (
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
var cmd_subcmd1 = &commander.Command{
UsageLine: "subcmd1 <command>",
Short: "subcmd1 subcommand. does subcmd1 thingies",
Subcommands: []*commander.Command{
cmd_subcmd1_cmd1,
cmd_subcmd1_cmd2,
},
Flag: *flag.NewFlagSet("my-cmd-subcmd1", flag.ExitOnError),
}
// EOF

View File

@ -1,34 +0,0 @@
package main
import (
"fmt"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
var cmd_subcmd1_cmd1 = &commander.Command{
Run: ex_run_cmd_subcmd1_cmd1,
UsageLine: "cmd1 [options]",
Short: "runs cmd1 and exits",
Long: `
runs cmd1 and exits.
ex:
$ my-cmd subcmd1 cmd1
`,
Flag: *flag.NewFlagSet("my-cmd-subcmd1-cmd1", flag.ExitOnError),
}
func init() {
cmd_subcmd1_cmd1.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
}
func ex_run_cmd_subcmd1_cmd1(cmd *commander.Command, args []string) error {
name := "my-cmd-subcmd1-" + cmd.Name()
quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
fmt.Printf("%s: hello from subcmd1-cmd1 (quiet=%v)\n", name, quiet)
return nil
}
// EOF

View File

@ -1,34 +0,0 @@
package main
import (
"fmt"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
var cmd_subcmd1_cmd2 = &commander.Command{
Run: ex_run_cmd_subcmd1_cmd2,
UsageLine: "cmd2 [options]",
Short: "runs cmd2 and exits",
Long: `
runs cmd2 and exits.
ex:
$ my-cmd subcmd1 cmd2
`,
Flag: *flag.NewFlagSet("my-cmd-subcmd1-cmd2", flag.ExitOnError),
}
func init() {
cmd_subcmd1_cmd2.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
}
func ex_run_cmd_subcmd1_cmd2(cmd *commander.Command, args []string) error {
name := "my-cmd-subcmd1-" + cmd.Name()
quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
fmt.Printf("%s: hello from subcmd1-cmd2 (quiet=%v)\n", name, quiet)
return nil
}
// EOF

View File

@ -1,22 +0,0 @@
package main
import (
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
func ex_make_cmd_subcmd2() *commander.Command {
cmd := &commander.Command{
UsageLine: "subcmd2",
Short: "subcmd2 subcommand. does subcmd2 thingies (help list)",
List: commander.HelpTopicsList,
Subcommands: []*commander.Command{
ex_make_cmd_subcmd2_cmd1(),
ex_make_cmd_subcmd2_cmd2(),
},
Flag: *flag.NewFlagSet("my-cmd-subcmd2", flag.ExitOnError),
}
return cmd
}
// EOF

View File

@ -1,34 +0,0 @@
package main
import (
"fmt"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
func ex_make_cmd_subcmd2_cmd1() *commander.Command {
cmd := &commander.Command{
Run: ex_run_cmd_subcmd2_cmd1,
UsageLine: "cmd1 [options]",
Short: "runs cmd1 and exits",
Long: `
runs cmd1 and exits.
ex:
$ my-cmd subcmd2 cmd1
`,
Flag: *flag.NewFlagSet("my-cmd-subcmd2-cmd1", flag.ExitOnError),
}
cmd.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
return cmd
}
func ex_run_cmd_subcmd2_cmd1(cmd *commander.Command, args []string) error {
name := "my-cmd-subcmd2-" + cmd.Name()
quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
fmt.Printf("%s: hello from subcmd2-cmd1 (quiet=%v)\n", name, quiet)
return nil
}
// EOF

View File

@ -1,34 +0,0 @@
package main
import (
"fmt"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
func ex_make_cmd_subcmd2_cmd2() *commander.Command {
cmd := &commander.Command{
Run: ex_run_cmd_subcmd2_cmd2,
UsageLine: "cmd2 [options]",
Short: "runs cmd2 and exits",
Long: `
runs cmd2 and exits.
ex:
$ my-cmd subcmd2 cmd2
`,
Flag: *flag.NewFlagSet("my-cmd-subcmd2-cmd2", flag.ExitOnError),
}
cmd.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
return cmd
}
func ex_run_cmd_subcmd2_cmd2(cmd *commander.Command, args []string) error {
name := "my-cmd-subcmd2-" + cmd.Name()
quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
fmt.Printf("%s: hello from subcmd2-cmd2 (quiet=%v)\n", name, quiet)
return nil
}
// EOF

View File

@ -1,33 +0,0 @@
package main
import (
"fmt"
"os"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
var g_cmd = &commander.Command{
UsageLine: os.Args[0] + " does cool things",
}
func init() {
g_cmd.Subcommands = []*commander.Command{
cmd_cmd1,
ex_make_cmd_cmd2(),
cmd_subcmd1,
ex_make_cmd_subcmd2(),
}
}
func main() {
err := g_cmd.Dispatch(os.Args[1:])
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
return
}
// EOF

View File

@ -0,0 +1,18 @@
{
"ImportPath": "github.com/jbenet/go-multiaddr-net",
"GoVersion": "go1.3",
"Packages": [
"./..."
],
"Deps": [
{
"ImportPath": "github.com/h2so5/utp",
"Rev": "654d875bb65e96729678180215cf080fe2810371"
},
{
"ImportPath": "github.com/jbenet/go-multiaddr",
"Comment": "0.1.2-30-g99cf3ed",
"Rev": "99cf3edc711751cf7b43505fac0e3913f6b9a75c"
}
]
}

View File

@ -0,0 +1,5 @@
This directory tree is generated automatically by godep.
Please do not edit.
See https://github.com/tools/godep for more information.

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Juan Batiz-Benet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,16 @@
all: install
godep:
go get github.com/tools/godep
# saves/vendors third-party dependencies to Godeps/_workspace
# -r flag rewrites import paths to use the vendored path
# ./... performs operation on all packages in tree
vendor: godep
godep save -r ./...
install: dep
cd multiaddr && go install
dep:
cd multiaddr && go get ./...

View File

@ -1,11 +1,11 @@
# multiaddr/net - Multiaddr friendly net
Package multiaddr/net provides Multiaddr specific versions of common
Package multiaddr/net provides [Multiaddr](http://github.com/jbenet/go-multiaddr) specific versions of common
functions in stdlib's net package. This means wrappers of
standard net symbols like net.Dial and net.Listen, as well
as conversion to/from net.Addr.
Docs:
- `multiaddr/net`: https://godoc.org/github.com/jbenet/go-multiaddr/net
- `multiaddr/net`: https://godoc.org/github.com/jbenet/go-multiaddr-net
- `multiaddr`: https://godoc.org/github.com/jbenet/go-multiaddr

View File

@ -5,6 +5,7 @@ import (
"net"
"strings"
utp "github.com/h2so5/utp"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
@ -55,6 +56,33 @@ func FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
// Encapsulate
return ipm.Encapsulate(udpm), nil
case "utp", "utp4", "utp6":
acc, ok := a.(*utp.UTPAddr)
if !ok {
return nil, errIncorrectNetAddr
}
// Get UDP Addr
ac, ok := acc.Addr.(*net.UDPAddr)
if !ok {
return nil, errIncorrectNetAddr
}
// Get IP Addr
ipm, err := FromIP(ac.IP)
if err != nil {
return nil, errIncorrectNetAddr
}
// Get UDP Addr
utpm, err := ma.NewMultiaddr(fmt.Sprintf("/udp/%d/utp", ac.Port))
if err != nil {
return nil, errIncorrectNetAddr
}
// Encapsulate
return ipm.Encapsulate(utpm), nil
case "ip", "ip4", "ip6":
ac, ok := a.(*net.IPAddr)
if !ok {
@ -88,6 +116,8 @@ func ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
return net.ResolveTCPAddr(network, host)
case "udp":
return net.ResolveUDPAddr(network, host)
case "utp":
return utp.ResolveUTPAddr(network, host)
case "ip":
return net.ResolveIPAddr(network, host)
}
@ -121,6 +151,9 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
}
network := parts[2]
if parts[2] == "udp" && len(parts) > 4 && parts[4] == "utp" {
network = parts[4]
}
var host string
switch parts[0] {
case "ip4":

View File

@ -4,6 +4,7 @@ import (
"net"
"testing"
utp "github.com/h2so5/utp"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
@ -88,17 +89,31 @@ func TestFromUDP(t *testing.T) {
})
}
func TestFromUTP(t *testing.T) {
testConvert(t, "/ip4/10.20.30.40/udp/1234/utp", func() (ma.Multiaddr, error) {
return FromNetAddr(&utp.UTPAddr{
Addr: &net.UDPAddr{
IP: net.ParseIP("10.20.30.40"),
Port: 1234,
},
})
})
}
func TestThinWaist(t *testing.T) {
addrs := map[string]bool{
"/ip4/127.0.0.1/udp/1234": true,
"/ip4/127.0.0.1/tcp/1234": true,
"/ip4/127.0.0.1/udp/1234/utp": true,
"/ip4/127.0.0.1/udp/1234/tcp/1234": true,
"/ip4/127.0.0.1/tcp/12345/ip4/1.2.3.4": true,
"/ip6/::1/tcp/80": true,
"/ip6/::1/udp/80": true,
"/ip6/::1": true,
"/ip6/::1/utp": false,
"/tcp/1234/ip4/1.2.3.4": false,
"/tcp/1234": false,
"/tcp/1234/utp": false,
"/tcp/1234/udp/1234": false,
"/ip4/1.2.3.4/ip4/2.3.4.5": true,
"/ip6/::1/ip4/2.3.4.5": true,
@ -117,21 +132,27 @@ func TestThinWaist(t *testing.T) {
}
func TestDialArgs(t *testing.T) {
m, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
if err != nil {
t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234")
test := func(e_maddr, e_nw, e_host string) {
m, err := ma.NewMultiaddr(e_maddr)
if err != nil {
t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234", e_maddr)
}
nw, host, err := DialArgs(m)
if err != nil {
t.Fatal("failed to get dial args", e_maddr, m, err)
}
if nw != e_nw {
t.Error("failed to get udp network Dial Arg", e_nw, nw)
}
if host != e_host {
t.Error("failed to get host:port Dial Arg", e_host, host)
}
}
nw, host, err := DialArgs(m)
if err != nil {
t.Fatal("failed to get dial args", "/ip4/127.0.0.1/udp/1234", err)
}
if nw != "udp" {
t.Error("failed to get udp network Dial Arg")
}
if host != "127.0.0.1:1234" {
t.Error("failed to get host:port Dial Arg")
}
test("/ip4/127.0.0.1/udp/1234", "udp", "127.0.0.1:1234")
test("/ip4/127.0.0.1/tcp/4321", "tcp", "127.0.0.1:4321")
test("/ip4/127.0.0.1/udp/1234/utp", "utp", "127.0.0.1:1234")
}

Binary file not shown.

View File

@ -7,7 +7,7 @@ import (
"os"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
)
// flags

View File

@ -4,6 +4,7 @@ import (
"fmt"
"net"
utp "github.com/h2so5/utp"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)
@ -68,7 +69,7 @@ func (c *maConn) RemoteMultiaddr() ma.Multiaddr {
// and RemoteAddr options are Multiaddrs, instead of net.Addrs.
type Dialer struct {
// Dialer is just an embed net.Dialer, with all its options.
// Dialer is just an embedded net.Dialer, with all its options.
net.Dialer
// LocalAddr is the local address to use when dialing an
@ -103,9 +104,24 @@ func (d *Dialer) Dial(remote ma.Multiaddr) (Conn, error) {
}
// ok, Dial!
nconn, err := d.Dialer.Dial(rnet, rnaddr)
if err != nil {
return nil, err
var nconn net.Conn
switch rnet {
case "tcp":
nconn, err = d.Dialer.Dial(rnet, rnaddr)
if err != nil {
return nil, err
}
case "utp":
// construct utp dialer, with options on our net.Dialer
utpd := utp.Dialer{
Timeout: d.Dialer.Timeout,
LocalAddr: d.Dialer.LocalAddr,
}
nconn, err = utpd.Dial(rnet, rnaddr)
if err != nil {
return nil, err
}
}
// get local address (pre-specified or assigned within net.Conn)
@ -206,9 +222,18 @@ func Listen(laddr ma.Multiaddr) (Listener, error) {
return nil, err
}
nl, err := net.Listen(lnet, lnaddr)
if err != nil {
return nil, err
var nl net.Listener
switch lnet {
case "utp":
nl, err = utp.Listen(lnet, lnaddr)
if err != nil {
return nil, err
}
case "tcp":
nl, err = net.Listen(lnet, lnaddr)
if err != nil {
return nil, err
}
}
return &maListener{

View File

@ -12,7 +12,7 @@ import (
func newMultiaddr(t *testing.T, m string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(m)
if err != nil {
t.Fatalf("failed to construct multiaddr: %s", m)
t.Fatal("failed to construct multiaddr:", m, err)
}
return maddr
}
@ -199,6 +199,67 @@ func TestListenAndDial(t *testing.T) {
wg.Wait()
}
func TestListenAndDialUTP(t *testing.T) {
maddr := newMultiaddr(t, "/ip4/127.0.0.1/udp/4323/utp")
listener, err := Listen(maddr)
if err != nil {
t.Fatal("failed to listen")
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
cB, err := listener.Accept()
if err != nil {
t.Fatal("failed to accept")
}
if !cB.LocalMultiaddr().Equal(maddr) {
t.Fatal("local multiaddr not equal:", maddr, cB.LocalMultiaddr())
}
// echo out
buf := make([]byte, 1024)
for {
_, err := cB.Read(buf)
if err != nil {
break
}
cB.Write(buf)
}
wg.Done()
}()
cA, err := Dial(newMultiaddr(t, "/ip4/127.0.0.1/udp/4323/utp"))
if err != nil {
t.Fatal("failed to dial", err)
}
buf := make([]byte, 1024)
if _, err := cA.Write([]byte("beep boop")); err != nil {
t.Fatal("failed to write:", err)
}
if _, err := cA.Read(buf); err != nil {
t.Fatal("failed to read:", buf, err)
}
if !bytes.Equal(buf[:9], []byte("beep boop")) {
t.Fatal("failed to echo:", buf)
}
maddr2 := cA.RemoteMultiaddr()
if !maddr2.Equal(maddr) {
t.Fatal("remote multiaddr not equal:", maddr, maddr2)
}
cA.Close()
wg.Wait()
}
func TestIPLoopback(t *testing.T) {
if IP4Loopback.String() != "/ip4/127.0.0.1" {
t.Error("IP4Loopback incorrect:", IP4Loopback)

View File

@ -28,12 +28,14 @@ func stringToBytes(s string) ([]byte, error) {
if p == nil {
return nil, fmt.Errorf("no protocol with name %s", sp[0])
}
b = append(b, byte(p.Code))
b = append(b, CodeToVarint(p.Code)...)
sp = sp[1:]
a := addressStringToBytes(p, sp[1])
b = append(b, a...)
sp = sp[2:]
if p.Size > 0 {
a := addressStringToBytes(p, sp[0])
b = append(b, a...)
sp = sp[1:]
}
}
return b, nil
}
@ -50,18 +52,22 @@ func bytesToString(b []byte) (ret string, err error) {
s := ""
for len(b) > 0 {
p := ProtocolWithCode(int(b[0]))
code, n := ReadVarintCode(b)
b = b[n:]
p := ProtocolWithCode(code)
if p == nil {
return "", fmt.Errorf("no protocol with code %d", b[0])
return "", fmt.Errorf("no protocol with code %d", code)
}
s = strings.Join([]string{s, "/", p.Name}, "")
b = b[1:]
a := addressBytesToString(p, b[:(p.Size/8)])
if len(a) > 0 {
s = strings.Join([]string{s, "/", a}, "")
if p.Size > 0 {
a := addressBytesToString(p, b[:(p.Size/8)])
if len(a) > 0 {
s = strings.Join([]string{s, "/", a}, "")
}
b = b[(p.Size / 8):]
}
b = b[(p.Size / 8):]
}
return s, nil
@ -78,12 +84,13 @@ func bytesSplit(b []byte) (ret [][]byte, err error) {
ret = [][]byte{}
for len(b) > 0 {
p := ProtocolWithCode(int(b[0]))
code, n := ReadVarintCode(b)
p := ProtocolWithCode(code)
if p == nil {
return [][]byte{}, fmt.Errorf("no protocol with code %d", b[0])
}
length := 1 + (p.Size / 8)
length := n + (p.Size / 8)
ret = append(ret, b[:length])
b = b[length:]
}

View File

@ -67,14 +67,15 @@ func (m *multiaddr) Protocols() []*Protocol {
ps := []*Protocol{}
b := m.bytes[:]
for len(b) > 0 {
p := ProtocolWithCode(int(b[0]))
code, n := ReadVarintCode(b)
p := ProtocolWithCode(code)
if p == nil {
// this is a panic (and not returning err) because this should've been
// caught on constructing the Multiaddr
panic(fmt.Errorf("no protocol with code %d", b[0]))
}
ps = append(ps, p)
b = b[1+(p.Size/8):]
b = b[n+(p.Size/8):]
}
return ps
}

View File

@ -68,6 +68,8 @@ func TestStringToBytes(t *testing.T) {
}
testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1")
}
func TestBytesToString(t *testing.T) {
@ -89,6 +91,8 @@ func TestBytesToString(t *testing.T) {
}
testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1")
}
func TestBytesSplitAndJoin(t *testing.T) {
@ -96,7 +100,7 @@ func TestBytesSplitAndJoin(t *testing.T) {
testString := func(s string, res []string) {
m, err := NewMultiaddr(s)
if err != nil {
t.Error("failed to convert", s)
t.Fatal("failed to convert", s, err)
}
split := Split(m)
@ -132,6 +136,8 @@ func TestBytesSplitAndJoin(t *testing.T) {
testString("/ip4/1.2.3.4/udp/1234", []string{"/ip4/1.2.3.4", "/udp/1234"})
testString("/ip4/1.2.3.4/tcp/1/ip4/2.3.4.5/udp/2",
[]string{"/ip4/1.2.3.4", "/tcp/1", "/ip4/2.3.4.5", "/udp/2"})
testString("/ip4/1.2.3.4/utp/ip4/2.3.4.5/udp/2/udt",
[]string{"/ip4/1.2.3.4", "/utp", "/ip4/2.3.4.5", "/udp/2", "/udt"})
}
func TestProtocols(t *testing.T) {

View File

@ -5,5 +5,7 @@ code size name
33 16 dccp
41 128 ip6
132 16 sctp
301 0 udt
302 0 utp
480 0 http
443 0 https

1 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

View File

@ -1,10 +1,15 @@
package multiaddr
import (
"encoding/binary"
)
// Protocol is a Multiaddr protocol description structure.
type Protocol struct {
Code int
Size int
Name string
Code int
Size int
Name string
VCode []byte
}
// replicating table here to:
@ -18,17 +23,21 @@ const (
P_DCCP = 33
P_IP6 = 41
P_SCTP = 132
P_UTP = 301
P_UDT = 302
)
// Protocols is the list of multiaddr protocols supported by this module.
var Protocols = []*Protocol{
&Protocol{P_IP4, 32, "ip4"},
&Protocol{P_TCP, 16, "tcp"},
&Protocol{P_UDP, 16, "udp"},
&Protocol{P_DCCP, 16, "dccp"},
&Protocol{P_IP6, 128, "ip6"},
&Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4)},
&Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP)},
&Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP)},
&Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP)},
&Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6)},
// these require varint:
&Protocol{P_SCTP, 16, "sctp"},
&Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP)},
&Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)},
&Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)},
// {480, 0, "http"},
// {443, 0, "https"},
}
@ -52,3 +61,26 @@ func ProtocolWithCode(c int) *Protocol {
}
return nil
}
// CodeToVarint converts an integer to a varint-encoded []byte
func CodeToVarint(num int) []byte {
buf := make([]byte, (num/7)+1) // varint package is uint64
n := binary.PutUvarint(buf, uint64(num))
return buf[:n]
}
// VarintToCode converts a varint-encoded []byte to an integer protocol code
func VarintToCode(buf []byte) int {
num, _ := ReadVarintCode(buf)
return num
}
// ReadVarintCode reads a varint code from the beginning of buf.
// returns the code, and the number of bytes read.
func ReadVarintCode(buf []byte) (int, int) {
num, n := binary.Uvarint(buf)
if n < 0 {
panic("varints larger than uint64 not yet supported")
}
return int(num), n
}

View File

@ -6,7 +6,7 @@ import (
manners "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/braintree/manners"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
cmds "github.com/jbenet/go-ipfs/commands"
cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
core "github.com/jbenet/go-ipfs/core"

View File

@ -12,7 +12,7 @@ import (
// TODO rm direct reference to go-logging
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
cmds "github.com/jbenet/go-ipfs/commands"
cmdsCli "github.com/jbenet/go-ipfs/commands/cli"

View File

@ -8,7 +8,7 @@ import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
msgio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"

View File

@ -5,7 +5,7 @@ import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
peer "github.com/jbenet/go-ipfs/peer"
)

View File

@ -3,7 +3,7 @@ package conn
import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
peer "github.com/jbenet/go-ipfs/peer"
ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"

View File

@ -2,7 +2,7 @@ package swarm
import (
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
"github.com/jbenet/go-ipfs/util/eventlog"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"

View File

@ -8,7 +8,7 @@ import (
mux "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
core "github.com/jbenet/go-ipfs/core"