diff --git a/commands/create.go b/commands/create.go index 5a5b463..0b79f90 100644 --- a/commands/create.go +++ b/commands/create.go @@ -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": @@ -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") @@ -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 } @@ -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 } diff --git a/config/main.go b/config/main.go index 8c7fb02..86299f3 100644 --- a/config/main.go +++ b/config/main.go @@ -2,7 +2,6 @@ package config import ( "encoding/json" - "fmt" "io/ioutil" "log" "os" @@ -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) diff --git a/main.go b/main.go index 6369394..8dfaefe 100644 --- a/main.go +++ b/main.go @@ -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{ diff --git a/utils/upload.go b/utils/upload.go index d04e793..f8ad043 100644 --- a/utils/upload.go +++ b/utils/upload.go @@ -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" @@ -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() @@ -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 }