2019-12-08 21:44:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-12-10 22:25:41 +00:00
|
|
|
"sort"
|
2019-12-08 21:44:12 +00:00
|
|
|
|
|
|
|
"github.com/google/subcommands"
|
|
|
|
"within.website/x/writer"
|
|
|
|
)
|
|
|
|
|
2019-12-10 22:25:41 +00:00
|
|
|
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")
|
|
|
|
}
|
2019-12-08 21:44:12 +00:00
|
|
|
|
2019-12-10 22:25:41 +00:00
|
|
|
func (handlerLogsCmd) Name() string { return "logs" }
|
|
|
|
func (handlerLogsCmd) Synopsis() string { return "shows logs for a handler" }
|
2019-12-08 21:44:12 +00:00
|
|
|
func (handlerLogsCmd) Usage() string {
|
|
|
|
return `wasmcloud logs <handler-name>
|
|
|
|
|
|
|
|
$ wasmcloud logs four-of-aether-60037
|
|
|
|
|
|
|
|
Returns all of the logs for a handler.
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:25:41 +00:00
|
|
|
func (h handlerLogsCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
2019-12-08 21:44:12 +00:00
|
|
|
if fs.NArg() != 1 {
|
|
|
|
fmt.Println("usage: wasmcloud create [options] <filename>")
|
|
|
|
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))
|
2019-12-10 22:25:41 +00:00
|
|
|
|
|
|
|
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"]))
|
2019-12-08 21:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return subcommands.ExitSuccess
|
|
|
|
}
|