-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add "wrench secret list|delete|upsert" commands
Signed-off-by: Stephen Gutekanst <[email protected]>
- Loading branch information
Showing
5 changed files
with
225 additions
and
1 deletion.
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,52 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/hexops/cmder" | ||
) | ||
|
||
// secretCommands contains all registered 'wrench secret' subcommands. | ||
var secretCommands cmder.Commander | ||
|
||
var ( | ||
secretFlagSet = flag.NewFlagSet("secret", flag.ExitOnError) | ||
secretConfigFile = secretFlagSet.String("config", defaultConfigFilePath(), "Path to TOML configuration file (see config.go)") | ||
) | ||
|
||
func init() { | ||
const usage = `wrench secret: manage wrench as a secret | ||
Usage: | ||
wrench secret [-config=config.toml] <command> [arguments] | ||
The commands are: | ||
list list all secrets | ||
delete delete a secret | ||
upsert create or update a secret | ||
Use "wrench secret <command> -h" for more information about a command. | ||
` | ||
|
||
usageFunc := func() { | ||
fmt.Printf("%s", usage) | ||
} | ||
secretFlagSet.Usage = usageFunc | ||
|
||
// Handles calls to our subcommand. | ||
handler := func(args []string) error { | ||
_ = secretFlagSet.Parse(args) | ||
secretCommands.Run(secretFlagSet, "wrench secret", usage, args) | ||
return nil | ||
} | ||
|
||
// Register the command. | ||
commands = append(commands, &cmder.Command{ | ||
FlagSet: secretFlagSet, | ||
Handler: handler, | ||
UsageFunc: usageFunc, | ||
}) | ||
} |
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 main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/hexops/cmder" | ||
"github.com/hexops/wrench/internal/errors" | ||
"github.com/hexops/wrench/internal/wrench" | ||
"github.com/hexops/wrench/internal/wrench/api" | ||
) | ||
|
||
func init() { | ||
const usage = ` | ||
Examples: | ||
Delete a secrets: | ||
$ wrench secret delete [id] | ||
` | ||
|
||
// Parse flags for our subcommand. | ||
flagSet := flag.NewFlagSet("delete", flag.ExitOnError) | ||
|
||
// Handles calls to our subcommand. | ||
handler := func(args []string) error { | ||
_ = flagSet.Parse(args) | ||
if len(args) != 1 { | ||
return &cmder.UsageError{Err: errors.New("expected [id] argument")} | ||
} | ||
|
||
ctx := context.Background() | ||
client, err := wrench.Client(*secretConfigFile) | ||
if err != nil { | ||
return errors.Wrap(err, "Client") | ||
} | ||
_, err = client.SecretsDelete(ctx, &api.SecretsDeleteRequest{ID: args[0]}) | ||
if err != nil { | ||
return errors.Wrap(err, "SecretsDelete") | ||
} | ||
return nil | ||
} | ||
|
||
// Register the command. | ||
secretCommands = append(secretCommands, &cmder.Command{ | ||
FlagSet: flagSet, | ||
Handler: handler, | ||
UsageFunc: func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'wrench secret %s':\n", flagSet.Name()) | ||
flagSet.PrintDefaults() | ||
fmt.Printf("%s", usage) | ||
}, | ||
}) | ||
} |
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 main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/hexops/cmder" | ||
"github.com/hexops/wrench/internal/errors" | ||
"github.com/hexops/wrench/internal/wrench" | ||
"github.com/hexops/wrench/internal/wrench/api" | ||
) | ||
|
||
func init() { | ||
const usage = ` | ||
Examples: | ||
List secrets: | ||
$ wrench secret list | ||
` | ||
|
||
// Parse flags for our subcommand. | ||
flagSet := flag.NewFlagSet("list", flag.ExitOnError) | ||
|
||
// Handles calls to our subcommand. | ||
handler := func(args []string) error { | ||
_ = flagSet.Parse(args) | ||
|
||
ctx := context.Background() | ||
client, err := wrench.Client(*secretConfigFile) | ||
if err != nil { | ||
return errors.Wrap(err, "Client") | ||
} | ||
resp, err := client.SecretsList(ctx, &api.SecretsListRequest{}) | ||
if err != nil { | ||
return errors.Wrap(err, "SecretsList") | ||
} | ||
for _, secretID := range resp.IDs { | ||
fmt.Println(secretID) | ||
} | ||
return nil | ||
} | ||
|
||
// Register the command. | ||
secretCommands = append(secretCommands, &cmder.Command{ | ||
FlagSet: flagSet, | ||
Handler: handler, | ||
UsageFunc: func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'wrench secret %s':\n", flagSet.Name()) | ||
flagSet.PrintDefaults() | ||
fmt.Printf("%s", usage) | ||
}, | ||
}) | ||
} |
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,59 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/hexops/cmder" | ||
"github.com/hexops/wrench/internal/errors" | ||
"github.com/hexops/wrench/internal/wrench" | ||
"github.com/hexops/wrench/internal/wrench/api" | ||
) | ||
|
||
func init() { | ||
const usage = ` | ||
Examples: | ||
Upsert a secrets: | ||
$ wrench secret upsert [id] [value] | ||
` | ||
|
||
// Parse flags for our subcommand. | ||
flagSet := flag.NewFlagSet("upsert", flag.ExitOnError) | ||
|
||
// Handles calls to our subcommand. | ||
handler := func(args []string) error { | ||
_ = flagSet.Parse(args) | ||
if len(args) != 2 { | ||
return &cmder.UsageError{Err: errors.New("expected [id] [value] arguments")} | ||
} | ||
|
||
ctx := context.Background() | ||
client, err := wrench.Client(*secretConfigFile) | ||
if err != nil { | ||
return errors.Wrap(err, "Client") | ||
} | ||
_, err = client.SecretsUpsert(ctx, &api.SecretsUpsertRequest{ | ||
ID: args[0], | ||
Value: args[1], | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "SecretsUpsert") | ||
} | ||
return nil | ||
} | ||
|
||
// Register the command. | ||
secretCommands = append(secretCommands, &cmder.Command{ | ||
FlagSet: flagSet, | ||
Handler: handler, | ||
UsageFunc: func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'wrench secret %s':\n", flagSet.Name()) | ||
flagSet.PrintDefaults() | ||
fmt.Printf("%s", usage) | ||
}, | ||
}) | ||
} |