ffctl/ffscript/main.go

62 lines
1.2 KiB
Go
Raw Normal View History

2024-02-09 01:53:56 +00:00
package main
import (
"context"
"fmt"
2024-02-09 08:49:16 +00:00
"io"
"net"
2024-02-09 01:53:56 +00:00
"os"
2024-02-09 08:05:39 +00:00
"time"
2024-02-09 01:53:56 +00:00
)
func main() {
if err := WithSession(context.Background(), func(ctx context.Context, t *TCPTransport) error {
2024-02-09 08:49:16 +00:00
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),
2024-02-09 08:05:39 +00:00
},
2024-02-09 08:49:16 +00:00
}); err != nil {
2024-02-09 08:05:39 +00:00
return err
}
2024-02-09 01:53:56 +00:00
return nil
}); err != nil {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
os.Exit(1)
}
}
2024-02-09 08:05:39 +00:00
2024-02-09 08:49:16 +00:00
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
}
}
2024-02-09 08:05:39 +00:00
2024-02-09 08:49:16 +00:00
t, err := NewTransport(c)
if err != nil {
return err
}
2024-02-09 08:05:39 +00:00
2024-02-09 08:49:16 +00:00
if _, err := t.CmdNewSession(nil); err != nil {
return err
}
2024-02-09 08:05:39 +00:00
2024-02-09 08:49:16 +00:00
defer func() {
_, err := t.CmdQuit()
maybeSetErr(err)
maybeSetErr(c.CloseWrite())
_, err = io.Copy(io.Discard, c)
maybeSetErr(err)
}()
return fn(ctx, t)
})
2024-02-09 08:05:39 +00:00
}