91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/google/subcommands"
|
|
"github.com/gosuri/uitable"
|
|
)
|
|
|
|
type handlerListCmd struct {
|
|
format string
|
|
}
|
|
|
|
func (h *handlerListCmd) SetFlags(fs *flag.FlagSet) {
|
|
fs.StringVar(&h.format, "format", "table", "what format to present output in (table|json)")
|
|
}
|
|
|
|
func (handlerListCmd) Name() string { return "list" }
|
|
func (handlerListCmd) Synopsis() string { return "lists handlers you've created" }
|
|
func (handlerListCmd) Usage() string {
|
|
return `wasmcloud list [options]
|
|
|
|
$ wasmcloud list
|
|
$ wasmcloud list -format json
|
|
|
|
Shows all of the handlers you have registered with wasmcloud.
|
|
|
|
Flags:`
|
|
}
|
|
|
|
func (h handlerListCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, *apiServer+"/api/handler", nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
withAPI(req)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
io.Copy(os.Stdout, resp.Body)
|
|
return subcommands.ExitFailure
|
|
}
|
|
|
|
switch h.format {
|
|
case "json":
|
|
io.Copy(os.Stdout, resp.Body)
|
|
return subcommands.ExitSuccess
|
|
case "table":
|
|
type apiResp struct {
|
|
CreatedAt time.Time `json:"CreatedAt"`
|
|
UpdatedAt time.Time `json:"UpdatedAt"`
|
|
Name string `json:"Name"`
|
|
CID string `json:"Path"`
|
|
}
|
|
var result []apiResp
|
|
err = json.NewDecoder(resp.Body).Decode(&result)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
table := uitable.New()
|
|
table.AddRow("Name", "Created at", "Updated at", "CID")
|
|
|
|
for _, hdlr := range result {
|
|
table.AddRow(
|
|
hdlr.Name,
|
|
hdlr.CreatedAt.Format(time.RFC3339),
|
|
hdlr.UpdatedAt.Format(time.RFC3339),
|
|
hdlr.CID,
|
|
)
|
|
}
|
|
|
|
fmt.Println(table.String())
|
|
}
|
|
|
|
return subcommands.ExitSuccess
|
|
}
|