-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logpasta.go
88 lines (73 loc) · 1.51 KB
/
logpasta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/ripexz/logpasta/clipboard"
)
var version = "v0.3.3"
func main() {
initLogger()
conf := loadConfig()
checkForCommands()
var content string
fi, _ := os.Stdin.Stat()
if (fi.Mode() & os.ModeCharDevice) == 0 {
// piped
bytes, _ := ioutil.ReadAll(os.Stdin)
content = string(bytes)
} else {
content = strings.Join(flag.Args(), " ")
}
if content == "" {
log.Printf("No input detected, see 'logpasta help' for usage")
os.Exit(0)
}
// make request
var output string
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)
if conf.Copy {
err = clipboard.Copy(pasteURL)
if err != nil {
output += "\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 {
log.Println(content)
}
log.Println(output)
}
func checkForCommands() {
if len(os.Args) <= 1 {
return
}
switch os.Args[1] {
case "version":
printVersion()
os.Exit(0)
case "help":
printUsage()
os.Exit(0)
}
}