Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Jul 4, 2015
1 parent f4b832f commit 0c139b8
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,42 @@ import (
"net/rpc"
)

func (c *CLI) Open() int {
client, err := c.rpcClient()
if err != nil {
c.writeError(err)
return RPCError
}

err = client.Call("URI.Open", c.DataSource, &struct{}{})
if err != nil {
c.writeError(err)
return RPCError
}
var dummy = &struct{}{}

return Success
func (c *CLI) Open() int {
return c.withRPCClient(func(client *rpc.Client) error {
return client.Call("URI.Open", c.DataSource, dummy)
})
}

func (c *CLI) Paste() int {
client, err := c.rpcClient()
if err != nil {
c.writeError(err)
return RPCError
}

var resp string
err = client.Call("Clipboard.Paste", struct{}{}, &resp)
if err != nil {
c.writeError(err)
return RPCError
}
c.Out.Write([]byte(resp))

return Success
return c.withRPCClient(func(client *rpc.Client) error {
var resp string
err := client.Call("Clipboard.Paste", dummy, &resp)
if err == nil {
c.Out.Write([]byte(resp))
}
return err
})
}

func (c *CLI) Copy() int {
client, err := c.rpcClient()
return c.withRPCClient(func(client *rpc.Client) error {
return client.Call("Clipboard.Copy", c.DataSource, dummy)
})
}

func (c *CLI) withRPCClient(f func(*rpc.Client) error) int {
client, err := rpc.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, c.Port))
if err != nil {
c.writeError(err)
return RPCError
}

err = client.Call("Clipboard.Copy", c.DataSource, &struct{}{})
err = f(client)
if err != nil {
c.writeError(err)
return RPCError
}

return Success
}

func (c *CLI) rpcClient() (*rpc.Client, error) {
return rpc.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, c.Port))
}

0 comments on commit 0c139b8

Please sign in to comment.