97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var ErrVaultNotInitiated = errors.New("vault is not initiated set either vault-passphrase or vault-passphrase-file")
|
|
|
|
func VaultCommand(cmder VaultCommander) *cobra.Command {
|
|
var initCfg VaultInitConfig
|
|
|
|
vaultCmd := &cobra.Command{
|
|
Use: "vault",
|
|
Short: "Interact with the buildr vault",
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
}
|
|
|
|
initVaultCmd := &cobra.Command{
|
|
Use: "init",
|
|
Short: "Initialize vault - create an empty vault and a key file",
|
|
Long: `Creates an empty vault file and bootstraps a random passphrase
|
|
which will be written either to the configured --vault-passphrase-file or to the default .buildr/.vaultpw file`,
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
return cmder.Init(cmd.Context(), initCfg)
|
|
},
|
|
}
|
|
|
|
getVaultCmd := &cobra.Command{
|
|
Use: "get",
|
|
Short: "Get value from vault",
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return cmder.Get(cmd.Context(), args[0], os.Stdout)
|
|
},
|
|
}
|
|
|
|
listVaultCmd := &cobra.Command{
|
|
Use: "list",
|
|
Short: "List all vault entries - no decrypted values",
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
return cmder.List(cmd.Context(), os.Stdout)
|
|
},
|
|
}
|
|
|
|
const argsWithVaultValue = 2
|
|
setVaultCmd := &cobra.Command{
|
|
Use: "set",
|
|
Short: "Set a vault value",
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
Args: cobra.RangeArgs(1, argsWithVaultValue),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if len(args) == 1 {
|
|
inBuf := bytes.NewBuffer(nil)
|
|
if _, err := io.Copy(inBuf, os.Stdin); err != nil && !errors.Is(err, io.EOF) {
|
|
return err
|
|
}
|
|
|
|
return cmder.Set(cmd.Context(), args[0], inBuf.Bytes())
|
|
}
|
|
|
|
return cmder.Set(cmd.Context(), args[0], []byte(args[1]))
|
|
},
|
|
}
|
|
|
|
rmVaultCmd := &cobra.Command{
|
|
Use: "rm",
|
|
Short: "Remove value from vault",
|
|
Aliases: []string{"del"},
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return cmder.Remove(cmd.Context(), args[0])
|
|
},
|
|
}
|
|
initVaultCmd.Flags().AddGoFlagSet(initCfg.Flags())
|
|
|
|
vaultCmd.AddCommand(initVaultCmd, listVaultCmd, getVaultCmd, setVaultCmd, rmVaultCmd)
|
|
|
|
return vaultCmd
|
|
}
|