Skip to content

Commit 49bb54f

Browse files
feat: Add support for Hookdeck Console (#42)
* feat: Add support for Hookdeck Console * fix: Fix comment * fix: Remove innacurate comment
1 parent 35c8780 commit 49bb54f

File tree

12 files changed

+167
-22
lines changed

12 files changed

+167
-22
lines changed

pkg/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func init() {
100100
// Hidden configuration flags, useful for dev/debugging
101101
rootCmd.PersistentFlags().StringVar(&Config.APIBaseURL, "api-base", hookdeck.DefaultAPIBaseURL, "Sets the API base URL")
102102
rootCmd.PersistentFlags().StringVar(&Config.DashboardBaseURL, "dashboard-base", hookdeck.DefaultDashboardBaseURL, "Sets the web dashboard base URL")
103+
rootCmd.PersistentFlags().StringVar(&Config.ConsoleBaseURL, "console-base", hookdeck.DefaultConsoleBaseURL, "Sets the web console base URL")
103104

104105
rootCmd.Flags().BoolP("version", "v", false, "Get the version of the Hookdeck CLI")
105106

pkg/cmd/whoami.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/spf13/cobra"
8-
97
"github.com/hookdeck/hookdeck-cli/pkg/ansi"
8+
"github.com/hookdeck/hookdeck-cli/pkg/login"
109
"github.com/hookdeck/hookdeck-cli/pkg/validators"
10+
"github.com/spf13/cobra"
1111
)
1212

1313
type whoamiCmd struct {
@@ -22,22 +22,28 @@ func newWhoamiCmd() *whoamiCmd {
2222
Use: "whoami",
2323
Args: validators.NoArgs,
2424
Short: "Show the logged-in user",
25-
RunE: lc.runWhoamiCmd,
25+
RunE: lc.runWhoamiCmd,
2626
}
2727

2828
return lc
2929
}
3030

3131
func (lc *whoamiCmd) runWhoamiCmd(cmd *cobra.Command, args []string) error {
32-
displayName := Config.Profile.GetDisplayName()
33-
teamName := Config.Profile.GetTeamName()
32+
key, err := Config.Profile.GetAPIKey()
33+
if err != nil {
34+
return err
35+
}
36+
response, err := login.ValidateKey(Config.APIBaseURL, key)
37+
if err != nil {
38+
return err
39+
}
3440

3541
color := ansi.Color(os.Stdout)
3642

3743
fmt.Printf(
3844
"Logged in as %s in workspace %s\n",
39-
color.Bold(displayName),
40-
color.Bold(teamName),
45+
color.Bold(response.UserName),
46+
color.Bold(response.TeamName),
4147
)
4248

4349
return nil

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Config struct {
3737
ProfilesFile string
3838
APIBaseURL string
3939
DashboardBaseURL string
40+
ConsoleBaseURL string
4041
Insecure bool
4142
}
4243

pkg/config/profile.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Profile struct {
2222
DeviceName string
2323
ProfileName string
2424
TeamName string
25+
TeamID string
26+
TeamMode string
2527
APIKey string
2628
ClientID string
2729
DisplayName string
@@ -124,6 +126,28 @@ func (p *Profile) GetDisplayName() string {
124126
return ""
125127
}
126128

129+
func (p *Profile) GetTeamMode() string {
130+
if p.TeamMode != "" {
131+
return p.TeamMode
132+
}
133+
if err := viper.ReadInConfig(); err == nil {
134+
return viper.GetString(p.GetConfigField("team_mode"))
135+
}
136+
137+
return ""
138+
}
139+
140+
func (p *Profile) GetTeamId() string {
141+
if p.TeamID != "" {
142+
return p.TeamID
143+
}
144+
if err := viper.ReadInConfig(); err == nil {
145+
return viper.GetString(p.GetConfigField("team_id"))
146+
}
147+
148+
return ""
149+
}
150+
127151
func (p *Profile) refreshTeamName() string {
128152
apiKey, err := p.GetAPIKey()
129153
if err != nil {
@@ -248,6 +272,14 @@ func (p *Profile) writeProfile(runtimeViper *viper.Viper) error {
248272
runtimeViper.Set(p.GetConfigField("team_name"), strings.TrimSpace(p.TeamName))
249273
}
250274

275+
if p.TeamID != "" {
276+
runtimeViper.Set(p.GetConfigField("team_id"), strings.TrimSpace(p.TeamID))
277+
}
278+
279+
if p.TeamMode != "" {
280+
runtimeViper.Set(p.GetConfigField("team_mode"), strings.TrimSpace(p.TeamMode))
281+
}
282+
251283
runtimeViper.MergeInConfig()
252284

253285
runtimeViper.SetConfigFile(profilesFile)

pkg/hookdeck/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const DefaultDashboardURL = "https://dashboard.hookdeck.com"
2424
// DefaultDashboardBaseURL is the default base URL for dashboard requests
2525
const DefaultDashboardBaseURL = "http://dashboard.hookdeck.com"
2626

27+
const DefaultConsoleBaseURL = "http://console.hookdeck.com"
28+
2729
const DefaultWebsocektURL = "wss://ws.hookdeck.com"
2830

2931
// Client is the API client used to sent requests to Hookdeck.

pkg/listen/listen.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,21 @@ func Listen(URL *url.URL, source_alias string, connection_query string, flags Fl
8282
fmt.Println()
8383
fmt.Println(ansi.Bold("Dashboard"))
8484
if guest_url != "" {
85-
fmt.Println("👤 Login URL: " + guest_url)
86-
fmt.Println("Sign up in the dashboard to make your webhook URL permanent.")
85+
fmt.Println("👤 Console URL: " + guest_url)
86+
fmt.Println("Sign up in the Console to make your webhook URL permanent.")
87+
fmt.Println()
88+
} else {
89+
var url = config.DashboardBaseURL
90+
if config.Profile.GetTeamId() != "" {
91+
url += "?team_id=" + config.Profile.GetTeamId()
92+
}
93+
if config.Profile.GetTeamMode() == "console" {
94+
url = config.ConsoleBaseURL + "?source_id=" + source.Id
95+
}
96+
fmt.Println("👉 Inspect and replay webhooks: " + url)
8797
fmt.Println()
8898
}
8999

90-
fmt.Printf("👉 Inspect and replay webhooks: %s/cli/events\n", config.DashboardBaseURL)
91-
fmt.Println()
92-
93100
fmt.Println(ansi.Bold(source.Label + " Source"))
94101
fmt.Println("🔌 Webhook URL: " + source.Url)
95102
fmt.Println()
@@ -110,6 +117,8 @@ func Listen(URL *url.URL, source_alias string, connection_query string, flags Fl
110117
Key: key,
111118
APIBaseURL: config.APIBaseURL,
112119
DashboardBaseURL: config.DashboardBaseURL,
120+
ConsoleBaseURL: config.ConsoleBaseURL,
121+
Profile: config.Profile,
113122
WSBaseURL: flags.WSBaseURL,
114123
NoWSS: flags.NoWSS,
115124
URL: URL,

pkg/login/client_login.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,37 @@ type Links struct {
3232

3333
// Login function is used to obtain credentials via hookdeck dashboard.
3434
func Login(config *config.Config, input io.Reader) error {
35+
var s *spinner.Spinner
36+
37+
if config.Profile.APIKey != "" {
38+
s = ansi.StartNewSpinner("Verifying CLI Key...", os.Stdout)
39+
response, err := ValidateKey(config.APIBaseURL, config.Profile.APIKey)
40+
if err != nil {
41+
return err
42+
}
43+
44+
config.Profile.ClientID = response.ClientID
45+
config.Profile.DisplayName = response.UserName
46+
config.Profile.TeamName = response.TeamName
47+
config.Profile.TeamMode = response.TeamMode
48+
config.Profile.TeamID = response.TeamID
49+
50+
profileErr := config.Profile.CreateProfile()
51+
if profileErr != nil {
52+
return profileErr
53+
}
54+
55+
message := SuccessMessage(response.UserName, response.TeamName, config.Profile.TeamMode == "console")
56+
ansi.StopSpinner(s, message, os.Stdout)
57+
58+
return nil
59+
}
60+
3561
links, err := getLinks(config.APIBaseURL, config.Profile.DeviceName)
3662
if err != nil {
3763
return err
3864
}
3965

40-
var s *spinner.Spinner
41-
4266
if isSSH() || !canOpenBrowser() {
4367
fmt.Printf("To authenticate with Hookdeck, please go to: %s\n", links.BrowserURL)
4468

@@ -72,13 +96,15 @@ func Login(config *config.Config, input io.Reader) error {
7296
config.Profile.ClientID = response.ClientID
7397
config.Profile.DisplayName = response.UserName
7498
config.Profile.TeamName = response.TeamName
99+
config.Profile.TeamMode = response.TeamMode
100+
config.Profile.TeamID = response.TeamID
75101

76102
profileErr := config.Profile.CreateProfile()
77103
if profileErr != nil {
78104
return profileErr
79105
}
80106

81-
message := SuccessMessage(response.UserName, response.TeamName)
107+
message := SuccessMessage(response.UserName, response.TeamName, response.TeamMode == "console")
82108
ansi.StopSpinner(s, message, os.Stdout)
83109

84110
return nil
@@ -118,6 +144,8 @@ func GuestLogin(config *config.Config) (string, error) {
118144
config.Profile.ClientID = response.ClientID
119145
config.Profile.DisplayName = response.UserName
120146
config.Profile.TeamName = response.TeamName
147+
config.Profile.TeamMode = response.TeamMode
148+
config.Profile.TeamID = response.TeamID
121149

122150
profileErr := config.Profile.CreateProfile()
123151
if profileErr != nil {

pkg/login/interactive_login.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ func InteractiveLogin(config *config.Config) error {
6666
config.Profile.ClientID = response.ClientID
6767
config.Profile.DisplayName = response.UserName
6868
config.Profile.TeamName = response.TeamName
69+
config.Profile.TeamMode = response.TeamMode
70+
config.Profile.TeamID = response.TeamID
6971

7072
profileErr := config.Profile.CreateProfile()
7173
if profileErr != nil {
7274
ansi.StopSpinner(s, "", os.Stdout)
7375
return profileErr
7476
}
7577

76-
message := SuccessMessage(response.UserName, response.TeamName)
78+
message := SuccessMessage(response.UserName, response.TeamName, response.TeamMode == "console")
7779

7880
ansi.StopSpinner(s, message, os.Stdout)
7981

pkg/login/login_message.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ import (
88
)
99

1010
// SuccessMessage returns the display message for a successfully authenticated user
11-
func SuccessMessage(displayName string, teamName string) string {
11+
func SuccessMessage(displayName string, teamName string, isConsole bool) string {
1212
color := ansi.Color(os.Stdout)
13+
14+
if isConsole == true {
15+
return fmt.Sprintf(
16+
"Done! The Hookdeck CLI is configured with your console Sandbox",
17+
)
18+
}
1319
return fmt.Sprintf(
1420
"Done! The Hookdeck CLI is configured for %s in workspace %s\n",
1521
color.Bold(displayName),

pkg/login/poll.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type PollAPIKeyResponse struct {
2121
UserName string `json:"user_name"`
2222
TeamID string `json:"team_id"`
2323
TeamName string `json:"team_name"`
24+
TeamMode string `json:"team_mode"`
2425
APIKey string `json:"key"`
2526
ClientID string `json:"client_id"`
2627
}

0 commit comments

Comments
 (0)