package main
import (
"context"
"fmt"
"time"
"github.com/alexflint/go-arg"
"github.com/joho/godotenv"
"github.com/pancsta/asyncmachine-go/examples/cli_daemon/states"
"github.com/pancsta/asyncmachine-go/examples/cli_daemon/types"
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
arpc "github.com/pancsta/asyncmachine-go/pkg/rpc"
ssrpc "github.com/pancsta/asyncmachine-go/pkg/rpc/states"
)
var ss = states .DaemonStates
type Args struct {
Foo1 bool `arg:"--foo-1" help:"Foo help"`
Bar2 bool `arg:"--bar-2" help:"Bar help"`
Addr string `default:"localhost:8090"`
Duration time .Duration `arg:"-d,--duration" default:"3s" help:"Duration of the op"`
Debug bool `default:"true" help:"Enable debugging for asyncmachine"`
}
type cli struct {
*am .ExceptionHandler
Mach *am .Machine
DaemonRpc *arpc .Client
Args *Args
}
var args Args
func main() {
p := arg .MustParse (&args )
ctx := context .Background ()
cli , err := newCli (ctx , &args )
if err != nil {
p .Fail (err .Error())
}
netmach := cli .DaemonRpc .NetMach
if args .Foo1 {
netmach .Add1 (ss .OpFoo1 , types .PassRpc (&types .ARpc {
Duration : args .Duration ,
}))
} else if args .Bar2 {
netmach .Add1 (ss .OpBar2 , types .PassRpc (&types .ARpc {
Duration : args .Duration ,
}))
} else {
p .Fail ("no flag" )
}
err = amhelp .WaitForAll (ctx , 5 *time .Second ,
cli .Mach .When1 (ssrpc .ConsumerStates .WorkerPayload , nil ))
if err != nil {
p .Fail (err .Error())
}
}
func newCli(ctx context .Context , args *Args ) (*cli , error ) {
if args .Debug {
fmt .Printf ("debugging enabled\n" )
_ = godotenv .Load ()
}
consumer := am .New (ctx , ssrpc .ConsumerSchema , nil )
err := consumer .BindHandlers (&cli {})
if err != nil {
return nil , err
}
c , err := newClient (ctx , args .Addr , consumer , states .DaemonSchema )
if err != nil {
return nil , err
}
c .Start ()
err = amhelp .WaitForAll (ctx , 3 *time .Second ,
c .Mach .When1 (ssrpc .ClientStates .Ready , ctx ))
fmt .Printf ("Connected to aRPC %s\n" , c .Addr )
return &cli {
Mach : consumer ,
DaemonRpc : c ,
Args : args ,
}, nil
}
func newClient(
ctx context .Context , addr string , consumer *am .Machine , netSrcSchema am .Schema ,
) (*arpc .Client , error ) {
id := "cli-" + time .Now ().Format (time .RFC3339Nano )
c , err := arpc .NewClient (ctx , addr , id , netSrcSchema , &arpc .ClientOpts {
Consumer : consumer ,
})
if err != nil {
return nil , err
}
amhelp .MachDebugEnv (c .Mach )
return c , nil
}
func (c *cli ) WorkerPayloadState (e *am .Event ) {
e .Machine ().Remove1 (ssrpc .ConsumerStates .WorkerPayload , nil )
args := arpc .ParseArgs (e .Args )
println ("Payload: " + args .Payload .Data .(string ))
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .