Skip to content

Commit 4c5556d

Browse files
committed
Merge pull request #148 from QuentinPerez/fix_130
fix 130 add infos
2 parents 94973ca + a854ae0 commit 4c5556d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/commands/info.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/scaleway/scaleway-cli/vendor/github.com/kardianos/osext"
1313

1414
"github.com/scaleway/scaleway-cli/pkg/config"
15+
"github.com/scaleway/scaleway-cli/pkg/utils"
1516
)
1617

1718
// InfoArgs are flags for the `RunInfo` function
@@ -41,5 +42,23 @@ func RunInfo(ctx CommandContext, args InfoArgs) error {
4142
fmt.Fprintf(ctx.Stdout, " Snapshots: %d\n", ctx.API.Cache.GetNbSnapshots())
4243
fmt.Fprintf(ctx.Stdout, " Volumes: %d\n", ctx.API.Cache.GetNbVolumes())
4344
fmt.Fprintf(ctx.Stdout, " Bootscripts: %d\n", ctx.API.Cache.GetNbBootscripts())
45+
user, err := ctx.API.GetUser()
46+
if err != nil {
47+
return fmt.Errorf("Unable to get your SSH Keys")
48+
} else {
49+
if len(user.SSHPublicKeys) == 0 {
50+
fmt.Fprintln(ctx.Stdout, "You have no ssh keys")
51+
} else {
52+
fmt.Fprintln(ctx.Stdout, "SSH Keys:")
53+
for id, key := range user.SSHPublicKeys {
54+
fingerprint, err := utils.SSHGetFingerprint(key.Key)
55+
if err != nil {
56+
return err
57+
} else {
58+
fmt.Fprintf(ctx.Stdout, " [%d] %s", id, fingerprint)
59+
}
60+
}
61+
}
62+
}
4463
return nil
4564
}

pkg/utils/utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14+
"io/ioutil"
1415
"net"
1516
"os"
1617
"os/exec"
@@ -211,3 +212,25 @@ However, you can access your serial using a web browser:
211212
}
212213
return nil
213214
}
215+
216+
func SSHGetFingerprint(key string) (string, error) {
217+
tmp, err := ioutil.TempFile("", ".tmp")
218+
if err != nil {
219+
return "", fmt.Errorf("Unable to create a tempory file: %v", err)
220+
}
221+
defer os.Remove(tmp.Name())
222+
buff := []byte(key)
223+
bytesWritten := 0
224+
for bytesWritten < len(buff) {
225+
nb, err := tmp.Write(buff[bytesWritten:])
226+
if err != nil {
227+
return "", fmt.Errorf("Unable to write: %v", err)
228+
}
229+
bytesWritten += nb
230+
}
231+
ret, err := exec.Command("ssh-keygen", "-l", "-f", tmp.Name()).Output()
232+
if err != nil {
233+
return "", fmt.Errorf("Unable to run ssh-keygen: %v", err)
234+
}
235+
return string(ret), nil
236+
}

0 commit comments

Comments
 (0)