Files
cunicu/cmd/monitor.go
Steffen Vogel 410756f11c windows portability fixes
Signed-off-by: Steffen Vogel <post@steffenvogel.de>
2022-08-04 00:20:43 +02:00

67 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"
"riasc.eu/wice/pkg/config"
"riasc.eu/wice/pkg/util"
)
var monitorCmd = &cobra.Command{
Use: "monitor",
Short: "Monitor the ɯice daemon for events",
Run: monitor,
Args: cobra.NoArgs,
}
var format config.OutputFormat
func init() {
addClientCommand(RootCmd, monitorCmd)
f := monitorCmd.PersistentFlags()
f.VarP(&format, "format", "f", fmt.Sprintf("Output `format` (one of: %s)", strings.Join(config.OutputFormatNames, ", ")))
}
func monitor(cmd *cobra.Command, args []string) {
signals := util.SetupSignals()
logger := logger.Named("events")
mo := protojson.MarshalOptions{
UseProtoNames: true,
}
out:
for {
select {
case sig := <-signals:
logger.Info("Received signal", zap.Any("signal", sig))
break out
case evt := <-client.Events:
switch format {
case config.OutputFormatCSV:
case config.OutputFormatJSON:
buf, err := mo.Marshal(evt)
if err != nil {
logger.Fatal("Failed to marshal", zap.Error(err))
}
buf = append(buf, '\n')
if _, err = os.Stdout.Write(buf); err != nil {
logger.Fatal("Failed to write to stdout", zap.Error(err))
}
case config.OutputFormatLogger:
evt.Log(logger, "Event")
}
}
}
}