-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
80 lines (66 loc) · 1.38 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/haya14busa/goplay"
"github.com/skratchdot/open-golang/open"
)
var (
help = flag.Bool("h", false, "show help")
run = flag.Bool("run", true, "compile and run Go program on The Go Playground")
share = flag.Bool("share", true, "share code on The Go Playground")
openBrowser = flag.Bool("openbrowser", true, "open browser automatically")
)
func main() {
flag.Parse()
if *help || (!*run && !*share) {
showHelp()
os.Exit(1)
}
code := os.Stdin
if len(flag.Args()) > 0 {
path := flag.Arg(0)
if path != "-" { // use stdin
file, err := os.Open(path)
fatalIf(err)
defer file.Close()
code = file
}
}
cli := goplay.DefaultClient
b, err := ioutil.ReadAll(code)
fatalIf(err)
if string(b) == "" {
fmt.Println("INPUT code is empty")
fmt.Println("")
showHelp()
os.Exit(1)
}
if *run {
if err := cli.Run(bytes.NewReader(b), os.Stdout, os.Stderr); err != nil {
fatalIf(err)
}
}
if *share {
url, err := cli.Share(bytes.NewReader(b))
fatalIf(err)
fmt.Println(url)
if *openBrowser {
fatalIf(open.Start(url))
}
}
}
func fatalIf(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func showHelp() {
fmt.Println("SYNOPSIS: goplay [OPTION]... [FILE] (With no FILE, or when FILE is -, read standard input.")
fmt.Println("")
flag.PrintDefaults()
}