goldwarden/cli/cmd/daemonize.go

79 lines
2.2 KiB
Go
Raw Normal View History

2023-07-17 04:23:26 +03:00
package cmd
import (
2024-02-04 03:58:04 +03:00
"fmt"
2024-02-21 08:42:10 +03:00
"os"
2024-02-04 03:58:04 +03:00
"syscall"
2023-07-17 04:23:26 +03:00
"github.com/awnumar/memguard"
2024-05-04 02:06:24 +03:00
"github.com/quexten/goldwarden/cli/agent"
2023-07-17 04:23:26 +03:00
"github.com/spf13/cobra"
)
var daemonizeCmd = &cobra.Command{
Use: "daemonize",
Short: "Starts the agent as a daemon",
Long: `Starts the agent as a daemon. The agent will run in the background and will
run in the background until it is stopped.`,
Run: func(cmd *cobra.Command, args []string) {
2023-08-21 19:37:34 +03:00
websocketDisabled := runtimeConfig.WebsocketDisabled
sshDisabled := runtimeConfig.DisableSSHAgent
2023-08-21 14:52:06 +03:00
if websocketDisabled {
fmt.Println("Websocket disabled")
2023-08-21 14:52:06 +03:00
}
if sshDisabled {
fmt.Println("SSH agent disabled")
2023-08-21 14:52:06 +03:00
}
2024-02-04 03:58:04 +03:00
cleanup := func() {
fmt.Println("removing sockets and exiting")
fmt.Println("unlinking", runtimeConfig.GoldwardenSocketPath)
err := syscall.Unlink(runtimeConfig.GoldwardenSocketPath)
if err != nil {
fmt.Println(err)
}
fmt.Println("unlinking", runtimeConfig.SSHAgentSocketPath)
err = syscall.Unlink(runtimeConfig.SSHAgentSocketPath)
if err != nil {
fmt.Println(err)
}
fmt.Println("memguard wiping memory and exiting")
2023-07-17 04:23:26 +03:00
memguard.SafeExit(0)
2024-02-04 03:58:04 +03:00
}
2024-02-21 08:42:10 +03:00
home, _ := os.UserHomeDir()
_, err := os.Stat("/.flatpak-info")
isFlatpak := err == nil
if runtimeConfig.GoldwardenSocketPath == "" {
if isFlatpak {
2024-02-23 07:57:46 +03:00
fmt.Println("Socket path is empty, overwriting with flatpak path.")
2024-02-21 08:42:10 +03:00
runtimeConfig.GoldwardenSocketPath = home + "/.var/app/com.quexten.Goldwarden/data/goldwarden.sock"
2024-02-23 07:57:46 +03:00
} else {
fmt.Println("Socket path is empty, overwriting with default path.")
runtimeConfig.GoldwardenSocketPath = home + "/.goldwarden.sock"
2024-02-21 08:42:10 +03:00
}
}
if runtimeConfig.SSHAgentSocketPath == "" {
if isFlatpak {
2024-02-23 07:57:46 +03:00
fmt.Println("SSH Agent socket path is empty, overwriting with flatpak path.")
2024-02-21 08:42:10 +03:00
runtimeConfig.SSHAgentSocketPath = home + "/.var/app/com.quexten.Goldwarden/data/ssh-auth-sock"
2024-02-23 07:57:46 +03:00
} else {
fmt.Println("SSH Agent socket path is empty, overwriting with default path.")
runtimeConfig.SSHAgentSocketPath = home + "/.goldwarden-ssh-agent.sock"
2024-02-21 08:42:10 +03:00
}
}
err = agent.StartUnixAgent(runtimeConfig.GoldwardenSocketPath, runtimeConfig)
2023-07-17 04:23:26 +03:00
if err != nil {
panic(err)
}
2024-02-04 03:58:04 +03:00
cleanup()
2023-07-17 04:23:26 +03:00
},
}
func init() {
rootCmd.AddCommand(daemonizeCmd)
}