Skip to content

Commit 581aa97

Browse files
committed
Added a start callback flag to the router
1 parent 8cfea03 commit 581aa97

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net"
2626
"os"
2727
"os/signal"
28+
"strings"
2829
"sync"
2930
"syscall"
3031
"time"
@@ -35,6 +36,7 @@ import (
3536
networkapi "github.com/arduino/arduino-router/internal/network-api"
3637
"github.com/arduino/arduino-router/msgpackrpc"
3738

39+
"github.com/arduino/go-paths-helper"
3840
"github.com/spf13/cobra"
3941
"go.bug.st/f"
4042
"go.bug.st/serial"
@@ -51,6 +53,7 @@ type Config struct {
5153
SerialPortAddr string
5254
SerialBaudRate int
5355
MonitorPortAddr string
56+
StartCallback string
5457
}
5558

5659
func main() {
@@ -80,6 +83,7 @@ func main() {
8083
cmd.Flags().StringVarP(&cfg.SerialPortAddr, "serial-port", "p", "", "Serial port address")
8184
cmd.Flags().IntVarP(&cfg.SerialBaudRate, "serial-baudrate", "b", 115200, "Serial port baud rate")
8285
cmd.Flags().StringVarP(&cfg.MonitorPortAddr, "monitor-port", "m", "127.0.0.1:7500", "Listening port for MCU monitor proxy")
86+
cmd.Flags().StringVar(&cfg.StartCallback, "after-start", "", "Command to execute when the router has successfully completed startup")
8387

8488
cmd.AddCommand(&cobra.Command{
8589
Use: "version",
@@ -224,6 +228,10 @@ func startRouter(cfg Config) error {
224228
return true, nil
225229
})
226230
f.Assert(err == nil, "Failed to register $/serial/close method")
231+
232+
var started sync.WaitGroup
233+
started.Add(1)
234+
initialized := sync.OnceFunc(started.Done)
227235
go func() {
228236
for {
229237
serialOpened.L.Lock()
@@ -246,6 +254,7 @@ func startRouter(cfg Config) error {
246254
time.Sleep(5 * time.Second)
247255
continue
248256
}
257+
initialized()
249258
slog.Info("Opened serial connection", "serial", cfg.SerialPortAddr)
250259
wr := &MsgpackDebugStream{Name: cfg.SerialPortAddr, Upstream: serialPort}
251260

@@ -262,6 +271,7 @@ func startRouter(cfg Config) error {
262271
<-routerExit
263272
}
264273
}()
274+
started.Wait()
265275
}
266276

267277
// Wait for incoming connections on all listeners
@@ -280,6 +290,20 @@ func startRouter(cfg Config) error {
280290
}()
281291
}
282292

293+
// Execute start callback if specified
294+
if cfg.StartCallback != "" {
295+
slog.Info("Executing start callback", "cmd", cfg.StartCallback)
296+
cb, err := paths.NewProcess(nil, strings.Split(cfg.StartCallback, " ")...)
297+
if err != nil {
298+
slog.Error("Failed to start callback process", "err", err)
299+
return err
300+
}
301+
if err := cb.Run(); err != nil {
302+
slog.Error("Start callback process failed", "err", err)
303+
return err
304+
}
305+
}
306+
283307
// Sleep forever until interrupted
284308
signalChan := make(chan os.Signal, 1)
285309
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

0 commit comments

Comments
 (0)