goldwarden-vaultwarden-bitw.../cmd/ssh.go

96 lines
2.3 KiB
Go
Raw Normal View History

2023-07-17 03:23:26 +02:00
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/atotto/clipboard"
2023-07-17 03:23:26 +02:00
"github.com/quexten/goldwarden/ipc"
"github.com/spf13/cobra"
)
var sshCmd = &cobra.Command{
Use: "ssh",
Short: "Commands for managing SSH keys",
Long: `Commands for managing SSH keys.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
// runCmd represents the run command
var sshAddCmd = &cobra.Command{
Use: "add",
Short: "Runs a command with environment variables from your vault",
Long: `Runs a command with environment variables from your vault.
The variables are stored as a secure note. Consult the documentation for more information.`,
Run: func(cmd *cobra.Command, args []string) {
2023-08-21 18:37:34 +02:00
loginIfRequired()
2023-07-17 03:23:26 +02:00
name, _ := cmd.Flags().GetString("name")
2023-07-18 22:07:40 +02:00
copyToClipboard, _ := cmd.Flags().GetBool("clipboard")
2023-07-17 03:23:26 +02:00
2023-08-21 13:52:06 +02:00
result, err := commandClient.SendToAgent(ipc.CreateSSHKeyRequest{
2023-07-17 03:23:26 +02:00
Name: name,
})
if err != nil {
println("Error: " + err.Error())
println("Is the daemon running?")
return
}
switch result.(type) {
case ipc.CreateSSHKeyResponse:
response := result.(ipc.CreateSSHKeyResponse)
fmt.Println(response.Digest)
2023-07-18 22:07:40 +02:00
if copyToClipboard {
clipboard.WriteAll(string(response.Digest))
}
break
2023-07-17 03:23:26 +02:00
case ipc.ActionResponse:
println("Error: " + result.(ipc.ActionResponse).Message)
return
}
},
}
var listSSHCmd = &cobra.Command{
Use: "list",
Short: "Lists all SSH keys in your vault",
Long: `Lists all SSH keys in your vault.`,
Run: func(cmd *cobra.Command, args []string) {
2023-08-21 18:37:34 +02:00
loginIfRequired()
2023-08-21 13:52:06 +02:00
result, err := commandClient.SendToAgent(ipc.GetSSHKeysRequest{})
2023-07-17 03:23:26 +02:00
if err != nil {
println("Error: " + err.Error())
println("Is the daemon running?")
return
}
switch result.(type) {
case ipc.GetSSHKeysResponse:
response := result.(ipc.GetSSHKeysResponse)
for _, key := range response.Keys {
fmt.Println(key)
}
break
case ipc.ActionResponse:
println("Error: " + result.(ipc.ActionResponse).Message)
return
}
},
}
func init() {
rootCmd.AddCommand(sshCmd)
sshCmd.AddCommand(sshAddCmd)
sshAddCmd.PersistentFlags().String("name", "", "")
sshAddCmd.MarkFlagRequired("name")
2023-07-18 22:07:40 +02:00
sshAddCmd.PersistentFlags().Bool("clipboard", false, "Copy the public key to the clipboard")
2023-07-17 03:23:26 +02:00
sshCmd.AddCommand(listSSHCmd)
}