ffctl/ffscript/main.go

62 lines
1.2 KiB
Go

package main
import (
"context"
"fmt"
"io"
"net"
"os"
"time"
)
func main() {
if err := WithSession(context.Background(), func(ctx context.Context, t *TCPTransport) error {
if err := t.CmdPerformActions(ActionSequence{
Type: ActionSourceKeyboard,
ID: "my_keyboard",
Actions: []any{
ActionPause(1 * time.Second),
ActionKeyDown(KeyF12),
ActionPause(15 * time.Millisecond),
ActionKeyUp(KeyF12),
ActionPause(1 * time.Second),
},
}); err != nil {
return err
}
return nil
}); err != nil {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
os.Exit(1)
}
}
func WithSession(ctx context.Context, fn func(context.Context, *TCPTransport) error) error {
return WithGecko(ctx, func(ctx context.Context, c *net.TCPConn) (_retErr error) {
maybeSetErr := func(_err error) {
if _retErr == nil && _err != nil {
_retErr = _err
}
}
t, err := NewTransport(c)
if err != nil {
return err
}
if _, err := t.CmdNewSession(nil); err != nil {
return err
}
defer func() {
_, err := t.CmdQuit()
maybeSetErr(err)
maybeSetErr(c.CloseWrite())
_, err = io.Copy(io.Discard, c)
maybeSetErr(err)
}()
return fn(ctx, t)
})
}