2023-07-17 03:23:26 +02:00
|
|
|
/*
|
|
|
|
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// runCmd represents the run command
|
|
|
|
var runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
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) {
|
|
|
|
if len(args) < 1 {
|
|
|
|
println("Error: No command specified")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
executable := args[0]
|
|
|
|
executableArgs := args[1:]
|
|
|
|
|
|
|
|
env := []string{}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
result, err := commandClient.SendToAgent(messages.GetCLICredentialsRequest{
|
2023-07-17 03:23:26 +02:00
|
|
|
ApplicationName: executable,
|
|
|
|
})
|
|
|
|
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.GetCLICredentialsResponse:
|
|
|
|
response := result.(messages.GetCLICredentialsResponse)
|
2023-07-17 03:23:26 +02:00
|
|
|
for key, value := range response.Env {
|
|
|
|
env = append(env, key+"="+value)
|
|
|
|
}
|
2023-09-20 03:05:44 +02:00
|
|
|
case messages.ActionResponse:
|
|
|
|
println("Error: " + result.(messages.ActionResponse).Message)
|
2023-07-17 03:23:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
command := exec.Command(executable, executableArgs...)
|
|
|
|
command.Env = append(command.Env, os.Environ()...)
|
|
|
|
command.Env = append(command.Env, env...)
|
|
|
|
command.Stdout = os.Stdout
|
|
|
|
command.Stderr = os.Stderr
|
|
|
|
command.Stdin = os.Stdin
|
|
|
|
command.Run()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
}
|