From d6145a83c2134046531d333a5c3ec66b30a1d0f7 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Sun, 4 Dec 2022 18:14:35 -0700 Subject: [PATCH] load from file --- main.go | 63 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 5b90022..3af400a 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main import ( + "encoding/json" "fmt" "io" "net/http" "net/url" + "os" ) type App struct { @@ -17,10 +19,10 @@ type App struct { func (a App) Create() (application Application, err error) { resp, err := http.PostForm("https://hackers.town/api/v1/apps", url.Values{ - "client_name": {"dead-tooter"}, - "redirect_uris": {"urn:ietf:wg:oauth:2.0:oob"}, - "scopes": {"read:follows write:blocks"}, - "website": {"https://dead-tooter.vexingworkshop.com"}, + "client_name": {a.ClientName}, + "redirect_uris": {a.RedirectUris}, + "scopes": {a.Scopes}, + "website": {a.Website}, }, ) if err != nil { @@ -29,10 +31,29 @@ func (a App) Create() (application Application, err error) { defer resp.Body.Close() body, err := io.ReadAll(resp.Body) - fmt.Printf("%s", resp.Status) - fmt.Printf("%s", string(body)) + err = json.Unmarshal(body, &application) - return Application{}, nil + return +} + +func (a App) Load(name string) (Application, error) { + err := os.Mkdir("foodir", 0750) + if err != nil && !os.IsExist(err) { + panic(err.Error()) + } + + data, err := os.ReadFile("~/foodir/" + name) + if err != nil && os.IsNotExist(err) { + panic(err.Error()) + } + + var application Application + err = json.Unmarshal(data, &application) + if err != nil { + panic(err.Error()) + } + + return application, nil } type Application struct { @@ -47,15 +68,29 @@ type Application struct { func (a Application) Login() {} -func main() { - app := App{ - ClientName: "dead-tooter", - RedirectUris: "urn:ietf:wg:oauth:2.0:oob", - Scopes: "read:follows", - Website: "https://dead-tooter.vexingworkshop.com", +/* + func main() { + app := App{ + ClientName: "dead-tooter", + RedirectUris: "urn:ietf:wg:oauth:2.0:oob", + Scopes: "read:follows", + Website: "https://dead-tooter.vexingworkshop.com", + } + + tooter, err := app.Create() + if err != nil { + panic(err.Error()) + } + + fmt.Printf("%v", tooter) } - _, err := app.Create() +*/ +func main() { + + tooter, err := App{}.Load("dead-tooter") if err != nil { panic(err.Error()) } + + fmt.Printf("%v", tooter) } -- 2.39.5