-
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.
* Add flags and docs to the main file * Add json output style
- Loading branch information
1 parent
67fe9e1
commit 8ba1121
Showing
5 changed files
with
201 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
*.so | ||
*.dylib | ||
|
||
# Ignore output json files | ||
*.json | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
|
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,37 @@ | ||
// # Display/JSON | ||
// | ||
// This package contains the code for the JSON display of the AWS accounts and | ||
// OUs. It uses the tree structure generated in the generation package to | ||
// create a JSON representation of the accounts and OUs. | ||
package json | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/CentricaDevOps/aws-organizations-visualiser/generation" | ||
) | ||
|
||
// Create is a function that takes in the tree structure and creates a JSON | ||
// representation of it. | ||
func Create(tree *generation.OU) ([]byte, error) { | ||
// Use the existing structure to create a JSON representation of the tree. | ||
return tree.ToJSON() | ||
} | ||
|
||
// OutputToFile is a function that takes in the json representaiton of the tree and | ||
// outputs it to the given file | ||
func OutputToFile(json []byte, filename string) error { | ||
// Open the file | ||
file, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
// Write the JSON to the file | ||
_, err = file.Write(json) | ||
if err != nil { | ||
return err | ||
} | ||
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
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 |
---|---|---|
@@ -1 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestSetupLoggingTrue tests the setupLogging function when the logging | ||
// parameter is true. | ||
func TestSetupLoggingTrue(t *testing.T) { | ||
setupLogging("true") | ||
require.NotNil(t, logs, "Expected logs to be initialised") | ||
require.Equal(t, true, logs.Enabled, "Expected logs to be enabled") | ||
} | ||
|
||
// TestSetupLoggingFalse tests the setupLogging function when the logging | ||
// parameter is false. | ||
func TestSetupLoggingFalse(t *testing.T) { | ||
setupLogging("false") | ||
require.NotNil(t, logs, "Expected logs to be initialised") | ||
require.Equal(t, false, logs.Enabled, "Expected logs to be disabled") | ||
} | ||
|
||
// TestSetupLoggingInvalid tests the setupLogging function when the logging | ||
// parameter is invalid. | ||
func TestSetupLoggingInvalid(t *testing.T) { | ||
setupLogging("invalid") | ||
require.NotNil(t, logs, "Expected logs to be initialised") | ||
require.Equal(t, false, logs.Enabled, "Expected logs to be disabled") | ||
} | ||
|
||
// TestSetupLoggingEmpty tests the setupLogging function when the logging | ||
// parameter is empty. | ||
func TestSetupLoggingEmpty(t *testing.T) { | ||
setupLogging("") | ||
require.NotNil(t, logs, "Expected logs to be initialised") | ||
require.Equal(t, false, logs.Enabled, "Expected logs to be disabled") | ||
} | ||
|
||
// TestPrintlnLogging tests the Println function of the Logs struct. | ||
func TestPrintlnLogging(t *testing.T) { | ||
setupLogging("true") | ||
output1 := captureOutput(func() { | ||
logs.Println("Test") | ||
}) | ||
require.Equal(t, "Test\n", output1, "Expected output to be 'Test\n'") | ||
|
||
setupLogging("false") | ||
output2 := captureOutput(func() { | ||
logs.Println("Test") | ||
}) | ||
require.Equal(t, "", output2, "Expected output to be ''") | ||
} | ||
|
||
// captureOutput is a helper function to capture the output of a function. | ||
// This is used to test the output of the display functions. | ||
func captureOutput(f func()) string { | ||
// Store the old stdout and replace it with a pipe. | ||
original := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
// Run the function. | ||
f() | ||
// Close the pipe and restore stdout. | ||
w.Close() | ||
os.Stdout = original | ||
|
||
// Read the output from the pipe. | ||
var buf bytes.Buffer | ||
_, err := io.Copy(&buf, r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// Return the output. | ||
return buf.String() | ||
} |