Skip to content

Commit 253e897

Browse files
committed
init
1 parent 36b2b6e commit 253e897

File tree

5 files changed

+421
-0
lines changed

5 files changed

+421
-0
lines changed

go.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"net/url"
9+
"strings"
10+
"time"
11+
)
12+
13+
const GoHelloWorld = `
14+
package main
15+
16+
import (
17+
"fmt"
18+
)
19+
20+
func main() {
21+
fmt.Println("Hello, code-playground")
22+
}
23+
`
24+
25+
type GoCompile struct {
26+
Errors string `json:"Errors"`
27+
Events []GoCompileEvent `json:"Events"`
28+
Status int `json:"Status"`
29+
IsTest bool `json:"IsTest"`
30+
TestsFailed int `json:"TestsFailed"`
31+
VetOK bool `json:"VetOK"`
32+
}
33+
type GoCompileEvent struct {
34+
Message string `json:"Message"`
35+
Kind string `json:"Kind"`
36+
Delay int32 `json:"Delay"`
37+
}
38+
39+
type Go struct {
40+
Code string
41+
}
42+
43+
func (g *Go) Init(code string) {
44+
g.Code = code
45+
}
46+
47+
func (g *Go) Default() string {
48+
return GoHelloWorld
49+
}
50+
51+
func (g *Go) Evaluate() {
52+
53+
Spinner.Start()
54+
defer Spinner.Stop()
55+
56+
values := url.Values{
57+
"version": {"2"},
58+
"body": {g.Code},
59+
"withVet": {"true"},
60+
}
61+
62+
values.Add("version", "2")
63+
values.Add("body", g.Code)
64+
values.Add("withVet", "true")
65+
66+
response, err := http.PostForm("https://play.golang.org/compile", values)
67+
68+
if err != nil {
69+
Log.Error(err)
70+
return
71+
}
72+
73+
body, err := ioutil.ReadAll(response.Body)
74+
75+
if err != nil {
76+
Log.Error(err)
77+
return
78+
}
79+
80+
result := &GoCompile{}
81+
82+
err = json.Unmarshal(body, result)
83+
84+
if err != nil {
85+
Log.Error(err)
86+
return
87+
}
88+
89+
result.Execute()
90+
91+
}
92+
93+
func (g *Go) Share() {
94+
95+
Spinner.Start()
96+
defer Spinner.Stop()
97+
98+
response, err := http.Post("https://play.golang.org/share", "text/plain", strings.NewReader(g.Code))
99+
100+
if err != nil {
101+
Log.Error(err)
102+
return
103+
}
104+
105+
body, err := ioutil.ReadAll(response.Body)
106+
107+
if err != nil {
108+
Log.Error(err)
109+
return
110+
}
111+
112+
var result = string(body)
113+
114+
Log.Info("https://play.golang.org/p/" + result)
115+
116+
}
117+
118+
func (g *GoCompile) Execute() {
119+
120+
Spinner.Stop()
121+
122+
fmt.Println("-------------------------------------------------")
123+
124+
if g.VetOK {
125+
for _, event := range g.Events {
126+
time.Sleep(time.Duration(event.Delay))
127+
fmt.Print(event.Message)
128+
}
129+
} else {
130+
Log.Error(g.Errors)
131+
}
132+
133+
fmt.Println("-------------------------------------------------")
134+
135+
}

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module code-playground
2+
3+
go 1.13
4+
5+
require (
6+
github.com/briandowns/spinner v1.8.0
7+
github.com/fatih/color v1.9.0
8+
github.com/spf13/cobra v0.0.5
9+
)

go.sum

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
3+
github.com/briandowns/spinner v1.8.0 h1:SeidJ8ASAayR4Wxl5Of54LHqgi8s6sBvAHg4kxKxia4=
4+
github.com/briandowns/spinner v1.8.0/go.mod h1://Zf9tMcxfRUA36V23M6YGEAv+kECGfvpnLTnb8n4XQ=
5+
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
6+
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
7+
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
8+
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
9+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
11+
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
12+
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
13+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
14+
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
15+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
16+
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
17+
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
18+
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
19+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
20+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
21+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
22+
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
23+
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
24+
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
25+
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
26+
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
27+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
28+
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
29+
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
30+
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
31+
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
32+
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
33+
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
34+
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
35+
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
36+
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
37+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
38+
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
39+
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
40+
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
41+
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
42+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
43+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
44+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
45+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
46+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
47+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"github.com/briandowns/spinner"
5+
"github.com/spf13/cobra"
6+
"time"
7+
)
8+
9+
var Spinner = spinner.New(spinner.CharSets[43], 100*time.Millisecond)
10+
var Log = NewLogger()
11+
12+
func main() {
13+
14+
var file string
15+
var share bool
16+
17+
var evaluate = func(playground IPlayground) {
18+
19+
editor := NewEditor(file, playground.Default())
20+
21+
code, err := editor.Open()
22+
23+
if err != nil {
24+
Log.Error(err)
25+
return
26+
}
27+
28+
playground.Init(code)
29+
30+
playground.Evaluate()
31+
32+
if share {
33+
playground.Share()
34+
}
35+
}
36+
37+
var cmd = &cobra.Command{
38+
Use: "play",
39+
Args: cobra.NoArgs,
40+
Run: func(cmd *cobra.Command, args []string) {
41+
},
42+
}
43+
44+
var goCmd = &cobra.Command{
45+
Use: "go",
46+
Args: cobra.OnlyValidArgs,
47+
Run: func(cmd *cobra.Command, args []string) {
48+
49+
if len(args) > 0 {
50+
file = args[0]
51+
}
52+
53+
evaluate(new(Go))
54+
},
55+
}
56+
57+
var rustCmd = &cobra.Command{
58+
Use: "rust",
59+
Args: cobra.OnlyValidArgs,
60+
Run: func(cmd *cobra.Command, args []string) {
61+
62+
if len(args) > 0 {
63+
file = args[0]
64+
}
65+
66+
evaluate(new(Rust))
67+
},
68+
}
69+
70+
goCmd.Flags().BoolVarP(&share, "share", "s", false, "share playground")
71+
rustCmd.Flags().BoolVarP(&share, "share", "s", false, "share playground")
72+
73+
cmd.AddCommand(goCmd, rustCmd)
74+
75+
_ = cmd.Execute()
76+
}

0 commit comments

Comments
 (0)