-
Notifications
You must be signed in to change notification settings - Fork 16
/
output.go
35 lines (29 loc) · 833 Bytes
/
output.go
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
32
33
34
35
package main
import (
"fmt"
"log"
"os"
"strings"
)
func PrintKeys(outputPath *string, keys map[string][]string) {
fd := os.Stdout
// Open output file if necessary
if outputPath != nil {
var err error
fd, err = os.OpenFile(*outputPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatal(fmt.Sprintf("Failed to open %s\n", *outputPath))
}
defer fd.Close()
}
// header, written once
fmt.Fprintf(fd, "\n########################################################\n")
fmt.Fprintf(fd, "# Generated by https://github.com/samber/sync-ssh-keys #\n")
fmt.Fprintf(fd, "########################################################\n\n")
for s, sshKeys := range keys {
if len(keys) > 0 {
fmt.Fprintf(fd, "#\n# %s\n#\n\n%s\n", s, strings.Join(sshKeys, "\n\n"))
}
}
fmt.Fprintf(fd, "\n# END\n")
}