Skip to content

Commit

Permalink
add delete URL support and default server
Browse files Browse the repository at this point in the history
  • Loading branch information
jozsefsallai committed Jan 27, 2021
1 parent 4d800cc commit 7f7b5b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
20 changes: 12 additions & 8 deletions commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import (
"github.com/urfave/cli"
)

func printUrls(key string, mode string, extension string) {
func printUrls(key string, secret string, mode string, extension string) {
conf := config.GetConfig()

documentURL := fmt.Sprintf("%s/%s%s", conf.Server, key, extension)
rawURL := fmt.Sprintf("%s/raw/%s", conf.Server, key)
deleteURL := ""
if len(secret) > 0 {
deleteURL = fmt.Sprintf("%s/delete/%s", conf.Server, secret)
}

switch mode {
case "full":
Expand All @@ -34,12 +38,12 @@ func printUrls(key string, mode string, extension string) {
fmt.Println("Snippet uploaded successfully!")
fmt.Println("URL:", documentURL)
fmt.Println("Raw:", rawURL)
fmt.Println("Delete:", deleteURL)
}
}

// CreateSnippet is the function that creates a snippet on the
// remote server either from a file or from another command's
// output
// CreateSnippet is the function that creates a snippet on the remote server
// either from a file or from another command's output
func CreateSnippet(ctx *cli.Context) error {
mode := ""
isFull := ctx.Bool("full")
Expand Down Expand Up @@ -82,12 +86,12 @@ func CreateSnippet(ctx *cli.Context) error {
panic(err)
}

result, err := utils.Upload(string(data))
key, secret, err := utils.Upload(string(data))
if err != nil {
log.Fatal(err)
}

printUrls(result, mode, extension)
printUrls(key, secret, mode, extension)

return nil
}
Expand All @@ -103,12 +107,12 @@ func CreateSnippet(ctx *cli.Context) error {
output = append(output, input)
}

result, err := utils.Upload(string(output))
key, secret, err := utils.Upload(string(output))
if err != nil {
log.Fatal(err)
}

printUrls(result, mode, "")
printUrls(key, secret, mode, "")

return nil
}
6 changes: 3 additions & 3 deletions config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
Expand All @@ -23,8 +22,9 @@ func GetConfig() Config {
rcPath := path.Join(home, ".fastbinrc.json")

if _, err := os.Stat(rcPath); os.IsNotExist(err) {
fmt.Println("Configuration file not found. Please create a .fastbinrc.json file in your home directory.")
os.Exit(1)
return Config{
Server: "https://fastbin.xyz",
}
}

rc, err := os.Open(rcPath)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {
app := cli.NewApp()
app.Name = "fastbin"
app.Usage = "command line client for fastbin"
app.Version = "1.0.0"
app.Version = "1.1.0"
app.Action = commands.CreateSnippet
app.Flags = []cli.Flag{
cli.BoolFlag{
Expand Down
19 changes: 10 additions & 9 deletions utils/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
// RequestResponse is the structure of a potential JSON response
// coming from the server
type RequestResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
Key string `json:"key"`
Ok bool `json:"ok"`
Error string `json:"error"`
Key string `json:"key"`
Secret string `json:"secret"`
}

// Upload will upload a file to the remote server.
func Upload(input string) (string, error) {
func Upload(input string) (string, string, error) {
conf := config.GetConfig()

url := conf.Server + "/documents"
Expand All @@ -33,12 +34,12 @@ func Upload(input string) (string, error) {
}

if err != nil {
return "", err
return "", "", err
}

res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
return "", "", err
}

defer res.Body.Close()
Expand All @@ -50,11 +51,11 @@ func Upload(input string) (string, error) {

if jsonResponse.Ok == false {
if len(jsonResponse.Error) > 0 {
return "", errors.New(jsonResponse.Error)
return "", "", errors.New(jsonResponse.Error)
}

return "", errors.New("failed to upload the snippet")
return "", "", errors.New("failed to upload the snippet")
}

return string(jsonResponse.Key), nil
return string(jsonResponse.Key), string(jsonResponse.Secret), nil
}

0 comments on commit 7f7b5b4

Please sign in to comment.