Skip to content

Commit

Permalink
feat(daemon/client): add Execute method
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Dec 29, 2023
1 parent be42a5b commit 3c2a0d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
11 changes: 11 additions & 0 deletions server/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/nedpals/bugbuddy/server/rpc"
)

type Client = client.Client

const DEFAULT_PORT = ":3434"

Expand All @@ -32,3 +33,13 @@ func Connect(addr string, clientType types.ClientType, handlerFunc ...rpc.Handle
func Serve(addr string) error {
return server.Start(addr)
}

func Execute(clientType types.ClientType, execFn func(client *Client) error) error {
client := NewClient(context.Background(), CURRENT_DAEMON_PORT, clientType)
if err := client.Connect(); err != nil {
if err := client.EnsureConnection(); err != nil {
return err
}
}
return execFn(client)
}
36 changes: 24 additions & 12 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ var rootCmd = &cobra.Command{
log.Fatalln(err)
}

daemonClient := daemon.Connect(daemon.DEFAULT_PORT, types.MonitorClientType)
numErrors, errCode, err := monitorProcess(wd, daemonClient, args[0], args[1:]...)
err = daemon.Execute(types.MonitorClientType, func(client *daemon.Client) error {
numErrors, errCode, err := monitorProcess(wd, client, args[0], args[1:]...)
if err != nil {
return err
} else if errCode > 0 {
os.Stderr.WriteString(fmt.Sprintf("\n\nCatched %d error/s.\n", numErrors))
os.Exit(errCode)
}
return nil
})

if err != nil {
log.Fatalln(err)
} else if errCode > 0 {
os.Stderr.WriteString(fmt.Sprintf("\n\nCatched %d error/s.\n", numErrors))
os.Exit(errCode)
}

return nil
Expand Down Expand Up @@ -102,18 +108,24 @@ var participantIdCmd = &cobra.Command{
log.Fatalln(err)
}

var participantId string
daemonClient := daemon.Connect(daemon.DEFAULT_PORT, types.MonitorClientType)
err = daemon.Execute(types.MonitorClientType, func(client *daemon.Client) error {
var participantId string

if isGenerate {
if participantId, err = daemonClient.GenerateParticipantId(); err != nil {
log.Fatalln(err)
if isGenerate {
if participantId, err = client.GenerateParticipantId(); err != nil {
return err
}
} else if participantId, err = client.RetrieveParticipantId(); err != nil {
return err
}
} else if participantId, err = daemonClient.RetrieveParticipantId(); err != nil {

fmt.Println(participantId)
return nil
})
if err != nil {
log.Fatalln(err)
}

fmt.Println(participantId)
return nil
},
}
Expand Down

0 comments on commit 3c2a0d9

Please sign in to comment.