show deleted handlers in list

This commit is contained in:
Cadey Ratio 2019-12-13 11:28:56 +00:00
parent 6ac270463f
commit 49c1f3b766
2 changed files with 38 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"strconv"
"time" "time"
"github.com/google/subcommands" "github.com/google/subcommands"
@ -16,11 +17,13 @@ import (
) )
type handlerListCmd struct { type handlerListCmd struct {
format string format string
showDeleted bool
} }
func (h *handlerListCmd) SetFlags(fs *flag.FlagSet) { func (h *handlerListCmd) SetFlags(fs *flag.FlagSet) {
fs.StringVar(&h.format, "format", "table", "what format to present output in (table|json)") 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" } func (handlerListCmd) Name() string { return "list" }
@ -37,7 +40,7 @@ Flags:`
} }
func (h handlerListCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -60,10 +63,11 @@ func (h handlerListCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...inte
return subcommands.ExitSuccess return subcommands.ExitSuccess
case "table": case "table":
type apiResp struct { type apiResp struct {
CreatedAt time.Time `json:"CreatedAt"` CreatedAt time.Time `json:"CreatedAt"`
UpdatedAt time.Time `json:"UpdatedAt"` UpdatedAt time.Time `json:"UpdatedAt"`
Name string `json:"Name"` DeletedAt *time.Time `json:"DeletedAt"`
CID string `json:"Path"` Name string `json:"Name"`
CID string `json:"Path"`
} }
var result []apiResp var result []apiResp
err = json.NewDecoder(resp.Body).Decode(&result) 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 := 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 { for _, hdlr := range result {
table.AddRow( if h.showDeleted {
hdlr.Name, table.AddRow(
hdlr.CreatedAt.Format(time.RFC3339), hdlr.Name,
hdlr.UpdatedAt.Format(time.RFC3339), hdlr.CreatedAt.Format(time.RFC3339),
hdlr.CID, 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()) fmt.Println(table.String())

View File

@ -103,9 +103,16 @@ func createHandler(w http.ResponseWriter, r *http.Request, u *User) {
func listHandlers(w http.ResponseWriter, r *http.Request, u *User) { func listHandlers(w http.ResponseWriter, r *http.Request, u *User) {
ctx := r.Context() ctx := r.Context()
ctx = opname.With(ctx, "read-handlers") ctx = opname.With(ctx, "read-handlers")
myDB := db
if q := r.URL.Query(); q.Get("even-deleted") == "true" {
myDB = db.Unscoped()
}
var hdlrs []Handler 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 { if err != nil {
ln.Error(ctx, err) ln.Error(ctx, err)
http.Error(w, "can't read handlers", http.StatusInternalServerError) http.Error(w, "can't read handlers", http.StatusInternalServerError)