Skip to content

Commit

Permalink
feat: Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmichaelchen committed Sep 13, 2023
1 parent a6d8841 commit 3b92e0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 5 additions & 1 deletion internal/cli/cli_sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -38,7 +39,10 @@ func fnSync(cmd *cobra.Command, args []string) {
}

req := buildNewSelectPermissions(table, source, rule)
err := executeRequest(req)
err := executeRequest(req,
fmt.Sprintf("create-%s-%s-%s", source, table, rule.Role),
true,
)
if err != nil {
log.Fatal("unable to create select rule", "err", err)
}
Expand Down
23 changes: 22 additions & 1 deletion internal/cli/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ type TableName struct {
Schema string `json:"schema"`
}

func executeRequest(payload any) error {
type Response struct {
Error string `json:"error"`
Path string `json:"path"`
Code string `json:"code"`
Message string `json:"message"`
}

func executeRequest(payload any, operationName string, logErrors bool) error {
client := &http.Client{}

body, err := json.Marshal(payload)
Expand Down Expand Up @@ -49,5 +56,19 @@ func executeRequest(payload any) error {

log.Debug("received response", "body", string(resb))

var response Response
err = json.Unmarshal(resb, &response)
if err != nil {
return fmt.Errorf("unable to unmarshal response body: %w", err)
}

if response.Message == "success" {
log.Info("Successfully executed operation", "operation", operationName)
}

if logErrors && response.Code != "" {
log.Errorf("Received %s error for path (%s): %s", response.Code, response.Path, response.Error)
}

return nil
}
10 changes: 8 additions & 2 deletions internal/cli/http_drop.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cli

import "github.com/charmbracelet/log"
import (
"fmt"
"github.com/charmbracelet/log"
)

type DropPermissionRequest struct {
Type string `json:"type"`
Expand Down Expand Up @@ -31,7 +34,10 @@ func dropPermission(typeStr string, table TableName, role, source string) {
}),
}

err := executeRequest(r)
err := executeRequest(r,
fmt.Sprintf("drop-%s-%s-%s", source, table, role),
false,
)

// idempotence means not aborting if the permission has already been dropped
if err != nil {
Expand Down

0 comments on commit 3b92e0b

Please sign in to comment.