summaryrefslogtreecommitdiff
path: root/digitalocean/listKeys.go
blob: 000a66d796549680830b54d54621c223bcd57790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package digitalocean

import (
	"context"
	"fmt"

	"golang.org/x/oauth2"

	"github.com/digitalocean/godo"
)

func GetSSHKeyID(token, name string) (string, error) {
	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
	oauthClient := oauth2.NewClient(context.Background(), tokenSource)
	client := godo.NewClient(oauthClient)

	// List all keys.
	keys, _, err := client.Keys.List(context.Background(), &godo.ListOptions{})
	if err != nil {
		return "", err
	}

	// Find the key by name.
	for _, key := range keys {
		if key.Name == name {
			return key.Fingerprint, nil
		}
	}

	return "", fmt.Errorf("SSH Key not found")
}