Skip to content

Commit

Permalink
Merge pull request #1 from ripexz/delete-keys
Browse files Browse the repository at this point in the history
Fix copy option, delete key support
  • Loading branch information
ripexz authored Nov 26, 2019
2 parents 048f10c + 1cc3507 commit 05df62b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
25 changes: 21 additions & 4 deletions logpasta.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,34 @@ func main() {

// make request
var output string
pasteURL, err := saveLog(conf, content)
paste, err := saveLog(conf, content)
if err != nil {
conf.Silent = false
output = fmt.Sprintf("Failed to save log: %s", err.Error())
} else {
pasteURL := fmt.Sprintf("%s/paste/%s", conf.BaseURL, paste.UUID)

output = fmt.Sprintf("Log saved successfully:\n%s", pasteURL)

err = clipboard.Copy(pasteURL)
if err != nil {
output += fmt.Sprintf("\nFailed to copy URL to clipboard: %s", err.Error())
if conf.Copy {
err = clipboard.Copy(pasteURL)
if err != nil {
output += fmt.Sprintf("\n(failed to copy to clipboard)")
if !conf.Silent {
output += fmt.Sprintf("\nError: %s", err.Error())
}
} else {
output += " (copied to clipboard)"
}
}

if paste.DeleteKey != nil {
output += fmt.Sprintf(
"\nYou can delete it early by visiting:\n%s/delete/%s/%s",
conf.BaseURL, paste.UUID, *paste.DeleteKey,
)
}

}

if !conf.Silent {
Expand Down
5 changes: 3 additions & 2 deletions paste.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

type Paste struct {
UUID string `json:"uuid,omitempty"`
Content string `json:"content,omitempty"`
UUID string `json:"uuid,omitempty"`
Content string `json:"content,omitempty"`
DeleteKey *string `json:"deleteKey"`
}

type PasteData struct {
Expand Down
24 changes: 12 additions & 12 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ import (
"time"
)

func saveLog(conf *Config, content string) (string, error) {
func saveLog(conf *Config, content string) (*Paste, error) {
client := http.Client{Timeout: time.Second * time.Duration(conf.Timeout)}

data := &PasteData{
data := PasteData{
Paste: Paste{
Content: content,
},
}

payload, err := json.Marshal(data)
payload, err := json.Marshal(&data)
if err != nil {
return "", err
return nil, err
}

var buf bytes.Buffer
zipper := gzip.NewWriter(&buf)
if _, err = zipper.Write(payload); err != nil {
return "", err
return nil, err
}
if err = zipper.Close(); err != nil {
return "", err
return nil, err
}

req, err := http.NewRequest(
Expand All @@ -39,27 +39,27 @@ func saveLog(conf *Config, content string) (string, error) {
bytes.NewReader(buf.Bytes()),
)
if err != nil {
return "", err
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Set("Content-Encoding", "gzip")
req.Header.Set("User-Agent", fmt.Sprintf("Logpasta CLI %s", version))

res, err := client.Do(req)
if err != nil {
return "", err
return nil, err
}
defer res.Body.Close()

if res.StatusCode > 299 {
return "", fmt.Errorf("failed to make request: %s", res.Status)
return nil, fmt.Errorf("failed to make request: %s", res.Status)
}

resData, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
return nil, err
}

err = json.Unmarshal(resData, data)
return fmt.Sprintf("%s/paste/%s", conf.BaseURL, data.Paste.UUID), err
err = json.Unmarshal(resData, &data)
return &data.Paste, err
}

0 comments on commit 05df62b

Please sign in to comment.