-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #724 from HuKeping/cli
Feature: Introduce some flags on notary verify
- Loading branch information
Showing
3 changed files
with
116 additions
and
7 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,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
// getPayload is a helper function to get the content used to be verified | ||
// either from an existing file or STDIN. | ||
func getPayload(t *tufCommander) ([]byte, error) { | ||
|
||
// Reads from the given file | ||
if t.input != "" { | ||
// Please note that ReadFile will cut off the size if it was over 1e9. | ||
// Thus, if the size of the file exceeds 1GB, the over part will not be | ||
// loaded into the buffer. | ||
payload, err := ioutil.ReadFile(t.input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return payload, nil | ||
} | ||
|
||
// Reads all of the data on STDIN | ||
payload, err := ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error reading content from STDIN: %v", err) | ||
} | ||
return payload, nil | ||
} | ||
|
||
// feedback is a helper function to print the payload to a file or STDOUT or keep quiet | ||
// due to the value of flag "quiet" and "output". | ||
func feedback(t *tufCommander, payload []byte) error { | ||
// We only get here when everything goes well, since the flag "quiet" was | ||
// provided, we output nothing but just return. | ||
if t.quiet { | ||
return nil | ||
} | ||
|
||
// Flag "quiet" was not "true", that's why we get here. | ||
if t.output != "" { | ||
return ioutil.WriteFile(t.output, payload, 0644) | ||
} | ||
|
||
os.Stdout.Write(payload) | ||
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,54 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetPayload(t *testing.T) { | ||
tempDir, err := ioutil.TempDir("", "test-get-payload") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tempDir) | ||
|
||
file, err := os.Create(filepath.Join(tempDir, "content.txt")) | ||
require.NoError(t, err) | ||
|
||
fmt.Fprintf(file, "Release date: June 10, 2016 - Director: Duncan Jones") | ||
file.Close() | ||
|
||
commander := &tufCommander{ | ||
input: file.Name(), | ||
} | ||
|
||
payload, err := getPayload(commander) | ||
require.NoError(t, err) | ||
require.Equal(t, "Release date: June 10, 2016 - Director: Duncan Jones", string(payload)) | ||
} | ||
|
||
func TestFeedback(t *testing.T) { | ||
tempDir, err := ioutil.TempDir("", "test-feedback") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tempDir) | ||
|
||
file, err := os.Create(filepath.Join(tempDir, "content.txt")) | ||
require.NoError(t, err) | ||
|
||
// Expect it to print nothing since "quiet" takes priority. | ||
commander := &tufCommander{ | ||
output: file.Name(), | ||
quiet: true, | ||
} | ||
|
||
payload := []byte("Release date: June 10, 2016 - Director: Duncan Jones") | ||
err = feedback(commander, payload) | ||
require.NoError(t, err) | ||
|
||
content, err := ioutil.ReadFile(file.Name()) | ||
require.NoError(t, err) | ||
require.Equal(t, "", string(content)) | ||
} |