2023-07-17 03:23:26 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-15 16:23:11 +01:00
|
|
|
"os"
|
2023-07-17 03:23:26 +02:00
|
|
|
|
2023-07-17 20:59:17 +02:00
|
|
|
"github.com/atotto/clipboard"
|
2023-09-20 03:05:44 +02:00
|
|
|
"github.com/quexten/goldwarden/ipc/messages"
|
2023-07-17 03:23:26 +02:00
|
|
|
"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) {
|
2024-03-15 16:23:11 +01:00
|
|
|
_ = cmd.Help()
|
2023-07-17 03:23:26 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2024-03-15 16:22:45 +01:00
|
|
|
err := loginIfRequired()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-08-21 18:37:34 +02:00
|
|
|
|
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-09-20 03:05:44 +02:00
|
|
|
result, err := commandClient.SendToAgent(messages.CreateSSHKeyRequest{
|
2023-07-17 03:23:26 +02:00
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-11 22:45:01 +02:00
|
|
|
handleSendToAgentError(err)
|
2023-07-17 03:23:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch result.(type) {
|
2023-09-20 03:05:44 +02:00
|
|
|
case messages.CreateSSHKeyResponse:
|
|
|
|
response := result.(messages.CreateSSHKeyResponse)
|
2023-07-17 03:23:26 +02:00
|
|
|
fmt.Println(response.Digest)
|
2023-07-18 22:07:40 +02:00
|
|
|
|
|
|
|
if copyToClipboard {
|
2024-03-15 16:22:45 +01:00
|
|
|
err := clipboard.WriteAll(string(response.Digest))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-07-18 22:07:40 +02:00
|
|
|
}
|
2024-03-15 16:23:11 +01:00
|
|
|
return
|
2023-09-20 03:05:44 +02:00
|
|
|
case messages.ActionResponse:
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Error: " + result.(messages.ActionResponse).Message)
|
2023-07-17 03:23:26 +02:00
|
|
|
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) {
|
2024-03-15 16:22:45 +01:00
|
|
|
err := loginIfRequired()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-08-21 18:37:34 +02:00
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
result, err := commandClient.SendToAgent(messages.GetSSHKeysRequest{})
|
2023-07-17 03:23:26 +02:00
|
|
|
if err != nil {
|
2023-09-11 22:45:01 +02:00
|
|
|
handleSendToAgentError(err)
|
2023-07-17 03:23:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch result.(type) {
|
2023-09-20 03:05:44 +02:00
|
|
|
case messages.GetSSHKeysResponse:
|
|
|
|
response := result.(messages.GetSSHKeysResponse)
|
2023-07-17 03:23:26 +02:00
|
|
|
for _, key := range response.Keys {
|
|
|
|
fmt.Println(key)
|
|
|
|
}
|
2024-03-15 16:23:11 +01:00
|
|
|
return
|
2023-09-20 03:05:44 +02:00
|
|
|
case messages.ActionResponse:
|
2024-02-08 16:35:07 +01:00
|
|
|
fmt.Println("Error: " + result.(messages.ActionResponse).Message)
|
2023-07-17 03:23:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(sshCmd)
|
|
|
|
sshCmd.AddCommand(sshAddCmd)
|
|
|
|
sshAddCmd.PersistentFlags().String("name", "", "")
|
2024-03-15 16:23:11 +01:00
|
|
|
_ = 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)
|
|
|
|
}
|