From d9d0510cfdab30abc72d17789801aa53c901d966 Mon Sep 17 00:00:00 2001 From: Lucas Leadbetter Date: Sun, 20 Dec 2020 12:17:54 -0800 Subject: [PATCH] Misc. fixes. Addressing #2, #3, #4. --- cmd/api.go | 6 +++++- cmd/configure.go | 2 +- cmd/events.go | 4 ++-- docs/api.md | 2 +- internal/api/api.go | 19 ++++++++----------- internal/events/retrigger_event.go | 2 +- internal/events/trigger_event.go | 8 ++++---- internal/request/request_test.go | 12 ++++++++++++ internal/util/path_test.go | 19 +++++++++++++++++++ internal/util/random_test.go | 29 +++++++++++++++++++++++++++++ internal/util/version_test.go | 19 +++++++++++++++++++ 11 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 internal/request/request_test.go create mode 100644 internal/util/path_test.go create mode 100644 internal/util/random_test.go create mode 100644 internal/util/version_test.go diff --git a/cmd/api.go b/cmd/api.go index 0b200ae0..f5f619bf 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -65,8 +65,12 @@ func init() { apiCmd.PersistentFlags().StringArrayVarP(&queryParameters, "query-params", "q", nil, "Available multiple times. Passes in query parameters to endpoints using the format of `key=value`.") apiCmd.PersistentFlags().StringVarP(&body, "body", "b", "", "Passes a body to the request. Alteratively supports CURL-like references to files using the format of `@data,json`.") - // default here is false to enable -p commands to toggle off without explicitly defining -p=false as -p false will not work. The below commands invert the bool to pass the true default + + // default here is false to enable -p commands to toggle off without explicitly defining -p=false as -p false will not work. The below commands invert the bool to pass the true default. Deprecated, so marking as hidden in favor of the unformatted flag. apiCmd.PersistentFlags().BoolVarP(&prettyPrint, "pretty-print", "p", false, "Whether to pretty-print API requests. Default is true.") + apiCmd.PersistentFlags().MarkHidden("pretty-print") + + apiCmd.PersistentFlags().BoolVarP(&prettyPrint, "unformatted", "u", false, "Whether to have API requests come back unformatted/non-prettyprinted. Default is false.") } func cmdRun(cmd *cobra.Command, args []string) { diff --git a/cmd/configure.go b/cmd/configure.go index fb1222c5..8399d507 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -45,7 +45,7 @@ func configureCmdRun(cmd *cobra.Command, args []string) { clientSecretPrompt := promptui.Prompt{ Label: "Client Secret", Validate: func(s string) error { - if len(s) != 30 { + if len(s) == 30 || len(s) == 31 { return errors.New("Invalid length for Client Secret") } return nil diff --git a/cmd/events.go b/cmd/events.go index c236dd49..3e3223b9 100644 --- a/cmd/events.go +++ b/cmd/events.go @@ -87,7 +87,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) { } for i := 0; i < count; i++ { - res, err := trigger.Fire(trigger.TriggerParamaters{ + res, err := trigger.Fire(trigger.TriggerParameters{ Event: args[0], Transport: transport, ForwardAddress: forwardAddress, @@ -108,7 +108,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) { } func retriggerCmdRun(cmd *cobra.Command, args []string) { - res, err := trigger.RefireEvent(eventID, trigger.TriggerParamaters{ + res, err := trigger.RefireEvent(eventID, trigger.TriggerParameters{ ForwardAddress: forwardAddress, Secret: secret, }) diff --git a/docs/api.md b/docs/api.md index 0a932e69..01573885 100644 --- a/docs/api.md +++ b/docs/api.md @@ -32,7 +32,7 @@ Allows the user to make GET calls to endpoints on Helix. Requires a logged in to | Flag | Shorthand | Description | Example | Required? (Y/N) | |------------------|-----------|---------------------------------------------------------------------------------------------------------------|----------------------|-----------------| | `--query-param` | `-q` | Query parameters for the endpoint in `key=value` format. Multiple can be entered to give multiple parameters. | `get -q login=ninja` | N | -| `--pretty-print` | `-p` | Whether to pretty-print API requests. Default is `true`. | `get -p` | N | +| `--unformatted` | `-u` | Whether to return unformatted responses. Default is `false`. | `get -u` | N | **Examples** diff --git a/internal/api/api.go b/internal/api/api.go index 7dabe159..779882a5 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -26,22 +26,22 @@ type clientInformation struct { } // NewRequest is used to request data from the Twitch API using a HTTP GET request- this function is a wrapper for the apiRequest function that handles the network call -func NewRequest(method string, path string, queryParamaters []string, body []byte, prettyPrint bool) { +func NewRequest(method string, path string, queryParameters []string, body []byte, prettyPrint bool) { client, err := getClientInformation() if err != nil { fmt.Println("Error fetching client information", err.Error()) } - paramaters := url.Values{} + Parameters := url.Values{} - if queryParamaters != nil { + if queryParameters != nil { path += "?" - for _, param := range queryParamaters { + for _, param := range queryParameters { value := strings.Split(param, "=") - paramaters.Add(value[0], value[1]) + Parameters.Add(value[0], value[1]) } - path += paramaters.Encode() + path += Parameters.Encode() } resp, err := apiRequest(strings.ToUpper(method), baseURL+path, body, apiRequestParameters{ ClientID: client.ClientID, @@ -80,9 +80,6 @@ func ValidOptions(method string) []string { } } - // for _, endpoint := range names { - // names = append(names, strings.Split(endpoint, "/")...) - // } sort.Strings(names) return names @@ -106,7 +103,7 @@ func getClientInformation() (clientInformation, error) { refreshToken := viper.GetString("refreshToken") if refreshToken == "" { - log.Fatal("Please run github.com/twitchdev/twitch-cli token") + log.Fatal("Please run twitch token") } clientSecret := viper.GetString("clientSecret") @@ -119,7 +116,7 @@ func getClientInformation() (clientInformation, error) { }) if err != nil { - log.Fatal("Unable to refresh token, please rerun github.com/twitchdev/twitch-cli token", err.Error()) + log.Fatal("Unable to refresh token, please rerun twitch token", err.Error()) } } diff --git a/internal/events/retrigger_event.go b/internal/events/retrigger_event.go index 7cf0231b..b6eedbb3 100644 --- a/internal/events/retrigger_event.go +++ b/internal/events/retrigger_event.go @@ -8,7 +8,7 @@ import ( "github.com/twitchdev/twitch-cli/internal/util" ) -func RefireEvent(id string, p TriggerParamaters) (string, error) { +func RefireEvent(id string, p TriggerParameters) (string, error) { res, err := util.GetEventByID(id) if err != nil { return "", err diff --git a/internal/events/trigger_event.go b/internal/events/trigger_event.go index ec354900..0514be42 100644 --- a/internal/events/trigger_event.go +++ b/internal/events/trigger_event.go @@ -11,8 +11,8 @@ import ( "github.com/twitchdev/twitch-cli/internal/util" ) -// TriggerParamaters defines the parameters used to emit an event. -type TriggerParamaters struct { +// TriggerParameters defines the parameters used to emit an event. +type TriggerParameters struct { Event string Transport string IsAnonymous bool @@ -33,8 +33,8 @@ type TriggerResponse struct { Timestamp string } -// Fire emits an event using the TriggerParamaters defined above. -func Fire(p TriggerParamaters) (string, error) { +// Fire emits an event using the TriggerParameters defined above. +func Fire(p TriggerParameters) (string, error) { if len(triggerTypeMap[p.Transport]) == 0 { return "", errors.New("Invalid transport") } diff --git a/internal/request/request_test.go b/internal/request/request_test.go new file mode 100644 index 00000000..40ac7e42 --- /dev/null +++ b/internal/request/request_test.go @@ -0,0 +1,12 @@ +package request + +import ( + "testing" +) + +func TestNewRequest(t *testing.T) { + _, err := NewRequest("GET", "https://api.twitch.tv/helix/users", nil) + if err != nil { + t.Errorf("Received error %v for valid request", err) + } +} diff --git a/internal/util/path_test.go b/internal/util/path_test.go new file mode 100644 index 00000000..ba4fbde6 --- /dev/null +++ b/internal/util/path_test.go @@ -0,0 +1,19 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package util + +import ( + "strings" + "testing" +) + +func TestGetApplicationDir(t *testing.T) { + dir, err := GetApplicationDir() + if err != nil { + t.Errorf("GetApplicationDir() failed with error %v", err) + } + + if strings.HasSuffix(dir, ".twitch-cli") != true { + t.Errorf("GetApplicationDir() expected to end with %v, got %v", ".twitch-cli", dir) + } +} diff --git a/internal/util/random_test.go b/internal/util/random_test.go new file mode 100644 index 00000000..f1fb705b --- /dev/null +++ b/internal/util/random_test.go @@ -0,0 +1,29 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package util + +import ( + "regexp" + "testing" +) + +func TestRandomUserId(t *testing.T) { + userID := RandomUserID() + + if len(userID) == 0 { + t.Errorf("RandomUserID() returned string with a length of 0") + } +} + +func TestRandomGUID(t *testing.T) { + guid := RandomGUID() + if len(guid) == 0 { + t.Errorf("RandomGUID() returned string with a length of 0") + } + + r, _ := regexp.Compile("^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$") + + if r.MatchString(guid) != true { + t.Errorf("RandomGUID() returned a string with value %v, which does not meet the GUID pattern", guid) + } +} diff --git a/internal/util/version_test.go b/internal/util/version_test.go new file mode 100644 index 00000000..bdefe401 --- /dev/null +++ b/internal/util/version_test.go @@ -0,0 +1,19 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package util + +import ( + "testing" +) + +func TestVersion(t *testing.T) { + var testString = "test_version" + + SetVersion(testString) + + v := GetVersion() + + if v != testString { + t.Errorf("Version failed, set version to %v, received %v", testString, v) + } +}