This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
forked from ernoaapa/fetch-ssh-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented better output management
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
Showing
11 changed files
with
123 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package output | ||
package format | ||
|
||
import ( | ||
"bytes" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package output | ||
package format | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |