package cli import ( "encoding/json" "errors" "fmt" "log" "net/http" ) func (h *abraHandler) handleGetAppSecrets(w http.ResponseWriter, r *http.Request, appName string) { log.Printf("handling secrets!") secrets, err := h.cmd.Run("abra", "app", "secret", "list", appName, "-m") if err != nil { log.Printf("??") InternalServerError(w, err) return } resp := []struct { Created *string `json:"created on server"` GenName *string `json:"generated name"` Name *string `json:"name"` Version *string `json:"version"` }{} err = json.Unmarshal(secrets, &resp) if err != nil { log.Printf("???") InternalServerError(w, err) return } fs := []AppSecret{} for _, secret := range resp { if secret.Created == nil { InternalServerError(w, errors.New("Failed to decode field 'created on server' from abra response")) return } if secret.Name == nil { InternalServerError(w, errors.New("Failed to decode field 'name' from abra response")) return } if secret.Version == nil { InternalServerError(w, errors.New("Failed to decode field 'version' from abra response")) return } fs = append(fs, AppSecret{ Name: *secret.Name, Version: *secret.Version, Created: *secret.Created == "true", Value: "", }) } log.Printf("%v", fs) jsonBytes, err := json.Marshal(fs) if err != nil { InternalServerError(w, err) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(jsonBytes) } func (h *abraHandler) handleInsertAppSecret(w http.ResponseWriter, r *http.Request, appName string) { d := json.NewDecoder(r.Body) d.DisallowUnknownFields() // catch unwanted fields // anonymous struct type: handy for one-time use body := struct { Name *string `json:"name"` Version *string `json:"version"` Value *string `json:"value"` }{} err := d.Decode(&body) if err != nil { log.Printf("%v", err) BadRequest(w, err) return } if body.Name == nil { BadRequest(w, errors.New("missing field 'name' from JSON object")) return } if body.Version == nil { BadRequest(w, errors.New("missing field 'version' from JSON object")) return } if body.Value == nil { BadRequest(w, errors.New("missing field 'value' from JSON object")) return } log.Printf("%s %s %s", *body.Name, *body.Version, *body.Value) output, err := h.cmd.Run("abra", "app", "secret", "insert", appName, *body.Name, *body.Version, *body.Value) if err != nil { InternalServerError(w, fmt.Errorf("Error: %s", string(output))) return } w.WriteHeader(http.StatusOK) }