package main import ( "context" "encoding/json" "flag" "fmt" "io" "log" "net/http" "os" "sort" "github.com/google/subcommands" "within.website/x/writer" ) type handlerLogsCmd struct { rawBundle bool } func (h *handlerLogsCmd) SetFlags(fs *flag.FlagSet) { fs.BoolVar(&h.rawBundle, "raw-bundle", false, "if set, dump the entire logbundle") } func (handlerLogsCmd) Name() string { return "logs" } func (handlerLogsCmd) Synopsis() string { return "shows logs for a handler" } func (handlerLogsCmd) Usage() string { return `wasmcloud logs $ wasmcloud logs four-of-aether-60037 Returns all of the logs for a handler. Flags: ` } func (h handlerLogsCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { if fs.NArg() != 1 { fmt.Println("usage: wasmcloud logs [options] ") return subcommands.ExitUsageError } hname := fs.Arg(0) req, err := http.NewRequestWithContext(ctx, http.MethodGet, *apiServer+"/api/handler/logs?name="+hname, nil) if err != nil { log.Fatal(err) } withAPI(req) resp, err := http.DefaultClient.Do(req) if err != nil { log.Fatal(err) } if resp.StatusCode != http.StatusOK { io.Copy(os.Stdout, resp.Body) return subcommands.ExitFailure } type resultLine struct { ExecID string `json:"exec_id"` Comment string `json:"comment"` Logs map[string]string `json:"logs"` } var result []resultLine err = json.NewDecoder(resp.Body).Decode(&result) if err != nil { log.Fatal(err) } for _, line := range result { wr := writer.LineSplitting(writer.PrefixWriter(line.ExecID[:5]+" | ", os.Stdout)) if h.rawBundle { var keys []string for k := range line.Logs { keys = append(keys, k) } sort.Strings(keys) for _, k := range keys { wr.Write([]byte("-- " + k + " --")) wr.Write([]byte(line.Logs[k])) } continue } wr.Write([]byte(line.Logs["logs.txt"])) } return subcommands.ExitSuccess }