-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathoutpost.go
More file actions
118 lines (99 loc) · 3.85 KB
/
Copy pathoutpost.go
File metadata and controls
118 lines (99 loc) · 3.85 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/hookdeck/hookdeck-cli/pkg/config"
"github.com/hookdeck/hookdeck-cli/pkg/validators"
)
type outpostCmd struct {
cmd *cobra.Command
}
// isOutpostMCPLeafCommand reports whether cmd is the outpost mcp subcommand.
// MCP speaks JSON-RPC on stdout, so when there is no API key yet the project
// check must not run — the server needs to start and expose its login tool.
func isOutpostMCPLeafCommand(cmd *cobra.Command) bool {
return cmd != nil && cmd.Name() == "mcp" && cmd.Parent() != nil && cmd.Parent().Name() == "outpost"
}
// outpostPersistentPreRunE runs before every outpost subcommand. Cobra does not
// chain PersistentPreRun, so initTelemetry must be called here explicitly.
func outpostPersistentPreRunE(cmd *cobra.Command, args []string) error {
initTelemetry(cmd)
if isOutpostMCPLeafCommand(cmd) {
if err := Config.Profile.ValidateAPIKey(); err != nil {
return nil
}
}
return requireOutpostProject(nil)
}
// requireOutpostProject ensures the active project is an Outpost project.
//
// Without this the API answers 404 for a Gateway project, which reads as "the
// resource does not exist" rather than "you are pointed at the wrong project".
// cfg is optional; when nil the global Config is used.
func requireOutpostProject(cfg *config.Config) error {
if cfg == nil {
cfg = &Config
}
if err := cfg.Profile.ValidateAPIKey(); err != nil {
return err
}
if cfg.Profile.ProjectId == "" {
return fmt.Errorf("no project selected. Run 'hookdeck project use' to select a project")
}
projectType := cfg.Profile.ProjectType
if projectType == "" && cfg.Profile.ProjectMode != "" {
projectType = config.ModeToProjectType(cfg.Profile.ProjectMode)
}
if projectType == "" {
// Resolve from the API, which is authoritative for the key.
response, err := cfg.GetAPIClient().ValidateAPIKey()
if err != nil {
return err
}
cfg.Profile.ApplyValidateAPIKeyResponse(response, false)
projectType = cfg.Profile.ProjectType
_ = cfg.Profile.SaveProfile()
}
if !config.IsOutpostProject(projectType) {
return fmt.Errorf("this command requires an Outpost project; current project type is %s. Use 'hookdeck project use' to switch to an Outpost project", projectType)
}
return nil
}
func newOutpostCmd() *outpostCmd {
oc := &outpostCmd{}
oc.cmd = &cobra.Command{
Use: "outpost",
Args: validators.NoArgs,
Short: ShortBeta("Manage your Hookdeck Outpost resources"),
Long: LongBeta(`Commands for managing Hookdeck Outpost tenants, destinations, events,
attempts, topics, metrics, and project configuration.
Outpost delivers events to your users' destinations. Each of your users is a tenant,
and each tenant owns the destinations their events are delivered to.
These commands require an Outpost project. Use 'hookdeck project use' to switch.`),
Example: ` # List tenants
hookdeck outpost tenant list
# Create a webhook destination for a tenant
hookdeck outpost destination create --tenant-id acme --type webhook --config-url https://example.com/hooks
# Inspect recent events
hookdeck outpost event list --limit 10
# Check the deployment status
hookdeck outpost status`,
PersistentPreRunE: outpostPersistentPreRunE,
}
oc.cmd.AddCommand(newOutpostTenantCmd().cmd)
oc.cmd.AddCommand(newOutpostDestinationCmd().cmd)
oc.cmd.AddCommand(newOutpostDestinationTypeCmd().cmd)
oc.cmd.AddCommand(newOutpostEventCmd().cmd)
oc.cmd.AddCommand(newOutpostAttemptCmd().cmd)
oc.cmd.AddCommand(newOutpostTopicCmd().cmd)
oc.cmd.AddCommand(newOutpostStatusCmd().cmd)
oc.cmd.AddCommand(newOutpostPublishCmd().cmd)
oc.cmd.AddCommand(newOutpostMetricsCmd().cmd)
oc.cmd.AddCommand(newOutpostConfigCmd().cmd)
addOutpostMCPCmdTo(oc.cmd)
return oc
}
// addOutpostCmdTo registers the outpost command tree on the given parent.
func addOutpostCmdTo(parent *cobra.Command) {
parent.AddCommand(newOutpostCmd().cmd)
}