From 49c1f3b7666f4b88bf513eddfbb4f48e410a5a47 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 13 Dec 2019 11:28:56 +0000 Subject: [PATCH] show deleted handlers in list --- cmd/wasmcloud/handler_list.go | 43 ++++++++++++++++++++++++----------- cmd/wasmcloudd/handler.go | 9 +++++++- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/cmd/wasmcloud/handler_list.go b/cmd/wasmcloud/handler_list.go index db678e8..807fe8a 100644 --- a/cmd/wasmcloud/handler_list.go +++ b/cmd/wasmcloud/handler_list.go @@ -9,6 +9,7 @@ import ( "log" "net/http" "os" + "strconv" "time" "github.com/google/subcommands" @@ -16,11 +17,13 @@ import ( ) type handlerListCmd struct { - format string + format string + showDeleted bool } func (h *handlerListCmd) SetFlags(fs *flag.FlagSet) { fs.StringVar(&h.format, "format", "table", "what format to present output in (table|json)") + fs.BoolVar(&h.showDeleted, "show-deleted", false, "if set, show deleted handlers") } func (handlerListCmd) Name() string { return "list" } @@ -37,7 +40,7 @@ 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) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, *apiServer+"/api/handler?even-deleted="+strconv.FormatBool(h.showDeleted), nil) if err != nil { log.Fatal(err) } @@ -60,10 +63,11 @@ func (h handlerListCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...inte 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"` + CreatedAt time.Time `json:"CreatedAt"` + UpdatedAt time.Time `json:"UpdatedAt"` + DeletedAt *time.Time `json:"DeletedAt"` + Name string `json:"Name"` + CID string `json:"Path"` } var result []apiResp err = json.NewDecoder(resp.Body).Decode(&result) @@ -72,15 +76,28 @@ func (h handlerListCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...inte } table := uitable.New() - table.AddRow("Name", "Created at", "Updated at", "CID") + if h.showDeleted { + table.AddRow("Name", "Created at", "Deleted at", "CID") + } else { + 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, - ) + if h.showDeleted { + table.AddRow( + hdlr.Name, + hdlr.CreatedAt.Format(time.RFC3339), + hdlr.DeletedAt.Format(time.RFC3339), + hdlr.CID, + ) + } else { + table.AddRow( + hdlr.Name, + hdlr.CreatedAt.Format(time.RFC3339), + hdlr.UpdatedAt.Format(time.RFC3339), + hdlr.CID, + ) + } } fmt.Println(table.String()) diff --git a/cmd/wasmcloudd/handler.go b/cmd/wasmcloudd/handler.go index 2521472..7a2c11c 100644 --- a/cmd/wasmcloudd/handler.go +++ b/cmd/wasmcloudd/handler.go @@ -103,9 +103,16 @@ func createHandler(w http.ResponseWriter, r *http.Request, u *User) { func listHandlers(w http.ResponseWriter, r *http.Request, u *User) { ctx := r.Context() ctx = opname.With(ctx, "read-handlers") + + myDB := db + + if q := r.URL.Query(); q.Get("even-deleted") == "true" { + myDB = db.Unscoped() + } + var hdlrs []Handler - err := db.Where("user_id = ?", u.ID).Find(&hdlrs).Error + err := myDB.Where("user_id = ?", u.ID).Find(&hdlrs).Error if err != nil { ln.Error(ctx, err) http.Error(w, "can't read handlers", http.StatusInternalServerError)