gotelem/cmd/skylabify/skylabify.go

134 lines
2.7 KiB
Go
Raw Normal View History

2023-05-20 19:53:56 +00:00
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"syscall"
"github.com/kschamplin/gotelem/internal/logparsers"
2023-05-20 19:53:56 +00:00
"github.com/kschamplin/gotelem/skylab"
"github.com/urfave/cli/v2"
2023-06-28 01:39:57 +00:00
"golang.org/x/exp/slog"
2023-05-20 19:53:56 +00:00
)
// this command can be used to decode candump logs and dump json output.
func main() {
app := cli.NewApp()
app.Name = "skylabify"
app.Usage = "decode skylab packets"
app.ArgsUsage = "<input file>"
app.Commands = nil
app.Description = `skylabify can read in candump logs and output newline-delimited JSON.
It is designed to make reading candumps fast and easy.
skylabify can be combined with jq and candump to allow for advanced queries.
Examples:
skylabify candump.txt
candump -L can0 | skylabify -
skylabify previous_candump.txt | jq <some json query>
I highly suggest reading the manpages for candump and jq. The -L option is
required for piping candump into skylabify. Likewise, data should be stored with
-l.
`
parsersString := func() string {
// create a string like "'telem', 'candump', 'anotherparser'"
keys := make([]string, len(logparsers.ParsersMap))
i := 0
for k := range logparsers.ParsersMap {
keys[i] = k
i++
}
s := strings.Join(keys, "', '")
return "'" + s + "'"
}()
2023-05-20 19:53:56 +00:00
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
},
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Usage: "the format of the incoming data. One of " + parsersString,
},
2023-05-20 19:53:56 +00:00
}
app.Action = run
if err := app.Run(os.Args); err != nil {
panic(err)
}
}
2023-05-20 19:53:56 +00:00
func run(ctx *cli.Context) (err error) {
path := ctx.Args().Get(0)
if path == "" {
2023-07-07 19:03:53 +00:00
fmt.Println("missing input file")
2023-05-20 19:53:56 +00:00
cli.ShowAppHelpAndExit(ctx, int(syscall.EINVAL))
}
var istream *os.File
if path == "-" {
istream = os.Stdin
} else {
istream, err = os.Open(path)
if err != nil {
return
}
}
fileReader := bufio.NewReader(istream)
var pfun logparsers.ParserFunc
pfun, ok := logparsers.ParsersMap[ctx.String("format")]
if !ok {
fmt.Println("invalid format!")
cli.ShowAppHelpAndExit(ctx, int(syscall.EINVAL))
}
n_err := 0
unknown_packets := 0
2023-05-20 19:53:56 +00:00
for {
line, err := fileReader.ReadString('\n')
2023-05-20 19:53:56 +00:00
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err // i/o failures are fatal
2023-05-20 19:53:56 +00:00
}
f, err := pfun(line)
2023-06-28 01:39:57 +00:00
var idErr *skylab.UnknownIdError
if errors.As(err, &idErr) {
// unknown id
slog.Info("unknown id", "err", err)
unknown_packets++
2023-07-01 03:08:06 +00:00
continue
2023-06-28 01:39:57 +00:00
} else if err != nil {
// TODO: we should consider absorbing all errors.
fmt.Printf("got an error %v\n", err)
n_err++
continue
2023-05-20 19:53:56 +00:00
}
2023-05-22 13:50:07 +00:00
// format and print out the JSON.
out, _ := json.Marshal(&f)
2023-05-22 13:50:07 +00:00
fmt.Println(string(out))
2023-05-20 19:53:56 +00:00
}
}