-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(creds): Add edit credentials command (#921)
* feat(creds): Add edit credentials command * ensure EDITOR env var is checked * Edit the entire credential set instead of picking each one * fix spaces in EDITOR not opening editor correctly, e.g. using code.exe --wait * add test, change editor to use context.CommandBuilder so it works * add test for windows editor path * Changes from feedback: - shift editor into separate package, get it through editor.New() - embed the context struct into editor directly - use context.FileSystem instead of ioutil/os calls - fix editor to split on spaces, add comment on why and examples - remove "tempStrategy" stuff, edit the actual credentials directly - shift windows-specific check into conditional compilation with editor_windows.go, editor_nix.go etc * fix parsing of editor command, pass this to the shell to do it for us * fix tests * remove restriction on editing an empty credential set (it's possible) * use yaml extension for temp credential file for editor syntax, colors etc * minor variable naming, use Wrap instead of Wrapf, and unnecessary byte casting * minor tweaks to comment * validate source keys are correct after editing credentials * use multierrors for validation errors * Adding tests for CredentialStorage.Validate * show some slightly friendlier errors
- Loading branch information
Showing
11 changed files
with
318 additions
and
8 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
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,33 @@ | ||
--- | ||
title: "porter credentials edit" | ||
slug: porter_credentials_edit | ||
url: /cli/porter_credentials_edit/ | ||
--- | ||
## porter credentials edit | ||
|
||
Edit Credential | ||
|
||
### Synopsis | ||
|
||
Edit a named credential set. | ||
|
||
``` | ||
porter credentials edit [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for edit | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
--debug Enable debug logging | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [porter credentials](/cli/porter_credentials/) - Credentials commands | ||
|
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package credentials | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cnabio/cnab-go/credentials" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCredentialStorage_Validate_GoodSources(t *testing.T) { | ||
s := CredentialStorage{} | ||
testCreds := credentials.CredentialSet{ | ||
Credentials: []credentials.CredentialStrategy{ | ||
{ | ||
Source: credentials.Source{ | ||
Key: "env", | ||
Value: "SOME_ENV", | ||
}, | ||
}, | ||
{ | ||
Source: credentials.Source{ | ||
Key: "value", | ||
Value: "somevalue", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
err := s.Validate(testCreds) | ||
require.NoError(t, err, "Validate did not return errors") | ||
} | ||
|
||
func TestCredentialStorage_Validate_BadSources(t *testing.T) { | ||
s := CredentialStorage{} | ||
testCreds := credentials.CredentialSet{ | ||
Credentials: []credentials.CredentialStrategy{ | ||
{ | ||
Source: credentials.Source{ | ||
Key: "wrongthing", | ||
Value: "SOME_ENV", | ||
}, | ||
}, | ||
{ | ||
Source: credentials.Source{ | ||
Key: "anotherwrongthing", | ||
Value: "somevalue", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
err := s.Validate(testCreds) | ||
require.Error(t, err, "Validate returned errors") | ||
} |
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,88 @@ | ||
package editor | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"get.porter.sh/porter/pkg/context" | ||
) | ||
|
||
// Editor displays content to a user using an external text editor, like vi or notepad. | ||
// The content is captured and returned. | ||
// | ||
// The `EDITOR` environment variable is checked to find an editor. | ||
// Failing that, use some sensible default depending on the operating system. | ||
// | ||
// This is useful for editing things like configuration files, especially those | ||
// that might be stored on a remote server. For example: the content could be retrieved | ||
// from the remote store, edited locally, then saved back. | ||
type Editor struct { | ||
*context.Context | ||
contents []byte | ||
tempFilename string | ||
} | ||
|
||
// New returns a new Editor with the temp filename and contents provided. | ||
func New(context *context.Context, tempFilename string, contents []byte) *Editor { | ||
return &Editor{ | ||
Context: context, | ||
tempFilename: tempFilename, | ||
contents: contents, | ||
} | ||
} | ||
|
||
func editorArgs(filename string) []string { | ||
shell := defaultShell | ||
if os.Getenv("SHELL") != "" { | ||
shell = os.Getenv("SHELL") | ||
} | ||
editor := defaultEditor | ||
if os.Getenv("EDITOR") != "" { | ||
editor = os.Getenv("EDITOR") | ||
} | ||
|
||
// Example of what will be run: | ||
// on *nix: sh -c "vi /tmp/test.txt" | ||
// on windows: cmd /C "C:\Program Files\Visual Studio Code\Code.exe --wait C:\somefile.txt" | ||
// | ||
// Pass the editor command to the shell so we don't have to parse the command ourselves. | ||
// Passing the editor command that could possibly have an argument (e.g. --wait for VSCode) to the | ||
// shell means we don't have to parse this ourselves, like splitting on spaces. | ||
return []string{shell, shellCommandFlag, fmt.Sprintf("%s %s", editor, filename)} | ||
} | ||
|
||
// Run opens the editor, displaying the contents through a temporary file. | ||
// The content is returned once the editor closes. | ||
func (e *Editor) Run() ([]byte, error) { | ||
tempFile, err := e.FileSystem.OpenFile(filepath.Join(os.TempDir(), e.tempFilename), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer e.FileSystem.Remove(tempFile.Name()) | ||
|
||
_, err = tempFile.Write(e.contents) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// close here without defer so cmd can grab the file | ||
tempFile.Close() | ||
|
||
args := editorArgs(tempFile.Name()) | ||
cmd := e.NewCommand(args[0], args[1:]...) | ||
cmd.Stdout = e.Out | ||
cmd.Stderr = e.Err | ||
cmd.Stdin = e.In | ||
err = cmd.Run() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
contents, err := e.FileSystem.ReadFile(tempFile.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return contents, 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,7 @@ | ||
// +build !windows | ||
|
||
package editor | ||
|
||
const defaultEditor = "vi" | ||
const defaultShell = "sh" | ||
const shellCommandFlag = "-c" |
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,7 @@ | ||
// +build windows | ||
|
||
package editor | ||
|
||
const defaultEditor = "notepad" | ||
const defaultShell = "cmd" | ||
const shellCommandFlag = "/C" |
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
Oops, something went wrong.