-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to print curl commands from CLI (#6113)
- Loading branch information
Showing
7 changed files
with
218 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,69 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
retryablehttp "github.com/hashicorp/go-retryablehttp" | ||
) | ||
|
||
const ( | ||
ErrOutputStringRequest = "output a string, please" | ||
) | ||
|
||
var ( | ||
LastOutputStringError *OutputStringError | ||
) | ||
|
||
type OutputStringError struct { | ||
*retryablehttp.Request | ||
parsingError error | ||
parsedCurlString string | ||
} | ||
|
||
func (d *OutputStringError) Error() string { | ||
if d.parsedCurlString == "" { | ||
d.parseRequest() | ||
if d.parsingError != nil { | ||
return d.parsingError.Error() | ||
} | ||
} | ||
|
||
return ErrOutputStringRequest | ||
} | ||
|
||
func (d *OutputStringError) parseRequest() { | ||
body, err := d.Request.BodyBytes() | ||
if err != nil { | ||
d.parsingError = err | ||
return | ||
} | ||
|
||
// Build cURL string | ||
d.parsedCurlString = "curl " | ||
d.parsedCurlString = fmt.Sprintf("%s-X %s ", d.parsedCurlString, d.Request.Method) | ||
for k, v := range d.Request.Header { | ||
for _, h := range v { | ||
if strings.ToLower(k) == "x-vault-token" { | ||
h = `$(vault print token)` | ||
} | ||
d.parsedCurlString = fmt.Sprintf("%s-H \"%s: %s\" ", d.parsedCurlString, k, h) | ||
} | ||
} | ||
|
||
if len(body) > 0 { | ||
// We need to escape single quotes since that's what we're using to | ||
// quote the body | ||
escapedBody := strings.Replace(string(body), "'", "'\"'\"'", -1) | ||
d.parsedCurlString = fmt.Sprintf("%s-d '%s' ", d.parsedCurlString, escapedBody) | ||
} | ||
|
||
d.parsedCurlString = fmt.Sprintf("%s%s", d.parsedCurlString, d.Request.URL.String()) | ||
} | ||
|
||
func (d *OutputStringError) CurlString() string { | ||
if d.parsedCurlString == "" { | ||
d.parseRequest() | ||
} | ||
return d.parsedCurlString | ||
} |
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
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,56 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mitchellh/cli" | ||
"github.com/posener/complete" | ||
) | ||
|
||
var _ cli.Command = (*PrintTokenCommand)(nil) | ||
var _ cli.CommandAutocomplete = (*PrintTokenCommand)(nil) | ||
|
||
type PrintTokenCommand struct { | ||
*BaseCommand | ||
} | ||
|
||
func (c *PrintTokenCommand) Synopsis() string { | ||
return "Prints the contents of a policy" | ||
} | ||
|
||
func (c *PrintTokenCommand) Help() string { | ||
helpText := ` | ||
Usage: vault print token | ||
Prints the value of the Vault token that will be used for commands, after | ||
taking into account the configured token-helper and the environment. | ||
$ vault print token | ||
` + c.Flags().Help() | ||
|
||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *PrintTokenCommand) Flags() *FlagSets { | ||
return nil | ||
} | ||
|
||
func (c *PrintTokenCommand) AutocompleteArgs() complete.Predictor { | ||
return nil | ||
} | ||
|
||
func (c *PrintTokenCommand) AutocompleteFlags() complete.Flags { | ||
return nil | ||
} | ||
|
||
func (c *PrintTokenCommand) Run(args []string) int { | ||
client, err := c.Client() | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 2 | ||
} | ||
|
||
c.UI.Output(client.Token()) | ||
return 0 | ||
} |