-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_picker.go
More file actions
56 lines (47 loc) · 1.73 KB
/
Copy pathrequest_picker.go
File metadata and controls
56 lines (47 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cmd
import (
"context"
"errors"
"fmt"
"github.com/aaearon/grant-cli/internal/ui"
"github.com/aaearon/grant-cli/internal/workflows"
)
// earlyNonInteractiveCheck fails fast before bootstrap when no requestID is
// provided and stdin is not a TTY.
func earlyNonInteractiveCheck(requestID string) error {
if requestID == "" && !ui.IsInteractive() {
return fmt.Errorf("%w; pass the request ID as a positional argument (run `grant request list` to find it)", ui.ErrNotInteractive)
}
return nil
}
// pickerScope describes how to scope the list of requests surfaced in the picker.
type pickerScope struct {
filter string // OData filter; empty = no filter
requestRole string // "CREATOR" | "APPROVER" | ""
emptyMsg string // e.g. "open requests you created"
}
// resolveRequestIDFn is injectable for testing.
var resolveRequestIDFn = resolveRequestIDInteractive
// resolveRequestIDInteractive fetches a scoped list of access requests and
// shows the interactive picker, returning the chosen request ID.
func resolveRequestIDInteractive(ctx context.Context, svc accessRequestService, scope pickerScope) (string, error) {
if !ui.IsInteractive() {
return "", fmt.Errorf("%w; pass the request ID as a positional argument (run `grant request list` to find it)", ui.ErrNotInteractive)
}
items, _, err := svc.ListRequests(ctx, workflows.ListRequestsParams{
Filter: scope.filter,
RequestRole: scope.requestRole,
Sort: "createdAt desc",
})
if err != nil {
return "", fmt.Errorf("failed to list requests: %w", err)
}
if len(items) == 0 {
return "", errors.New("no " + scope.emptyMsg + "; nothing to act on")
}
chosen, err := ui.SelectRequest(items)
if err != nil {
return "", err
}
return chosen.RequestID, nil
}