From e34c50184f63478acedc3c99d60fe7807e9cd8d1 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 2 May 2019 09:35:45 -0600 Subject: [PATCH] no more logger --- cli/cli.go | 7 +++++ config/config_test.go | 29 ++++++++++++++++++ ffiii/ffiii.go | 69 +++++++++++++++++++++++++++++++++++++++++++ ffiii/ffiii_test.go | 47 +++++++++++++++++++++++++++++ main.go | 9 +++++- 5 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 cli/cli.go create mode 100644 config/config_test.go create mode 100644 ffiii/ffiii.go create mode 100644 ffiii/ffiii_test.go diff --git a/cli/cli.go b/cli/cli.go new file mode 100644 index 0000000..a4a3ee7 --- /dev/null +++ b/cli/cli.go @@ -0,0 +1,7 @@ +package cli + +import "errors" + +func Parse() error { + return errors.New("not impl") +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..0fb7c7a --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,29 @@ +package config + +import ( + "os" + "testing" +) + +func TestNew(t *testing.T) { + was := append([]string{}, os.Args...) + defer func() { + os.Args = was + }() + os.Args = []string{ + "nothing", + "-addr", "str", + "-u", "str", + "-p", "str", + "-description", "str", + "-category", "str", + "-to", "str", + "-from", "str", + "-type", "str", + "-cost", "1.5", + "-date", "2019-03-04", + } + if err := New(); err != nil { + t.Fatalf("error on new: %v", err) + } +} diff --git a/ffiii/ffiii.go b/ffiii/ffiii.go new file mode 100644 index 0000000..15957e8 --- /dev/null +++ b/ffiii/ffiii.go @@ -0,0 +1,69 @@ +package ffiii + +import ( + "context" + "fmt" + "io" + "log" + "net/http" + "os" + "time" + + "golang.org/x/oauth2" +) + +type Client struct { + url string + client *http.Client + apiToken string + apiID string + oauthToken *oauth2.Token + oauthCode string + reader io.Reader +} + +func New(apiID, apiToken, url string) (*Client, error) { + c := &Client{ + apiID: apiID, + apiToken: apiToken, + url: url, + reader: os.Stdin, + } + if err := c.Authorize(); err != nil { + return nil, err + } + return c, nil +} + +func (c *Client) Authorize() error { + conf := &oauth2.Config{ + ClientID: c.apiID, + ClientSecret: c.apiToken, + RedirectURL: c.url, + Scopes: []string{}, + Endpoint: oauth2.Endpoint{ + AuthURL: fmt.Sprintf("%s/oauth/authorize", c.url), + TokenURL: fmt.Sprintf("%s/oauth/token", c.url), + }, + } + + authURL := conf.AuthCodeURL("state", oauth2.AccessTypeOffline) + fmt.Printf("Go to the following link in your browser then type the "+ + "authorization code: \n%v\n", authURL) + var authCode string + if _, err := fmt.Fscan(c.reader, &authCode); err != nil { + log.Fatalf("Unable to read authorization code: %v", err) + } + c.oauthCode = authCode + + ctx, can := context.WithTimeout(context.Background(), time.Second*30) + defer can() + + tok, err := conf.Exchange(ctx, c.oauthCode) + if err != nil { + return err + } + c.client = conf.Client(context.Background(), tok) + + return nil +} diff --git a/ffiii/ffiii_test.go b/ffiii/ffiii_test.go new file mode 100644 index 0000000..bc43122 --- /dev/null +++ b/ffiii/ffiii_test.go @@ -0,0 +1,47 @@ +package ffiii + +import ( + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" +) + +func TestAuthorize(t *testing.T) { + if realURL, ok := os.LookupEnv("OAUTH_URL"); ok { + realToken := os.Getenv("OAUTH_TOKEN") + realID := os.Getenv("OAUTH_ID") + realCode := os.Getenv("OAUTH_CODE") + c := &Client{ + apiID: realID, + apiToken: realToken, + url: realURL, + reader: strings.NewReader(realCode), + } + if err := c.Authorize(); err != nil { + t.Fatal(err) + } + return + } + oauth := mockOAuthServer() + defer oauth.Close() + + if _, err := New("1", "token", oauth.URL); err != nil { + t.Fatal(err) + } +} + +func mockOAuthServer() *httptest.Server { + mux := http.NewServeMux() + + mux.HandleFunc("/oauth/authorize", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("authCode")) + }) + + mux.HandleFunc("/oauth/token", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("access_token=mocktoken&scope=user&token_type=bearer")) + }) + + return httptest.NewServer(mux) +} diff --git a/main.go b/main.go index 35adb1a..9e6f703 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,16 @@ package main -import "local/fireflyiii/config" +import ( + "local2/fireflyiii/cli" + "local2/fireflyiii/config" +) func main() { if err := config.New(); err != nil { panic(err) } + + if err := cli.Parse(); err != nil { + panic(err) + } }