Skip to content

Commit

Permalink
add output flags and extension detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jozsefsallai committed Feb 15, 2020
1 parent f5f1487 commit 8412a77
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ fastbin-cli file.txt
ls -ag | fastbin-cli
```

**To upload a file and only print and copy the full URL:**

```
fastbin-cli --full file.txt
```

**To upload a file and only print and copy the raw URL:**

```
fastbin-cli --raw file.txt
```

## Installation

### Linux, Mac OS, FreeBSD, Windows
Expand All @@ -42,7 +54,7 @@ You can use the built-in `init` command to initialize your environment settings.
fastbin-cli init
```

It will prompt you for the URL of the fastbin server you want to use. The settings will be stored in `~/.fastbinrc.json`. You can also create this file yourself, based on the [example](https://github.com/jozsefsallai/fastbin-cli/blob/master/.fastbinrc.example.json).
It will prompt you for the URL of the fastbin server you want to use and the private access key of the fastbin server (if it's not a public server). The settings will be stored in `~/.fastbinrc.json`. You can also create this file yourself, based on the [example](https://github.com/jozsefsallai/fastbin-cli/blob/master/.fastbinrc.example.json).

## Contribution

Expand Down
53 changes: 41 additions & 12 deletions commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,64 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/atotto/clipboard"
"github.com/jozsefsallai/fastbin-cli/config"
"github.com/jozsefsallai/fastbin-cli/utils"
"github.com/urfave/cli"
)

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

documentURL := conf.Server + "/" + key
rawURL := conf.Server + "/raw/" + key

clipboard.WriteAll(documentURL)

fmt.Println("Snippet uploaded successfully!")
fmt.Println("URL:", documentURL)
fmt.Println("Raw:", rawURL)
documentURL := fmt.Sprintf("%s/%s%s", conf.Server, key, extension)
rawURL := fmt.Sprintf("%s/raw/%s", conf.Server, key)

switch mode {
case "full":
clipboard.WriteAll(documentURL)
fmt.Println(documentURL)
case "raw":
clipboard.WriteAll(rawURL)
fmt.Println(rawURL)
default:
clipboard.WriteAll(documentURL)

fmt.Println("Snippet uploaded successfully!")
fmt.Println("URL:", documentURL)
fmt.Println("Raw:", rawURL)
}
}

// 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")
isRaw := ctx.Bool("raw")

if isFull && isRaw {
fmt.Println("Please use either --full or --raw, not both.")
return nil
}

if isFull {
mode = "full"
}

if isRaw {
mode = "raw"
}

info, err := os.Stdin.Stat()
if err != nil {
return err
}

if info.Mode()&os.ModeCharDevice != 0 {
if ctx.NArg() != 1 {
if ctx.NArg() == 0 {
fmt.Println("Please provide a file to upload.")
return nil
}
Expand All @@ -48,6 +75,8 @@ func CreateSnippet(ctx *cli.Context) error {
return nil
}

extension := filepath.Ext(fileName)

data, err := ioutil.ReadFile(ctx.Args().Get(0))
if err != nil {
panic(err)
Expand All @@ -58,7 +87,7 @@ func CreateSnippet(ctx *cli.Context) error {
log.Fatal(err)
}

printUrls(result)
printUrls(result, mode, extension)

return nil
}
Expand All @@ -79,7 +108,7 @@ func CreateSnippet(ctx *cli.Context) error {
log.Fatal(err)
}

printUrls(result)
printUrls(result, mode, "")

return nil
}
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ func main() {
app.Usage = "command line client for fastbin"
app.Version = "1.0.0"
app.Action = commands.CreateSnippet
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "full, f",
Usage: "only print out and copy the full URL",
},
cli.BoolFlag{
Name: "raw, r",
Usage: "only print out and copy the raw URL",
},
}
app.Commands = []cli.Command{
{
Name: "init",
Expand Down

0 comments on commit 8412a77

Please sign in to comment.