Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedylandev committed Jan 16, 2024
0 parents commit 51f2d5a
Show file tree
Hide file tree
Showing 14 changed files with 1,096 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dist/
pinata-go-cli
pinata
.idea
.vscode
gon.hcl
100 changes: 100 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:

- binary: pinata

id: pinata

env:
- CGO_ENABLED=0
goos:
- linux
- windows
goarch:
- amd64

- binary: pinata

id: pinata-macos

goos:
- darwin

goarch:
- amd64

hooks:
post: gon gon.hcl

universal_binaries:
- id: pinata

- ids:
- build1
- build2
# Universal binary name.
#
# You will want to change this if you have multiple builds!
#
# Default: '{{ .ProjectName }}'
# Templates: allowed
name_template: "{{.ProjectName}}_{{.Version}}"

# Whether to remove the previous single-arch binaries from the artifact list.
# If left as false, your end release might have both several macOS archives:
# amd64, arm64 and all.
replace: true

# Set the modified timestamp on the output binary, typically
# you would do this to ensure a build was reproducible. Pass
# empty string to skip modifying the output.
#
# Templates: allowed.
# Since: v1.20.
mod_timestamp: "{{ .CommitTimestamp }}"

# Hooks can be used to customize the final binary,
# for example, to run generators.
#
# Templates: allowed
# hooks:
# pre: rice embed-go
# post: ./script.sh {{ .Path }}

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
files:
- install.sh
- README.md

snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
39 changes: 39 additions & 0 deletions Delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"errors"
"fmt"
"net/http"
)

func Delete(cid string) error {
jwt, err := findToken()
if err != nil {
return err
}
host := GetHost()
url := fmt.Sprintf("https://%s/pinning/unpin/%s", host, cid)

req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return errors.Join(err, errors.New("failed to create the request"))
}
req.Header.Set("Authorization", "Bearer "+string(jwt))
req.Header.Set("content-type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return errors.Join(err, errors.New("failed to send the request"))
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("server Returned an error %d, check CID", resp.StatusCode)
}

fmt.Println("File Deleted")

return nil

}
61 changes: 61 additions & 0 deletions ListFiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"
)

func ListFiles(amount string, cid string, name string, status string, offset string) (ListResponse, error) {
jwt, err := findToken()
if err != nil {
return ListResponse{}, err
}
host := GetHost()
url := fmt.Sprintf("https://%s/data/pinList?includesCount=false&pageLimit=%s&status=%s", host, amount, status)

if cid != "null" {
url += "&hashContains=" + cid
}
if name != "null" {
url += "&metadata[name]=" + name
}
if offset != "null" {
url += "&pageOffset=" + offset
}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return ListResponse{}, errors.Join(err, errors.New("failed to create the request"))
}
req.Header.Set("Authorization", "Bearer "+string(jwt))
req.Header.Set("content-type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return ListResponse{}, errors.Join(err, errors.New("failed to send the request"))
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return ListResponse{}, fmt.Errorf("server Returned an error %d", resp.StatusCode)
}

var response ListResponse

err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return ListResponse{}, err
}
formattedJSON, err := json.MarshalIndent(response.Rows, "", " ")
if err != nil {
return ListResponse{}, errors.New("failed to format JSON")
}

fmt.Println(string(formattedJSON))

return response, nil

}
64 changes: 64 additions & 0 deletions PinByCID.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)

func PinByCID(cid string, name string) (PinByCIDResponse, error) {
jwt, err := findToken()
if err != nil {
return PinByCIDResponse{}, err
}
host := GetHost()
url := fmt.Sprintf("https://%s/pinning/pinByHash", host)

// Create the request body
requestBody := map[string]interface{}{
"hashToPin": cid,
"pinataMetadata": map[string]string{
"name": name,
},
}
jsonBody, err := json.Marshal(requestBody)
if err != nil {
return PinByCIDResponse{}, errors.New("failed to create JSON body")
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
if err != nil {
return PinByCIDResponse{}, errors.New("failed to create the request")
}
req.Header.Set("Authorization", "Bearer "+string(jwt))
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return PinByCIDResponse{}, errors.New("failed to send the request")
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return PinByCIDResponse{}, fmt.Errorf("server returned an error %s", resp.Status)
}

var response PinByCIDResponse

err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return PinByCIDResponse{}, err
}

fmt.Println("Pin by CID Request Started")
fmt.Println("Request ID:", response.Id)
fmt.Println("CID:", response.CID)
fmt.Println("Status:", response.Status)
fmt.Println("Name:", response.Name)

return response, nil
}

107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Pinata Go CLI

Welcome to the Pinata Go CLI! This is still in active development so please let us know if you have any questions! :)

## Installation

> Note - If you are on Windows please use WSL when installing, as the current implementation will not work natively on Windows OS.
We are currently working on the build flow for binaries to make installation easier, but for now we recommend building from source.

To do this make sure you have [Go](https://go.dev/) installed on your computer and the following command returns a version:

```shell
go version
```

Then paste and run the following into your terminal:

```shell
git clone https://github.com/stevedylandev/pinata-go-cli && cd pinata-go-cli && go install .
```

## Usage

The Pinata CLI is equipped with the majortiry of features on the Pinata API.

### `auth` - Authentication

With the CLI installed you will first need to authenticate it with your [Pinata JWT](https://docs.pinata.cloud/docs/api-keys)

```shell
pinata auth <your-jwt>
```

### `upload` - Uploads

After authentication you can now upload using the `upload` command or `u` for short, then pass in the path to the file or folder you want to upload.

```shell
pinata upload ~/Pictures/somefolder/image.png
```

The following flags are also available to set the name or CID version of the upload.

```shell
--version value, -v value Set desired CID version to either 0 or 1. Default is 1. (default: 1)
--name value, -n value Add a name for the file you are uploading. By default it will use the filename on your system. (default: "nil")

```

### `list` - List Files

You can list files with the `list` command or the alias `l`. The results are printed in raw JSON to help increase composability.

```shell
pinata list
```

By default it will retrieve the 10 latest files, but with the flags below you can get more results or fine tune your search.

```shell
--cid value, -c value Search files by CID (default: "null")
--amount value, -a value The number of files you would like to return, default 10 max 1000 (default: "10")
--name value, -n value The name of the file (default: "null")
--status value, -s value Status of the file. Options are 'pinned', 'unpinned', or 'all'. Default: 'pinned' (default: "pinned")
--pageOffset value, -p value Allows you to paginate through files. If your file amount is 10, then you could set the pageOffset to '10' to see the next 10 files. (default: "null")
```

### `delete` - Delete Files

If you ever need to you can delete a file by CID using the `delete` command or alias `d` followed by the file CID.

```shell
pinata delete QmVLwvmGehsrNEvhcCnnsw5RQNseohgEkFNN1848zNzdng
```

### `pin` - Pin by CID

Separate from the `upload` command which uploads files from your machine to Pinata, you can also pin a file already on the IPFS network by using the `pin` command or alias `p` followed by the CID. This will start a pin by CID request which will go into a queue.

```shell
pinata pin QmVLwvmGehsrNEvhcCnnsw5RQNseohgEkFNN1848zNzdng
```

To check the queue use the `request` command.

### `requests` - Pin by CID Requests

As mentioned in the `pin` command, when you submit an existing CID on IPFS to be pinned to your Pinata account, it goes into a request queue. From here it will go through multiple status'. For more info on these please consult the [documentation](https://docs.pinata.cloud/reference/get_pinning-pinjobs).

```shell
pinata requests
```

You can use flags to help filter requests as well.

```shell
--cid value, -c value Search pin by CID requests by CID (default: "null")
--status value, -s value Search by status for pin by CID requests. See https://docs.pinata.cloud/reference/get_pinning-pinjobs for more info. (default: "null")
--pageOffset value, -p value Allows you to paginate through requests by number of requests. (default: "null")
```

## Contact

If you have any questions please feel free to reach out to us!

[team@pinata.cloud](mailto:team@pinata.cloud)
Loading

0 comments on commit 51f2d5a

Please sign in to comment.