Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
Implemented better output management
Browse files Browse the repository at this point in the history
Previously we were piping the output to `~/.ssh/authorized_keys`
but that's not good because if there happen an error, we mess up
our authorized_keys file.
Now we update target file only if we manage to fetch the keys.
  • Loading branch information
ernoaapa committed Nov 14, 2016
1 parent a290682 commit c11ce92
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 25 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
## Usage
```shell
fetch-ssh-keys <source name> <parameters>
fetch-ssh-keys <source name> <parameters> <output file>
```

For example fetch users public SSH keys from GitHub `my-lovely-team` team in `my-awesome-company` organization and output in SSH authorized_keys format
```shell
fetch-ssh-keys github --organization my-awesome-company --team my-lovely-team --token YOUR-TOKEN-HERE
fetch-ssh-keys github --organization my-awesome-company --team my-lovely-team --token YOUR-TOKEN-HERE ./the-keys
```

Tool can be used for example to automatically update `.ssh/authorized_keys` file by adding the script to cron job.
Tool can be used for example to automatically update `.ssh/authorized_keys` file by giving path to `.ssh/authorized_keys` as last argument and adding the script to cron job.

## Installation
- Download binary from [releases](https://github.com/ernoaapa/fetch-ssh-keys/releases)
- Give execution rights (`chmod +x fetch-ssh-keys`) and add it into your $PATH

### Configuration
| Parameter | Required | Description |
|----------------|----------|-----------------------------------------------------------------------------------------------------------|
| --output | Yes | Output format. Only ssh authorized_keys format supported for now |
| Parameter | Required | Description |
|----------------|-------------------|-----------------------------------------------------------------------------------------------------------|
| --format | No (default ssh) | Output format. Only ssh authorized_keys format supported for now |

#### GitHub
| Parameter | Required | Description |
Expand All @@ -38,7 +38,7 @@ Tool can be used for example to automatically update `.ssh/authorized_keys` file
go get ./...
```

### Run stastd-exec
### Run
```shell
go run main.go github --output ssh
```
13 changes: 13 additions & 0 deletions format/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package format

import log "github.com/Sirupsen/logrus"

// Build builds output in given formatName format
func Build(formatName string, keysByUsername map[string][]string) string {
switch formatName {
case "ssh":
return ssh(keysByUsername)
}
log.Fatalf("Invalid output format name: %s", formatName)
return ""
}
2 changes: 1 addition & 1 deletion output/ssh.go → format/ssh.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package output
package format

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion output/ssh_test.go → format/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package output
package format

import (
"testing"
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
app.Usage = "Fetch user public SSH keys"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "output, o",
Name: "format, f",
Usage: "Output format. One of: ssh",
Value: "ssh",
},
Expand Down Expand Up @@ -67,8 +67,7 @@ func main() {
log.Fatalln("Failed to fetch keys", err)
}

println(output.Build(c.GlobalString("output"), keys))
return nil
return output.Write(c.GlobalString("format"), c.Args().Get(0), keys)
},
},
}
Expand Down
13 changes: 0 additions & 13 deletions output/builder.go

This file was deleted.

24 changes: 24 additions & 0 deletions output/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package output

import (
"io/ioutil"
"os"
)

// FileWriter writes the output to file
type FileWriter struct {
targetFile string
fileMode os.FileMode
}

// NewFileWriter creates new FileWriter what writes to targetFile
func NewFileWriter(targetFile string) *FileWriter {
return &FileWriter{
targetFile: targetFile,
fileMode: 0600,
}
}

func (w *FileWriter) write(output string) error {
return ioutil.WriteFile(w.targetFile, []byte(output), w.fileMode)
}
27 changes: 27 additions & 0 deletions output/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package output

import (
"io/ioutil"
"testing"

log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestFile(t *testing.T) {
log.SetLevel(log.DebugLevel)
log.Println("hwllllwe")

file, createErr := ioutil.TempFile("", "example")
assert.NoError(t, createErr, "Unable to create temp file")

writer := NewFileWriter(file.Name())

writer.write("foobar")
writer.write("foobar-second-time")

fileBytes, readErr := ioutil.ReadFile(file.Name())
assert.NoError(t, readErr, "Unable to read temp file")

assert.Equal(t, "foobar-second-time", string(fileBytes), "FileWriter didnt wrote expected output to file")
}
11 changes: 11 additions & 0 deletions output/stdout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package output

import "fmt"

// StdoutWriter just writes the output to stdout
type StdoutWriter struct{}

func (w *StdoutWriter) write(output string) error {
fmt.Println(output)
return nil
}
14 changes: 14 additions & 0 deletions output/stdout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package output

import (
"testing"

log "github.com/Sirupsen/logrus"
)

func TestStdout(t *testing.T) {
log.SetLevel(log.DebugLevel)

writer := &StdoutWriter{}
writer.write("whoop")
}
23 changes: 23 additions & 0 deletions output/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package output

import "github.com/ernoaapa/fetch-ssh-keys/format"

// Writer is interface for all output writers
type Writer interface {
write(output string) error
}

// Write writes keys to given outputName in given formatName
func Write(formatName, target string, keysByUsername map[string][]string) error {
writer := getWriter(target)
return writer.write(format.Build(formatName, keysByUsername))
}

func getWriter(target string) Writer {
switch target {
case "":
return &StdoutWriter{}
default:
return NewFileWriter(target)
}
}

0 comments on commit c11ce92

Please sign in to comment.