Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/conch/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var compCommands = []compItem{
{"send", "type into a pane"},
{"wait", "wait until an agent is done"},
{"close", "close a pane"},
{"desktop", "the bridge a desktop app talks to"},
{"rename", "name a pane"},
{"redraw", "draw a pane's screen again"},
{"view", "what the terminal is showing"},
Expand Down
92 changes: 92 additions & 0 deletions cmd/conch/desktop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"os/signal"
"sync"
"time"

"github.com/Amitgb14/conch/internal/client"
"github.com/Amitgb14/conch/internal/config"
"github.com/Amitgb14/conch/internal/desktop"
"github.com/Amitgb14/conch/internal/fleet"
"github.com/Amitgb14/conch/internal/proto"
)

// `conch desktop` is the bridge a desktop app runs and talks to over this
// process's stdin and stdout (internal/desktop): the Linux and Windows
// apps, and in time Conch.app. It is a command rather than a server of its
// own because the app is what decides it is needed, and because a pipe
// crosses into WSL (`wsl.exe -d Ubuntu conch desktop`) and over ssh with
// nothing listening and nothing to authenticate. Like `conch bridge`,
// stdout carries only the protocol: anything to say goes to stderr.

const desktopUsage = "usage: conch desktop [-every DURATION]"

func runDesktop(args []string) error {
return runDesktopOn(args, os.Stdin, os.Stdout)
}

func runDesktopOn(args []string, in io.Reader, out io.Writer) error {
fs := flag.NewFlagSet("desktop", flag.ContinueOnError)
fs.SetOutput(os.Stderr)
every := fs.Duration("every", time.Second, "how often the servers are read for changes")
if err := fs.Parse(args); err != nil {
return err
}
if fs.NArg() > 0 {
return errors.New(desktopUsage)
}
if *every <= 0 {
return fmt.Errorf("-every must be more than 0, not %s", *every)
}
sock := config.SocketPath()
if err := client.EnsureServer(sock, config.ServerLogPath()); err != nil {
return err
}
self, err := os.Executable()
if err != nil {
return err
}

// This computer's connection, made again if it drops (the server
// reloading closes it), as the phone gateway's is.
var mu sync.Mutex
var shared *client.Client
local := func() (*client.Client, error) {
mu.Lock()
defer mu.Unlock()
if shared != nil && shared.Err() == nil {
return shared, nil
}
c, err := client.Dial(sock, "conch-desktop")
if err != nil {
return nil, err
}
shared = c
return c, nil
}
pool := fleet.New(fleet.Options{
Local: local,
DialLocal: func() (*client.Client, error) { return client.Dial(sock, "conch-desktop") },
})
defer pool.Close()
defer func() {
mu.Lock()
if shared != nil {
shared.Close()
}
mu.Unlock()
}()

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
return desktop.Serve(ctx, in, out, desktop.Options{
Machines: pool, Conch: self, Version: proto.Version, Every: *every,
})
}
104 changes: 104 additions & 0 deletions cmd/conch/desktop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"bufio"
"encoding/json"
"io"
"os"
"strings"
"testing"
"time"

"github.com/Amitgb14/conch/internal/client"
"github.com/Amitgb14/conch/internal/config"
"github.com/Amitgb14/conch/internal/desktop"
"github.com/Amitgb14/conch/internal/server"
)

func TestDesktopArgs(t *testing.T) {
a4Env(t)
for _, args := range [][]string{{"extra"}, {"-every", "0s"}, {"-every", "-1s"}, {"-nope"}} {
if err := runDesktopOn(args, strings.NewReader(""), io.Discard); err == nil {
t.Errorf("%q was taken", args)
}
}
}

// TestDesktopOverARealServer runs the command as an app would: hello in,
// hello and the snapshot of this computer out, and the bridge ending when
// the app closes its end.
func TestDesktopOverARealServer(t *testing.T) {
dir := a4Env(t)
srv := server.New(config.SocketPath(), dir)
go srv.Run()
t.Cleanup(srv.StopAndWait)
for i := 0; ; i++ {
if c, err := client.Dial(config.SocketPath(), "test"); err == nil {
c.Close()
break
}
if i == 200 {
t.Fatal("server did not start")
}
time.Sleep(20 * time.Millisecond)
}

inR, inW := io.Pipe()
outR, outW := io.Pipe()
done := make(chan error, 1)
go func() {
done <- runDesktopOn([]string{"-every", "50ms"}, inR, outW)
outW.Close()
}()
if _, err := inW.Write([]byte(`{"type":"hello","data":{"app":"test"}}` + "\n")); err != nil {
t.Fatal(err)
}
lines := bufio.NewReader(outR)
read := func() desktop.Message {
t.Helper()
got := make(chan desktop.Message, 1)
go func() {
line, err := lines.ReadBytes('\n')
var m desktop.Message
if err == nil {
err = json.Unmarshal(line, &m)
}
if err != nil {
m.Error = "read: " + err.Error()
}
got <- m
}()
select {
case m := <-got:
return m
case <-time.After(15 * time.Second):
t.Fatal("timed out reading conch desktop")
}
return desktop.Message{}
}
if m := read(); m.Type != desktop.TypeHello {
t.Fatalf("first: %+v", m)
}
m := read()
var st desktop.State
if err := json.Unmarshal(m.Data, &st); err != nil || m.Type != desktop.TypeSnapshot {
t.Fatalf("second: %+v %v", m, err)
}
if len(st.Machines) == 0 || st.Machines[0].ID != "local" || st.Machines[0].State != "online" {
t.Errorf("snapshot: %+v", st.Machines)
}

inW.Close()
go io.Copy(io.Discard, outR)
select {
case err := <-done:
if err != nil {
t.Errorf("an app closing its end is not an error: %v", err)
}
case <-time.After(10 * time.Second):
t.Fatal("conch desktop did not end with its input")
}
if _, err := os.Stat(config.SocketPath()); err != nil {
t.Errorf("the server went with the bridge: %v", err)
}
}
5 changes: 5 additions & 0 deletions cmd/conch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ Usage:
terminal the program's own output to draw, not conch's screen;
-view (with -raw) never resizes the pane or takes its size
conch close ID close a pane
conch desktop [-every D] the bridge a desktop app talks to on stdin and stdout: every
machine's panes and agents as JSON lines (the Linux and
Windows apps); -every is how often it looks for changes
conch rename ID [NAME] name a pane; without NAME it gets its own back. Every
command taking a pane ID takes its name too
conch redraw ID draw a pane's screen again (after a program left stale text)
Expand Down Expand Up @@ -503,6 +506,8 @@ func main() {
err = runSandbox(args)
case "bridge":
err = runBridge()
case "desktop":
err = runDesktop(args)
case "update":
err = runUpdate(args)
case "upload":
Expand Down
10 changes: 2 additions & 8 deletions cmd/conch/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
Expand All @@ -17,6 +16,7 @@ import (

"github.com/Amitgb14/conch/internal/client"
"github.com/Amitgb14/conch/internal/config"
"github.com/Amitgb14/conch/internal/desktop"
"github.com/Amitgb14/conch/internal/proto"
"github.com/Amitgb14/conch/internal/remote"
"github.com/Amitgb14/conch/internal/sandbox"
Expand Down Expand Up @@ -697,13 +697,7 @@ func periodWhat(p sandbox.UsagePeriod) string {

// openInBrowser hands a link to the desktop. A variable so tests can check
// what would open without a window appearing on whoever runs them.
var openInBrowser = func(url string) error {
tool := "xdg-open"
if runtime.GOOS == "darwin" {
tool = "open"
}
return exec.Command(tool, url).Start()
}
var openInBrowser = desktop.Open

// sandboxUsage prints what a sandbox has cost, and where it went.
func sandboxUsage(args []string) error {
Expand Down
Loading
Loading