Skip to content

Commit

Permalink
Switch client to unix sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
marwan-at-work committed Nov 17, 2020
1 parent 3d9e682 commit 1665db3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
16 changes: 15 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
"sync"
"time"

"github.com/gorilla/websocket"
"google.golang.org/protobuf/proto"
Expand All @@ -27,7 +30,18 @@ func New() (*Client, error) {
if cookie := os.Getenv("ITERM2_COOKIE"); cookie != "" {
h.Set("x-iterm2-cookie", cookie)
}
c, resp, err := websocket.DefaultDialer.Dial("ws://localhost:1912", h)
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("os.UserHomeDir: %w", err)
}
d := &websocket.Dialer{
NetDial: func(network, addr string) (net.Conn, error) {
return net.Dial("unix", filepath.Join(homeDir, "/Library/Application Support/iTerm2/private/socket"))
},
HandshakeTimeout: 45 * time.Second,
Subprotocols: []string{"api.iterm2.com"},
}
c, resp, err := d.Dial("ws://localhost", h)
if err != nil && resp != nil {
b, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("error connecting to iTerm2: %v - body: %s", err, b)
Expand Down
53 changes: 53 additions & 0 deletions cmd/goiterm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/urfave/cli/v2"
)

func main() {
app := &cli.App{
Name: "goiterm",
Commands: []*cli.Command{
{
Name: "install",
Usage: "goiterm install <go binary name>",
Description: "Installs your plugin to the iTerm app",
Action: func(c *cli.Context) error {
bin := c.Args().First()
if bin == "" {
return cli.NewExitError("must pass go binary as first argument", 1)
}
homedir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("os.UserHomeDir: %w", err)
}
p := filepath.Join(homedir, "/Library/Application Support/iTerm2/Scripts/", bin+".py")
f := fmt.Sprintf(pyFile, bin)
err = ioutil.WriteFile(p, []byte(f), 0666)
if err != nil {
return fmt.Errorf("ioutil.WriteFile: %w", err)
}
return nil
},
},
},
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

const pyFile = `#!/usr/bin/env python3.7
import subprocess
subprocess.run(["%s"])
`
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ go 1.15
require (
github.com/golang/protobuf v1.4.2
github.com/gorilla/websocket v1.4.2
github.com/urfave/cli/v2 v2.3.0
google.golang.org/protobuf v1.23.0
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
Expand All @@ -11,6 +14,14 @@ github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand All @@ -20,3 +31,5 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit 1665db3

Please sign in to comment.