]> Vexing Labs - dead-tooter.git/commitdiff
load from file
authorAdam Shamblin <adam@vexingworkshop.com>
Mon, 5 Dec 2022 01:14:35 +0000 (18:14 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Mon, 5 Dec 2022 01:14:35 +0000 (18:14 -0700)
main.go

diff --git a/main.go b/main.go
index 5b90022c7e6b67b0e8b10294ec2a9dac1e476df8..3af400acc29986d774e05e973b1a47ff32d8db7e 100644 (file)
--- 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)
 }