// Parse parses the input commandline string (cmd, flags, and args). // returns the corresponding command Request object. func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *cmds.Command, []string, error) { path, input, cmd := parsePath(input, root) if len(path) == 0 { return nil, nil, path, ErrInvalidSubcmd } opts, stringVals, err := parseOptions(input) if err != nil { return nil, cmd, path, err } optDefs, err := root.GetOptions(path) if err != nil { return nil, cmd, path, err } // check to make sure there aren't any undefined options for k := range opts { if _, found := optDefs[k]; !found { err = fmt.Errorf("Unrecognized option: -%s", k) return nil, cmd, path, err } } req, err := cmds.NewRequest(path, opts, nil, nil, cmd, optDefs) if err != nil { return nil, cmd, path, err } // if -r is provided, and it is associated with the package builtin // recursive path option, allow recursive file paths recursiveOpt := req.Option(cmds.RecShort) recursive := false if recursiveOpt != nil && recursiveOpt.Definition() == cmds.OptionRecursivePath { recursive, _, err = recursiveOpt.Bool() if err != nil { return req, nil, nil, u.ErrCast() } } stringArgs, fileArgs, err := parseArgs(stringVals, stdin, cmd.Arguments, recursive) if err != nil { return req, cmd, path, err } req.SetArguments(stringArgs) file := &cmds.SliceFile{"", fileArgs} req.SetFiles(file) err = cmd.CheckArguments(req) if err != nil { return req, cmd, path, err } return req, cmd, path, nil }
func (ov OptionValue) Float() (value float64, found bool, err error) { if !ov.found { return 0, false, nil } val, ok := ov.value.(float64) if !ok { err = util.ErrCast() } return val, ov.found, err }
func (ov OptionValue) Uint() (value uint, found bool, err error) { if !ov.found { return 0, false, nil } val, ok := ov.value.(uint) if !ok { err = util.ErrCast() } return val, ov.found, err }
// value accessor methods, gets the value as a certain type func (ov OptionValue) Bool() (value bool, found bool, err error) { if !ov.found { return false, false, nil } val, ok := ov.value.(bool) if !ok { err = util.ErrCast() } return val, ov.found, err }
func bootstrapMarshaler(res cmds.Response) ([]byte, error) { v, ok := res.Output().(*BootstrapOutput) if !ok { return nil, u.ErrCast() } var buf bytes.Buffer err := bootstrapWritePeers(&buf, "", v.Peers) return buf.Bytes(), err }
func CastToReaders(slice []interface{}) ([]io.Reader, error) { readers := make([]io.Reader, 0) for _, arg := range slice { reader, ok := arg.(io.Reader) if !ok { return nil, u.ErrCast() } readers = append(readers, reader) } return readers, nil }
func CastToStrings(slice []interface{}) ([]string, error) { strs := make([]string, 0) for _, maybe := range slice { str, ok := maybe.(string) if !ok { return nil, u.ErrCast() } strs = append(strs, str) } return strs, nil }
func (r *request) ConvertOptions() error { for k, v := range r.options { opt, ok := r.optionDefs[k] if !ok { continue } kind := reflect.TypeOf(v).Kind() if kind != opt.Type() { if kind == String { convert := converters[opt.Type()] str, ok := v.(string) if !ok { return u.ErrCast() } val, err := convert(str) if err != nil { value := fmt.Sprintf("value '%v'", v) if len(str) == 0 { value = "empty value" } return fmt.Errorf("Could not convert %s to type '%s' (for option '-%s')", value, opt.Type().String(), k) } r.options[k] = val } else { return fmt.Errorf("Option '%s' should be type '%s', but got type '%s'", k, opt.Type().String(), kind.String()) } } else { r.options[k] = v } for _, name := range opt.Names() { if _, ok := r.options[name]; name != k && ok { return fmt.Errorf("Duplicate command options were provided ('%s' and '%s')", k, name) } } } return nil }
} p, err := node.Routing.FindPeer(ctx, id) if err == kb.ErrLookupFailure { return nil, errors.New(offlineIdErrorMessage) } if err != nil { return nil, err } return printPeer(p) }, Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { val, ok := res.Output().(*IdOutput) if !ok { return nil, u.ErrCast() } return json.MarshalIndent(val, "", "\t") }, }, Type: &IdOutput{}, } func printPeer(p peer.Peer) (interface{}, error) { if p == nil { return nil, errors.New("Attempted to print nil peer!") } info := new(IdOutput) info.ID = p.ID().String()
ID: peer.ID, UptimeSeconds: uint64(peer.LifeSpan.Seconds()), BandwidthBytesIn: peer.BwIn, BandwidthBytesOut: peer.BwOut, Connections: connections, } } return &DiagnosticOutput{output}, nil }, Type: &DiagnosticOutput{}, Marshalers: cmds.MarshalerMap{ cmds.Text: func(r cmds.Response) ([]byte, error) { output, ok := r.Output().(*DiagnosticOutput) if !ok { return nil, util.ErrCast() } var buf bytes.Buffer err := printDiagnostics(&buf, output) if err != nil { return nil, err } return buf.Bytes(), nil }, }, } func printDiagnostics(out io.Writer, info *DiagnosticOutput) error { diagTmpl := ` {{ range $peer := .Peers }}