diff --git a/cmd/send.go b/cmd/send.go new file mode 100644 index 0000000..5859f2d --- /dev/null +++ b/cmd/send.go @@ -0,0 +1,48 @@ +package cmd + +import ( + "fmt" + "net" + "os" + + "github.com/getsavvyinc/savvy-cli/display" + "github.com/spf13/cobra" +) + +// sendCmd represents the send command +var sendCmd = &cobra.Command{ + Use: "send", + // Send is meant for internal use only. + Hidden: true, + Short: "Send data to the unix socket listening at SAVVY_SOCKET_PATH", + Long: `Send data to the unix socket listening at SAVVY_SOCKET_PATH`, + Run: func(cmd *cobra.Command, args []string) { + socketPath := os.Getenv("SAVVY_SOCKET_PATH") + if socketPath == "" { + err := fmt.Errorf("cannot record commands: SAVVY_SOCKET_PATH is not set") + display.ErrorWithSupportCTA(err) + return + } + conn, err := net.Dial("unix", socketPath) + if err != nil { + err = fmt.Errorf("failed to record command: %v", err) + display.ErrorWithSupportCTA(err) + return + } + defer conn.Close() + message := args[:] + if len(message) == 0 { + // nothing to do. + return + } + if _, err = conn.Write([]byte(fmt.Sprintf("%s\n", message))); err != nil { + err = fmt.Errorf("failed to record command locally: %v", err) + display.ErrorWithSupportCTA(err) + return + } + }, +} + +func init() { + rootCmd.AddCommand(sendCmd) +} diff --git a/cmd/setup/bash-preexec.sh b/cmd/setup/bash-preexec.sh index 4eeabe5..6cb7fc8 100644 --- a/cmd/setup/bash-preexec.sh +++ b/cmd/setup/bash-preexec.sh @@ -59,7 +59,7 @@ savvy_cmd_pre_exec() { #TODO: expand aliases local cmd=$BASH_COMMAND if [[ "${SAVVY_CONTEXT}" == "1" ]] ; then - nc -w0 -U $SAVVY_INPUT_FILE <<< "$cmd" + SAVVY_SOCKET_PATH=${SAVVY_INPUT_FILE} savvy send "$cmd" fi } diff --git a/cmd/setup/savvy.zsh b/cmd/setup/savvy.zsh index c5b9e86..5193597 100755 --- a/cmd/setup/savvy.zsh +++ b/cmd/setup/savvy.zsh @@ -14,7 +14,7 @@ function __savvy_cmd_pre_exec__() { # $2 is the command with all the aliases expanded local cmd=$3 if [[ "${SAVVY_CONTEXT}" == "1" ]] ; then - nc -w0 -U $SAVVY_INPUT_FILE <<< "$cmd" + SAVVY_SOCKET_PATH=${SAVVY_INPUT_FILE} savvy send "$cmd" fi } add-zsh-hook preexec __savvy_cmd_pre_exec__