no more logger

master
Bel LaPointe 2019-05-02 09:35:45 -06:00
parent 1c3823b04e
commit e34c50184f
5 changed files with 160 additions and 1 deletions

7
cli/cli.go Normal file
View File

@ -0,0 +1,7 @@
package cli
import "errors"
func Parse() error {
return errors.New("not impl")
}

29
config/config_test.go Normal file
View File

@ -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)
}
}

69
ffiii/ffiii.go Normal file
View File

@ -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
}

47
ffiii/ffiii_test.go Normal file
View File

@ -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)
}

View File

@ -1,9 +1,16 @@
package main package main
import "local/fireflyiii/config" import (
"local2/fireflyiii/cli"
"local2/fireflyiii/config"
)
func main() { func main() {
if err := config.New(); err != nil { if err := config.New(); err != nil {
panic(err) panic(err)
} }
if err := cli.Parse(); err != nil {
panic(err)
}
} }