-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from twitchdev/feat/eventsub-configure
event configure command
- Loading branch information
Showing
6 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package configure_event | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/twitchdev/twitch-cli/internal/util" | ||
) | ||
|
||
type EventConfigurationParams struct { | ||
Secret string | ||
ForwardAddress string | ||
} | ||
|
||
func ConfigureEvents(p EventConfigurationParams) error { | ||
var err error | ||
if p.ForwardAddress == "" && p.Secret == "" { | ||
return fmt.Errorf("you must provide at least one of --secret or --forward-address") | ||
} | ||
|
||
// Validate that the forward address is actually a URL | ||
if len(p.ForwardAddress) > 0 { | ||
_, err := url.ParseRequestURI(p.ForwardAddress) | ||
if err != nil { | ||
return err | ||
} | ||
viper.Set("forwardAddress", p.ForwardAddress) | ||
} | ||
if p.Secret != "" { | ||
if len(p.Secret) < 10 || len(p.Secret) > 100 { | ||
return fmt.Errorf("invalid secret provided. Secrets must be between 10-100 characters") | ||
} | ||
viper.Set("eventSecret", p.Secret) | ||
} | ||
|
||
configPath, err := util.GetConfigPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := viper.WriteConfigAs(configPath); err != nil { | ||
return fmt.Errorf("failed to write configuration: %v", err.Error()) | ||
} | ||
|
||
fmt.Println("Updated configuration.") | ||
return nil | ||
} | ||
|
||
func GetEventConfiguration(noConfig bool) EventConfigurationParams { | ||
if noConfig { | ||
return EventConfigurationParams{} | ||
} | ||
return EventConfigurationParams{ | ||
ForwardAddress: viper.GetString("forwardAddress"), | ||
Secret: viper.GetString("eventSecret"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package configure_event_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
configure_event "github.com/twitchdev/twitch-cli/internal/events/configure" | ||
"github.com/twitchdev/twitch-cli/test_setup" | ||
) | ||
|
||
func TestWriteEventConfig(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
defaultForwardAddress := "http://localhost:3000/" | ||
defaultSecret := "12345678910" | ||
test_config := configure_event.EventConfigurationParams{ | ||
ForwardAddress: defaultForwardAddress, | ||
Secret: defaultSecret, | ||
} | ||
|
||
// test a good config writes correctly | ||
a.NoError(configure_event.ConfigureEvents(test_config)) | ||
|
||
a.Equal(defaultForwardAddress, viper.Get("forwardAddress")) | ||
a.Equal(defaultSecret, viper.Get("eventSecret")) | ||
|
||
// test for secret length validation | ||
test_config.Secret = "1" | ||
a.Error(configure_event.ConfigureEvents(test_config)) | ||
a.NotEqual("1", viper.Get("eventSecret")) | ||
test_config.Secret = defaultSecret | ||
|
||
// test for forward address validation | ||
test_config.ForwardAddress = "not a url" | ||
a.Error(configure_event.ConfigureEvents(test_config)) | ||
a.NotEqual("not a url", viper.Get("forwardAddress")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters