Example #1
0
func CmdAdd(name, path string, ik IKeys, id deploykeys.IDeployKeys) error {
	if strings.ContainsAny(name, config.InvalidChars) {
		return fmt.Errorf("Invalid key name. Names must not contain the following characters: %s", config.InvalidChars)
	}
	fullPath, err := homedir.Expand(path)
	if err != nil {
		return err
	}
	keyBytes, err := ioutil.ReadFile(fullPath)
	if err != nil {
		return err
	}
	k, err := id.ParsePublicKey(keyBytes)
	if err != nil {
		return err
	}
	key := ssh.MarshalAuthorizedKey(k)
	if err != nil {
		return err
	}
	err = ik.Add(name, string(key))
	if err != nil {
		return err
	}
	logrus.Printf("Key '%s' added to your account.", name)
	logrus.Println("If you use an ssh-agent, make sure you add this key to your ssh-agent in order to push code")
	return nil
}
Example #2
0
func CmdList(ik IKeys, id deploykeys.IDeployKeys) error {
	keys, err := ik.List()
	if err != nil {
		return err
	}

	if keys == nil || len(*keys) == 0 {
		logrus.Println("No keys found")
		return nil
	}

	invalidKeys := map[string]string{}

	data := [][]string{{"NAME", "FINGERPRINT"}}
	for _, key := range *keys {
		s, err := id.ParsePublicKey([]byte(key.Key))
		if err != nil {
			invalidKeys[key.Name] = err.Error()
			continue
		}
		h := sha256.New()
		h.Write(s.Marshal())
		fingerprint := base64.StdEncoding.EncodeToString(h.Sum(nil))
		data = append(data, []string{key.Name, fmt.Sprintf("SHA256:%s", strings.TrimRight(fingerprint, "="))})
	}

	table := tablewriter.NewWriter(logrus.StandardLogger().Out)
	table.SetBorder(false)
	table.SetRowLine(false)
	table.SetCenterSeparator("")
	table.SetColumnSeparator("")
	table.SetRowSeparator("")
	table.AppendBulk(data)
	table.Render()

	if len(invalidKeys) > 0 {
		logrus.Println("\nInvalid Keys:")
		for keyName, reason := range invalidKeys {
			logrus.Printf("%s: %s", keyName, reason)
		}
	}
	return nil
}