From 971e77229ab17ff654219baba671bfb69e065b87 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Sat, 6 Jan 2024 13:51:26 +0100 Subject: [PATCH] Initial test --- go.mod | 4 +- go.sum | 2 + main.go | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 120 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index a82e968..411c236 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,9 @@ module gitea.bruyant.xyz/alexandre/ipfs-node-pin go 1.21.5 +require github.com/sethvargo/go-githubactions v1.1.0 + require ( + github.com/google/go-cmp v0.5.9 // indirect github.com/sethvargo/go-envconfig v0.9.0 // indirect - github.com/sethvargo/go-githubactions v1.1.0 // indirect ) diff --git a/go.sum b/go.sum index 757224e..8421710 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/sethvargo/go-envconfig v0.9.0 h1:Q6FQ6hVEeTECULvkJZakq3dZMeBQ3JUpcKMfPQbKMDE= github.com/sethvargo/go-envconfig v0.9.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0= github.com/sethvargo/go-githubactions v1.1.0 h1:mg03w+b+/s5SMS298/2G6tHv8P0w0VhUFaqL1THIqzY= diff --git a/main.go b/main.go index e3162fb..685555a 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,121 @@ package main import ( - "github.com/sethvargo/go-githubactions" + "encoding/json" + "fmt" + "io" + "io/fs" + "mime/multipart" + "net/http" + "os" + "path/filepath" + "strings" + + "github.com/sethvargo/go-githubactions" ) -func main() { - path := githubactions.GetInput("path_to_add") - if path == "" { - githubactions.Fatalf("Missing: path_to_add") - } +type AddResponse struct { + Bytes int64 + Hash string + Name string + Size string +} + +func main() { + // Check inputs + path := githubactions.GetInput("path_to_add") + if path == "" { + githubactions.Fatalf("Missing: path_to_add") + } + + ipfsHost := githubactions.GetInput("ipfs_host") + if ipfsHost == "" { + githubactions.Fatalf("Missing: ipfs_host") + } + + ipfsPort := githubactions.GetInput("ipfs_port") + if ipfsPort == "" { + githubactions.Fatalf("Missing: ipfs_port") + } + + token := githubactions.GetInput("ipfs_token") + if token == "" { + githubactions.Fatalf("Missing: ipfs_token") + } + + targetPath, err := os.Open(path) + if err != nil { + githubactions.Fatalf("Unable to access path_to_add: %v", err.Error()) + } + defer targetPath.Close() + + targetPathInfo, err := targetPath.Stat() + if err != nil { + githubactions.Fatalf("Unable to access to access path_to_add info: %v", fmt.Errorf("%w", err)) + } + + if !targetPathInfo.IsDir() { + githubactions.Fatalf("%v is not a directory", path) + } + + body, writer := io.Pipe() + + // TODO: URL + req, err := http.NewRequest(http.MethodPost, "ipfs.narwhal-frog.ts.net", body) + if err != nil { + githubactions.Fatalf("Unable to create request: %v", err.Error()) + } + + go func() { + defer writer.Close() + + mwriter := multipart.NewWriter(writer) + req.Header.Add("Content-Type", mwriter.FormDataContentType()) + + err = filepath.Walk(path, func(innerPath string, info fs.FileInfo, err error) error { + if err != nil { + return err + } + + relativeParh := strings.TrimPrefix(innerPath, path) + w, err := mwriter.CreateFormFile("file", relativeParh) + if err != nil { + return err + } + + in, err := os.Open(innerPath) + if err != nil { + return err + } + defer in.Close() + + if written, err := io.Copy(w, in); err != nil { + return fmt.Errorf("error copying %s (%d bytes written): %v", path, written, err) + } + + if err := mwriter.Close(); err != nil { + return err + } + + return nil + }) + + if err != nil { + githubactions.Fatalf("Unable to create request body : %v", fmt.Errorf("%w", err)) + } + }() + + client := &http.Client{} + res, err := client.Do(req) + + resBody, err := io.ReadAll(res.Body) + + if err != nil { + panic(err.Error()) + } + + var ipfsAddResponse AddResponse + json.Unmarshal(resBody, &ipfsAddResponse) + + fmt.Println(ipfsAddResponse.Hash) }