-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send: use send instead of nc or echo to communicate with the unix socket
Using echo to write data to a unix socket as if it is a regular file "works", but it doesn't respect the usual protocol that `netcat` uses. However, `netcat` comes in two main flavors. GNU and BSD. BSD netcat has the -w and -U flag, both of which are missing form GNU. The reliability of savvy record now has a hard dependency on the version of netcat installed on the user's machine. `socat` is a third alternative but that is not guaranteed to be installed by default in most cases. This commit introduces `savvy send` to address all the issue above. `send` is a simple go program that creates a conn to the unix socket and uses the go standard library to write data to the socket and close the conn. send will support all the platforms that Go supports by default and we no longer rely on the user having a particular binary.
- Loading branch information
Showing
3 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters