Skip to content

Commit

Permalink
Run gofmt on all files
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Nov 14, 2019
1 parent 751af79 commit de29b14
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 131 deletions.
54 changes: 27 additions & 27 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
package main

import (
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"reflect"
"net/http"
"encoding/base64"
"encoding/json"
"runtime"
"strings"
"testing"
)

// Use this flag to update golden files with test outputs from current run.
Expand Down Expand Up @@ -109,7 +109,7 @@ func runTestScenariosWithInput(t *testing.T, tests []testCase, input *os.File) {
}

// Used for processing test output before comparing to golden files.
type processOutput func (string) string
type processOutput func(string) string

// Runs test cases where stdin input is needed and output needs to be processed before comparing to golden files.
func runTestScenariosWithInputAndProcessedOutput(t *testing.T, tests []testCase, input *os.File, processOutput processOutput) {
Expand Down Expand Up @@ -146,7 +146,7 @@ func runTestScenariosWithInputAndProcessedOutput(t *testing.T, tests []testCase,

// Test base-case scenarios
func TestCLI(t *testing.T) {
tests := []testCase {
tests := []testCase{
{
"no command",
[]string{},
Expand All @@ -159,7 +159,7 @@ func TestCLI(t *testing.T) {
"no-scope.golden",
false,
},
{
{
"fetch; jwt; no audience",
[]string{"fetch", "--type", "jwt"},
"no-audience.golden",
Expand All @@ -185,7 +185,7 @@ func TestCLI(t *testing.T) {
},
{
"curl; no scope",
[]string{"curl","--url","https://test.com"},
[]string{"curl", "--url", "https://test.com"},
"no-scope.golden",
false,
},
Expand All @@ -197,13 +197,13 @@ func TestCLI(t *testing.T) {
},
{
"info; invalid token",
[]string{"info","--token","invalid-token"},
[]string{"info", "--token", "invalid-token"},
"info-invalid-token.golden",
false,
},
{
"test; invalid token",
[]string{"test","--token","invalid-token"},
[]string{"test", "--token", "invalid-token"},
"test-invalid-token.golden",
false,
},
Expand All @@ -219,7 +219,7 @@ func TestCLI(t *testing.T) {

// Test OAuth 3LO flow with fake client secrets. Fake verification code is injected to stdin to advance the flow.
func Test3LOFlow(t *testing.T) {
tests := []testCase {
tests := []testCase{
{
"fetch; 3lo",
[]string{"fetch", "--scope", "pubsub", "--credentials", "integration/fixtures/fake-client-secrets.json", "--cache", ""},
Expand Down Expand Up @@ -257,12 +257,12 @@ func Test3LOFlow(t *testing.T) {
false,
},
}
runTestScenariosWithInput(t, tests, newFixture(t,"fake-verification-code.fixture").asFile())
runTestScenariosWithInput(t, tests, newFixture(t, "fake-verification-code.fixture").asFile())
}

// Test OAuth 2LO Flow with fake service account.
func Test2LOFlow(t *testing.T) {
tests := []testCase {
tests := []testCase{
{
"fetch; 2lo",
[]string{"fetch", "--scope", "pubsub", "--credentials", "integration/fixtures/fake-service-account.json", "--cache", ""},
Expand Down Expand Up @@ -293,7 +293,7 @@ func Test2LOFlow(t *testing.T) {

// Test JWT Flow.
func TestJWTFlow(t *testing.T) {
tests := []testCase {
tests := []testCase{
{
"fetch; jwt",
[]string{"fetch", "--type", "jwt", "--audience", "https://pubsub.googleapis.com/", "--credentials", "integration/fixtures/fake-service-account.json"},
Expand All @@ -312,8 +312,8 @@ func TestJWTFlow(t *testing.T) {
//JWT is signed with a timestamp that differs in every execution, so we will strip out "exp" and "iat" fields
encodedPayload := strings.Split(jwt, ".")[1]
decodedPayload, _ := base64.RawStdEncoding.DecodeString(encodedPayload)
var jsonData map[string]interface{}
json.Unmarshal(decodedPayload, &jsonData)
var jsonData map[string]interface{}
json.Unmarshal(decodedPayload, &jsonData) // nolint:errcheck
delete(jsonData, "exp")
delete(jsonData, "iat")
jsonString, _ := json.Marshal(jsonData)
Expand All @@ -324,7 +324,7 @@ func TestJWTFlow(t *testing.T) {

// Test SSO Flow. Uses "echo" as a fake ssocli to return the calling parameters instead of an actual token.
func TestSSOFlow(t *testing.T) {
tests := []testCase {
tests := []testCase{
{
"fetch; sso",
[]string{"fetch", "--type", "sso", "--email", "example@example.com", "--scope", "pubsub", "--ssocli", "echo"},
Expand All @@ -351,7 +351,7 @@ func readFile(path string) string {

func MockTokenApi(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response := readFile ("integration/fixtures/mock-token-response.json")
response := readFile("integration/fixtures/mock-token-response.json")
fmt.Fprint(w, response)
}

Expand Down Expand Up @@ -385,14 +385,14 @@ func TestMain(m *testing.M) {
}

// Start mock server
go func() {
mux := http.NewServeMux()
server := http.Server{Addr: ":8080", Handler: mux}
mux.HandleFunc("/token", MockTokenApi)
mux.HandleFunc("/curl", MockCurlApi)
if err := server.ListenAndServe(); err != nil {
fmt.Printf("could not listen on port 8080 %v", err)
}
go func() {
mux := http.NewServeMux()
server := http.Server{Addr: ":8080", Handler: mux}
mux.HandleFunc("/token", MockTokenApi)
mux.HandleFunc("/curl", MockCurlApi)
if err := server.ListenAndServe(); err != nil {
fmt.Printf("could not listen on port 8080 %v", err)
}
}()

status := m.Run()
Expand Down
43 changes: 22 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"regexp"
"strings"

"github.com/google/oauth2l/sgauth"
"github.com/google/oauth2l/util"
"github.com/jessevdk/go-flags"
Expand All @@ -43,12 +44,12 @@ var (

// Top level command-line flags (first argument after program name).
type commandOptions struct {
Fetch fetchOptions `command:"fetch" description:"Fetch an access token."`
Fetch fetchOptions `command:"fetch" description:"Fetch an access token."`
Header headerOptions `command:"header" description:"Fetch an access token and return it in header format."`
Curl curlOptions `command:"curl" description:"Fetch an access token and use it to make a curl request."`
Info infoOptions `command:"info" description:"Display info about an OAuth access token."`
Test infoOptions `command:"test" description:"Tests an OAuth access token. Returns 0 for valid token."`
Reset resetOptions `command:"reset" description:"Resets the cache."`
Curl curlOptions `command:"curl" description:"Fetch an access token and use it to make a curl request."`
Info infoOptions `command:"info" description:"Display info about an OAuth access token."`
Test infoOptions `command:"test" description:"Tests an OAuth access token. Returns 0 for valid token."`
Reset resetOptions `command:"reset" description:"Resets the cache."`
}

// Common options for "fetch", "header", and "curl" commands.
Expand All @@ -62,9 +63,9 @@ type commonFetchOptions struct {

// GUAC parameters
Credentials string `long:"credentials" description:"Credentials file containing OAuth Client Id or Service Account Key. Optional if environment variable GOOGLE_APPLICATION_CREDENTIALS is set."`
Scope string `long:"scope" description:"List of OAuth scopes requested. Required for oauth and sso authentication type. Comma delimited."`
Audience string `long:"audience" description:"Audience used for JWT self-signed token. Required for jwt authentication type."`
Email string `long:"email" description:"Email associated with SSO. Required for sso authentication type."`
Scope string `long:"scope" description:"List of OAuth scopes requested. Required for oauth and sso authentication type. Comma delimited."`
Audience string `long:"audience" description:"Audience used for JWT self-signed token. Required for jwt authentication type."`
Email string `long:"email" description:"Email associated with SSO. Required for sso authentication type."`

// Client parameters
SsoCli string `long:"ssocli" description:"Path to SSO CLI. Optional."`
Expand All @@ -73,9 +74,9 @@ type commonFetchOptions struct {
Cache *string `long:"cache" description:"Path to the credential cache file. Disables caching if set to empty. Defaults to ~/.oauth2l."`

// Deprecated flags kept for backwards compatibility. Hidden from help page.
Json string `long:"json" description:"Deprecated. Same as --credentials." hidden:"true"`
Jwt bool `long:"jwt" description:"Deprecated. Same as --type jwt." hidden:"true"`
Sso bool `long:"sso" description:"Deprecated. Same as --type sso." hidden:"true"`
Json string `long:"json" description:"Deprecated. Same as --credentials." hidden:"true"`
Jwt bool `long:"jwt" description:"Deprecated. Same as --type jwt." hidden:"true"`
Sso bool `long:"sso" description:"Deprecated. Same as --type sso." hidden:"true"`
OldFormat string `long:"credentials_format" choice:"bare" choice:"header" choice:"json" choice:"json_compact" choice:"pretty" description:"Deprecated. Same as --output_format" hidden:"true"`
}

Expand All @@ -94,7 +95,7 @@ type headerOptions struct {
type curlOptions struct {
commonFetchOptions
CurlCli string `long:"curlcli" description:"Path to Curl CLI. Optional."`
Url string `long:"url" description:"URL endpoint for the curl request." required:"true"`
Url string `long:"url" description:"URL endpoint for the curl request." required:"true"`
}

// Options for "info" and "test" commands.
Expand Down Expand Up @@ -150,7 +151,7 @@ func setCacheLocation(cache *string) {
}

// Extracts the common fetch options based on chosen command.
func getCommonFetchOptions (cmdOpts commandOptions, cmd string) commonFetchOptions {
func getCommonFetchOptions(cmdOpts commandOptions, cmd string) commonFetchOptions {
var commonOpts commonFetchOptions
switch cmd {
case "fetch":
Expand All @@ -164,7 +165,7 @@ func getCommonFetchOptions (cmdOpts commandOptions, cmd string) commonFetchOptio
}

// Get the authentication type, with backward compatibility.
func getAuthTypeWithFallback (commonOpts commonFetchOptions) string {
func getAuthTypeWithFallback(commonOpts commonFetchOptions) string {
authType := commonOpts.AuthType
if commonOpts.Jwt {
authType = "jwt"
Expand All @@ -175,7 +176,7 @@ func getAuthTypeWithFallback (commonOpts commonFetchOptions) string {
}

// Get the credentials file, with backward compatibility.
func getCredentialsWithFallback (commonOpts commonFetchOptions) string {
func getCredentialsWithFallback(commonOpts commonFetchOptions) string {
credentials := commonOpts.Credentials
if commonOpts.Json != "" {
credentials = commonOpts.Json
Expand All @@ -184,7 +185,7 @@ func getCredentialsWithFallback (commonOpts commonFetchOptions) string {
}

// Get the fetch output format, with backward compatibility.
func getOutputFormatWithFallback (fetchOpts fetchOptions) string {
func getOutputFormatWithFallback(fetchOpts fetchOptions) string {
format := fetchOpts.Format
if fetchOpts.OldFormat != "" {
format = fetchOpts.OldFormat
Expand All @@ -193,7 +194,7 @@ func getOutputFormatWithFallback (fetchOpts fetchOptions) string {
}

// Converts scope argument to string slice, with backward compatibility.
func getScopesWithFallback (scope string, remainingArgs ...string) []string {
func getScopesWithFallback(scope string, remainingArgs ...string) []string {
var scopes []string
// Fallback to reading scope from remaining args
if scope == "" {
Expand All @@ -217,7 +218,7 @@ func getTaskArgs(cmd, curlcli, url, format string, remainingArgs ...string) []st
}

// Extracts the info options based on chosen command.
func getInfoOptions (cmdOpts commandOptions, cmd string) infoOptions {
func getInfoOptions(cmdOpts commandOptions, cmd string) infoOptions {
var infoOpts infoOptions
switch cmd {
case "info":
Expand Down Expand Up @@ -245,7 +246,7 @@ func main() {
fetchTasks := map[string]func(*sgauth.Settings, ...string){
"fetch": util.Fetch,
"header": util.Header,
"curl": util.Curl,
"curl": util.Curl,
}

// Tasks that verify the existing token.
Expand All @@ -267,7 +268,7 @@ func main() {
curlcli := opts.Curl.CurlCli
url := opts.Curl.Url

if authType == "jwt"{
if authType == "jwt" {
// JWT flow
json, err := readJSON(credentials)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion sgauth/appengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"sync"
"time"

"golang.org/x/net/context"
"github.com/google/oauth2l/sgauth/internal"
"golang.org/x/net/context"
)

// appengineFlex is set at init time by appengineflex_hook.go. If true, we are on App Engine Flex.
Expand Down
6 changes: 3 additions & 3 deletions sgauth/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package sgauth

import (
"cloud.google.com/go/compute/metadata"
"errors"
"encoding/json"
"strings"
"errors"
"fmt"
"time"
"github.com/google/oauth2l/sgauth/internal"
"strings"
"time"
)

// ComputeTokenSource returns a token source that fetches access tokens
Expand Down
Loading

0 comments on commit de29b14

Please sign in to comment.