Example #1
0
func (c *Tell) Run(args []string) int {

	var to, method string
	var timeout time.Duration

	flags := flag.NewFlagSet("tell", flag.ExitOnError)
	flags.StringVar(&to, "to", "", "URL of remote kite")
	flags.StringVar(&method, "method", "", "method to be called")
	flags.DurationVar(&timeout, "timeout", 4*time.Second, "timeout of tell method")
	flags.Parse(args)

	if to == "" || method == "" {
		c.Ui.Output(c.Help())
		return 1
	}

	key, err := kitekey.Read()
	if err != nil {
		c.Ui.Error(err.Error())
		return 1
	}

	remote := c.KiteClient.NewClient(to)
	remote.Auth = &kite.Auth{
		Type: "kiteKey",
		Key:  key,
	}

	if err = remote.Dial(); err != nil {
		c.Ui.Error(err.Error())
		return 1
	}

	// Convert args to []interface{} in order to pass it to Tell() method.
	methodArgs := flags.Args()
	params := make([]interface{}, len(methodArgs))
	for i, arg := range methodArgs {
		if number, err := strconv.Atoi(arg); err != nil {
			params[i] = arg
		} else {
			params[i] = number
		}
	}

	result, err := remote.TellWithTimeout(method, timeout, params...)
	if err != nil {
		c.Ui.Error(err.Error())
		return 1
	}

	if result == nil {
		c.Ui.Info("nil")
	} else {
		c.Ui.Info(string(result.Raw))
	}

	return 0
}
Example #2
0
func (c *Register) Run(args []string) int {
	var kontrolURL, username string
	var err error

	flags := flag.NewFlagSet("register", flag.ExitOnError)
	flags.StringVar(&kontrolURL, "to", defaultKontrolURL, "Kontrol URL")
	flags.StringVar(&username, "username", "", "Username")
	flags.Parse(args)

	// Open up a prompt
	if username == "" {
		username, err = c.Ui.Ask("Username:"******"" {
			c.Ui.Error("Username can not be empty.")
			return 1
		}
	}

	c.KiteClient.Config.Username = username

	if _, err := kitekey.Read(); err == nil {
		c.Ui.Info("Already registered. Registering again...")
	}

	kontrol := c.KiteClient.NewClient(kontrolURL)
	if err := kontrol.Dial(); err != nil {
		c.Ui.Error(err.Error())
		return 1
	}

	result, err := kontrol.TellWithTimeout("registerMachine", 5*time.Minute, username)
	if err != nil {
		c.Ui.Error(err.Error())
		return 1
	}

	if err := kitekey.Write(result.MustString()); err != nil {
		c.Ui.Error(err.Error())
		return 1
	}

	c.Ui.Info("Registered successfully")

	return 0
}