From 2dce70b7a68d003ccc4269770731be42aedbd668 Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Tue, 11 Oct 2022 12:55:20 -0400 Subject: [PATCH 1/2] Clean up OAuth config * Get rid of unused variables and constants. * Move the main config to `config.go`. --- cmd/config.go | 14 ++++++++++++++ cmd/login.go | 41 +++++------------------------------------ 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 089495893f..f8a2e926f9 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -10,8 +10,22 @@ import ( "golang.org/x/oauth2" ) +const ( + oauthCallbackAddr = "localhost:8085" +) + var ( privateConfig = viper.New() + + oauthConf = &oauth2.Config{ + ClientID: "d8ae7ea0-4a1a-46b0-b556-6d742687223a", + Scopes: []string{"device", "offline_access"}, + Endpoint: oauth2.Endpoint{ + AuthURL: "https://login.tidbyt.com/oauth2/auth", + TokenURL: "https://login.tidbyt.com/oauth2/token", + }, + RedirectURL: fmt.Sprintf("http://%s", oauthCallbackAddr), + } ) func init() { diff --git a/cmd/login.go b/cmd/login.go index 84281b801b..94dbd3b0de 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -16,18 +16,9 @@ import ( cv "github.com/nirasan/go-oauth-pkce-code-verifier" "github.com/pkg/browser" "github.com/spf13/cobra" - "golang.org/x/oauth2" "golang.org/x/oauth2/authhandler" ) -const ( - oauthClientID = "d8ae7ea0-4a1a-46b0-b556-6d742687223a" - oauthCallbackAddr = "localhost:8085" - - oauthBaseURL = "https://login.tidbyt.com/oauth2/auth" - oauthTokenURL = "https://login.tidbyt.com/oauth2/token" -) - var LoginCmd = &cobra.Command{ Use: "login", Short: "Login to your Tidbyt account", @@ -35,38 +26,16 @@ var LoginCmd = &cobra.Command{ Run: login, } -var ( - oauthConf = &oauth2.Config{ - ClientID: "d8ae7ea0-4a1a-46b0-b556-6d742687223a", - Scopes: []string{"device", "offline_access"}, - Endpoint: oauth2.Endpoint{ - AuthURL: "https://login.tidbyt.com/oauth2/auth", - TokenURL: "https://login.tidbyt.com/oauth2/token", - }, - RedirectURL: fmt.Sprintf("http://%s", oauthCallbackAddr), - } -) - -type authResult struct { - code string - state string - err error -} - func login(cmd *cobra.Command, args []string) { server := &http.Server{ Addr: oauthCallbackAddr, } - authResult := struct { - code string - state string - }{} - - done := make(chan bool, 1) + var authCode, authState string + done := make(chan bool) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - authResult.code = r.URL.Query().Get("code") - authResult.state = r.URL.Query().Get("state") + authCode = r.URL.Query().Get("code") + authState = r.URL.Query().Get("state") w.Header().Set("content-type", "text/plain") io.WriteString(w, "please close this window and return to pixlet") @@ -84,7 +53,7 @@ func login(cmd *cobra.Command, args []string) { go server.ListenAndServe() <-done - return authResult.code, authResult.state, nil + return authCode, authState, nil } cv, err := cv.CreateCodeVerifier() From 9b7743f76679e71c576eec0c7e987c2457efe410 Mon Sep 17 00:00:00 2001 From: Rohan Singh Date: Tue, 11 Oct 2022 12:57:55 -0400 Subject: [PATCH 2/2] Follow Makefile idioms * Don't automatically `clean` on `build`. * Make building the default `make` action. --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4d77a1bac5..47e005a9cc 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ else LDFLAGS = -ldflags="-X 'tidbyt.dev/pixlet/cmd.Version=$(GIT_COMMIT)'" endif +all: build + test: go test -v -cover ./... @@ -19,7 +21,7 @@ clean: bench: go test -benchmem -benchtime=20s -bench BenchmarkRunAndRender tidbyt.dev/pixlet/encode -build: clean +build: go build $(LDFLAGS) -o $(BINARY) tidbyt.dev/pixlet embedfonts: