Skip to content

Commit

Permalink
add "wrench secret list|delete|upsert" commands
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
emidoots committed Dec 28, 2022
1 parent 4dc8058 commit aa37020
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 1 deletion.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ Usage:
The commands are:
service manage the wrench service (also 'wrench svc')
runners list registered runners
script execute a script built-in to wrench
runners (remote) list registered runners
secret (remote) manage secrets
Use "wrench <command> -h" for more information about a command.
`
Expand Down
52 changes: 52 additions & 0 deletions secret.go
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,
})
}
56 changes: 56 additions & 0 deletions secret_delete.go
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)
},
})
}
56 changes: 56 additions & 0 deletions secret_list.go
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)
},
})
}
59 changes: 59 additions & 0 deletions secret_upsert.go
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)
},
})
}

0 comments on commit aa37020

Please sign in to comment.