Skip to content

Commit 88a1a31

Browse files
authored
Merge pull request ernoaapa#15 from arnested/comment
Add comment to top and bottom of generated content
2 parents 1b9e810 + 240b4ef commit 88a1a31

File tree

8 files changed

+41
-11
lines changed

8 files changed

+41
-11
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: go
22

33
go:
4-
- 1.9
4+
- '1.10'
55

66
before_install:
77
- go get github.com/mitchellh/gox

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Tool can be used for example to automatically update `.ssh/authorized_keys` file
3232
|----------------|-------------------|-----------------------------------------------------------------------------------------------------------|
3333
| --format | No (default ssh) | Output format. Only ssh authorized_keys format supported for now |
3434
| --file-mode | No (default 0600) | File permissions when writing to a file |
35+
| --comment | No (default none) | Include COMMENT at top and bottom |
3536

3637
#### GitHub
3738
| Parameter | Description |

fetch/github.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func fetchUsers(client *github.Client, organizationName string, params GithubFet
7878
return users, err
7979
}
8080

81-
func resolveTeamID(client *github.Client, organizationName, teamName string) (int, error) {
81+
func resolveTeamID(client *github.Client, organizationName, teamName string) (int64, error) {
8282
ctx := context.Background()
8383

8484
teams, _, err := client.Organizations.ListTeams(ctx, organizationName, &github.ListOptions{})

format/builder.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package format
33
import log "github.com/Sirupsen/logrus"
44

55
// Build builds output in given formatName format
6-
func Build(formatName string, keysByUsername map[string][]string) string {
6+
func Build(formatName string, keysByUsername map[string][]string, comment string) string {
77
switch formatName {
88
case "ssh":
9-
return ssh(keysByUsername)
9+
return ssh(keysByUsername, comment)
1010
}
1111
log.Fatalf("Invalid output format name: %s", formatName)
1212
return ""

format/ssh.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@ import (
66
)
77

88
// ssh produces output in .ssh/authorized_keys compatible format
9-
func ssh(keysByUsername map[string][]string) string {
9+
func ssh(keysByUsername map[string][]string, comment string) string {
1010
stringBuffer := bytes.NewBufferString("")
1111

12+
if len(comment) > 0 {
13+
stringBuffer.WriteString(fmt.Sprintf("# %s\n", comment))
14+
}
15+
1216
for username, keys := range keysByUsername {
1317
for _, key := range keys {
1418
stringBuffer.WriteString(fmt.Sprintf("%s %s\n", key, username))
1519
}
1620
}
21+
22+
if len(comment) > 0 {
23+
stringBuffer.WriteString(fmt.Sprintf("# %s\n", comment))
24+
}
25+
1726
return stringBuffer.String()
1827
}

format/ssh_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
func TestSsh(t *testing.T) {
11+
func TestSshWithoutComment(t *testing.T) {
1212
log.SetLevel(log.DebugLevel)
1313

1414
keys := map[string][]string{
@@ -17,7 +17,21 @@ func TestSsh(t *testing.T) {
1717
},
1818
}
1919

20-
result := ssh(keys)
20+
result := ssh(keys, "")
2121

2222
assert.Equal(t, "ssh-rsa AAAAB3NzsshPublicKeyBlah ernoaapa\n", result, "Returned invalid ssh output")
2323
}
24+
25+
func TestSshWithComment(t *testing.T) {
26+
log.SetLevel(log.DebugLevel)
27+
28+
keys := map[string][]string{
29+
"ernoaapa": {
30+
"ssh-rsa AAAAB3NzsshPublicKeyBlah",
31+
},
32+
}
33+
34+
result := ssh(keys, "Generated file")
35+
36+
assert.Equal(t, "# Generated file\nssh-rsa AAAAB3NzsshPublicKeyBlah ernoaapa\n# Generated file\n", result, "Returned invalid ssh output")
37+
}

main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func main() {
3333
Usage: "File permissions for file",
3434
Value: "0600",
3535
},
36+
cli.StringFlag{
37+
Name: "comment",
38+
Usage: "Include `COMMENT` at top and bottom",
39+
},
3640
}
3741
app.Version = VersionString
3842
app.Commands = []cli.Command{
@@ -77,6 +81,7 @@ func main() {
7781
target = c.Args().Get(0)
7882
fileMode = os.FileMode(c.GlobalInt("file-mode"))
7983
format = c.GlobalString("format")
84+
comment = c.GlobalString("comment")
8085

8186
err error
8287
)
@@ -103,7 +108,7 @@ func main() {
103108
}
104109
}
105110

106-
return output.Write(format, target, fileMode, utils.MergeKeys(orgKeys, userKeys))
111+
return output.Write(format, target, fileMode, utils.MergeKeys(orgKeys, userKeys), comment)
107112
},
108113
},
109114
}

output/writer.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package output
22

33
import (
4-
"github.com/ernoaapa/fetch-ssh-keys/format"
54
"os"
5+
6+
"github.com/ernoaapa/fetch-ssh-keys/format"
67
)
78

89
// Writer is interface for all output writers
@@ -11,9 +12,9 @@ type Writer interface {
1112
}
1213

1314
// Write writes keys to given outputName in given formatName
14-
func Write(formatName, target string, perm os.FileMode, keysByUsername map[string][]string) error {
15+
func Write(formatName, target string, perm os.FileMode, keysByUsername map[string][]string, comment string) error {
1516
writer := getWriter(target, perm)
16-
return writer.write(format.Build(formatName, keysByUsername))
17+
return writer.write(format.Build(formatName, keysByUsername, comment))
1718
}
1819

1920
func getWriter(target string, perm os.FileMode) Writer {

0 commit comments

Comments
 (0)