// ParseArgv parses the command args.
func (c *CmdListTrackers) ParseArgv(ctx *cli.Context) error {
	byUID := ctx.Bool("uid")
	if len(ctx.Args()) == 1 {
		if byUID {
			var err error
			c.uid, err = libkb.UIDFromHex(ctx.Args()[0])
			if err != nil {
				return fmt.Errorf("UID must be a 32-character hex string")
			}
		} else {
			c.username = ctx.Args()[0]
		}
	}

	c.verbose = ctx.Bool("verbose")
	c.json = ctx.Bool("json")
	c.headers = ctx.Bool("headers")

	return nil
}
Exemple #2
0
func (e *SearchEngine) Run(ctx *Context) error {
	APIArgs := libkb.HTTPArgs{
		"q": libkb.S{Val: e.query},
	}
	if e.numWanted > 0 {
		APIArgs["num_wanted"] = libkb.I{Val: e.numWanted}
	}
	res, err := e.G().API.Get(libkb.APIArg{
		Endpoint: "user/autocomplete",
		Args:     APIArgs,
	})
	if err != nil {
		return err
	}
	length, err := res.Body.AtKey("completions").Len()
	if err != nil {
		return fmt.Errorf("Failed to get completions from server.")
	}
	allCompletions := res.Body.AtKey("completions")
	for i := 0; i < length; i++ {
		componentKeys, err := allCompletions.AtIndex(i).AtKey("components").Keys()
		if err != nil {
			return fmt.Errorf("Failed to get completion components from server.")
		}
		completion := allCompletions.AtIndex(i)
		components := completion.AtKey("components")
		searchComponents := []keybase1.SearchComponent{}

		add := func(key string, val string, score float64) {
			searchComponents = append(searchComponents, keybase1.SearchComponent{
				Key:   key,
				Value: val,
				Score: score,
			})
		}

		for _, key := range componentKeys {
			if key == "websites" {
				n, err := components.AtKey(key).Len()
				if err != nil {
					return err
				}
				for i := 0; i < n; i++ {
					obj := components.AtKey(key).AtIndex(i)
					val, err := obj.AtKey("val").GetString()
					if err != nil {
						return err
					}
					score, err := obj.AtKey("score").GetFloat()
					if err != nil {
						return err
					}
					protocol, err := obj.AtKey("protocol").GetString()
					if err != nil {
						return err
					}
					if protocol == "" {
						return err
					}
					add(key, libkb.MakeURI(protocol, val), score)
				}

			} else {
				val, err := components.AtKey(key).AtKey("val").GetString()
				if err != nil {
					return err
				}
				score, err := components.AtKey(key).AtKey("score").GetFloat()
				if err != nil {
					return err
				}
				add(key, val, score)
			}
		}
		username, err := components.AtKey("username").AtKey("val").GetString()
		if err != nil {
			return err
		}
		uidString, err := completion.AtKey("uid").GetString()
		if err != nil {
			return err
		}
		uid, err := libkb.UIDFromHex(uidString)
		if err != nil {
			return err
		}
		totalScore, err := completion.AtKey("total_score").GetFloat()
		if err != nil {
			return err
		}
		e.results = append(e.results, keybase1.SearchResult{
			Uid:        uid,
			Username:   username,
			Components: searchComponents,
			Score:      totalScore,
		})
	}
	return nil
}