Skip to content

Commit

Permalink
📎Add clipboard commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bdsoha committed Apr 21, 2024
1 parent f528a8b commit 6f15d51
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
74 changes: 74 additions & 0 deletions cmd/clipboard/clipboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package clipboard

import (
"bytes"
"fmt"
"io"
"net/http"
"os"

"github.com/kloudkit/ws-cli/internals/net"
"github.com/spf13/cobra"
)

var ClipboardCmd = &cobra.Command{
Use: "clipboard",
Aliases: []string{"clip"},
Short: "Interact with the native clipboard",
}

var copyCmd = &cobra.Command{
Use: "copy",
Aliases: []string{"cp"},
Args: cobra.NoArgs,
Short: "Copy piped content to the clipboard",
RunE: func(cmd *cobra.Command, args []string) error {
piped, err := io.ReadAll(os.Stdin)

if err != nil {
return fmt.Errorf("failed to read from stdin: %v", err)
}

client := net.GetIPCClient()

req, err := http.NewRequest("POST", "http://localhost/clipboard", bytes.NewReader(piped))
if err != nil {
return fmt.Errorf("error sending to workspace socket: %v", err)
}

req.Header.Set("Content-Type", "text/plain")

resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error making request: %v", err)
}
resp.Body.Close()

return nil
},
}

var pasteCmd = &cobra.Command{
Use: "paste",
Short: "Paste clipboard content to the terminal",
RunE: func(cmd *cobra.Command, args []string) error {
client := net.GetIPCClient()

resp, err := client.Get("http://localhost/clipboard")
if err != nil {
return fmt.Errorf("error retrieving from workspace socket: %v", err)
}
defer resp.Body.Close()

_, err = io.Copy(cmd.OutOrStdout(), resp.Body)
if err != nil {
return fmt.Errorf("error outputting clipboard data: %v", err)
}

return nil
},
}

func init() {
ClipboardCmd.AddCommand(copyCmd, pasteCmd)
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/kloudkit/ws-cli/cmd/config"
"github.com/kloudkit/ws-cli/cmd/clipboard"
"github.com/kloudkit/ws-cli/cmd/feature"
"github.com/kloudkit/ws-cli/cmd/fonts"
"github.com/kloudkit/ws-cli/cmd/get"
Expand All @@ -29,6 +30,7 @@ func Execute() {
func init() {
rootCmd.AddCommand(
config.ConfigCmd,
clipboard.ClipboardCmd,
feature.FeatureCmd,
fonts.FontsCmd,
get.GetCmd,
Expand Down
19 changes: 19 additions & 0 deletions internals/net/ipcClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net

import (
"context"
nativeNet "net"
"net/http"
"time"
)

func GetIPCClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (nativeNet.Conn, error) {
return nativeNet.Dial("unix", "/tmp/workspace-ipc.sock")
},
},
Timeout: 3 * time.Second,
}
}

0 comments on commit 6f15d51

Please sign in to comment.