├── .github └── workflows │ └── test.yaml ├── LICENSE ├── README.md ├── cmd └── playgo │ └── main.go ├── go.mod ├── go.sum ├── playgo.go ├── playgo_test.go └── usage.gif /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | on: [push] 3 | jobs: 4 | test: 5 | name: Tests 6 | runs-on: ubuntu-latest 7 | steps: 8 | - name: Install Go 9 | uses: actions/setup-go@v5 10 | with: 11 | go-version: 1.22 12 | 13 | - name: Check out code into the Go module directory 14 | uses: actions/checkout@v4 15 | 16 | - name: Run Tests 17 | run: go test -race -v . 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Aliaksandr Pliutau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Usually when we share a runnable Go code we do: copy code, open [Go Playground](https://go.dev), paste code, click Share. 2 | 3 | So `playgo` does it for you. 4 | 5 | ### Install 6 | 7 | ``` 8 | go get -u github.com/plutov/playgo/cmd/playgo 9 | ``` 10 | 11 | ### Usage 12 | 13 | ![usage.gif](https://raw.githubusercontent.com/plutov/playgo/master/usage.gif) 14 | -------------------------------------------------------------------------------- /cmd/playgo/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/plutov/playgo" 8 | ) 9 | 10 | func main() { 11 | url, err := playgo.ShareAndOpen() 12 | if err != nil { 13 | fmt.Printf("Error: %s\nUSAGE: playgo [FILE]\n", err.Error()) 14 | os.Exit(1) 15 | } 16 | 17 | fmt.Printf("%s (copied to clipboard)\n", url) 18 | } 19 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/plutov/playgo 2 | 3 | go 1.22 4 | 5 | require ( 6 | github.com/atotto/clipboard v0.1.4 7 | github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= 2 | github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= 3 | github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= 4 | github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= 5 | -------------------------------------------------------------------------------- /playgo.go: -------------------------------------------------------------------------------- 1 | package playgo 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "io/ioutil" 7 | "net/http" 8 | "os" 9 | "path/filepath" 10 | 11 | "github.com/atotto/clipboard" 12 | "github.com/skratchdot/open-golang/open" 13 | ) 14 | 15 | var playgroundHost = "https://play.golang.org" 16 | 17 | // ShareAndOpen func 18 | func ShareAndOpen() (string, error) { 19 | flag.Parse() 20 | path := flag.Arg(0) 21 | 22 | url, shareErr := Share(path) 23 | if shareErr != nil { 24 | return "", shareErr 25 | } 26 | 27 | clipboard.WriteAll(url) 28 | 29 | return url, open.Start(url) 30 | } 31 | 32 | // Share func 33 | func Share(path string) (string, error) { 34 | file, err := os.Open(path) 35 | if err != nil { 36 | return "", err 37 | } 38 | defer file.Close() 39 | 40 | if ext := filepath.Ext(path); ext != ".go" { 41 | return "", fmt.Errorf("File %s is not a .go file", path) 42 | } 43 | 44 | fileInfo, err := file.Stat() 45 | if err != nil { 46 | return "", err 47 | } 48 | if fileInfo.Size() == 0 { 49 | return "", fmt.Errorf("File %s is empty", path) 50 | } 51 | 52 | req, err := http.NewRequest("POST", playgroundHost+"/share", file) 53 | if err != nil { 54 | return "", err 55 | } 56 | req.Header.Set("User-Agent", "playgo/1.0") 57 | 58 | c := new(http.Client) 59 | resp, err := c.Do(req) 60 | if err != nil { 61 | return "", err 62 | } 63 | 64 | respBody, respErr := ioutil.ReadAll(resp.Body) 65 | resp.Body.Close() 66 | if respErr != nil { 67 | return "", respErr 68 | } 69 | 70 | return fmt.Sprintf("%s/p/%s", playgroundHost, string(respBody)), nil 71 | } 72 | -------------------------------------------------------------------------------- /playgo_test.go: -------------------------------------------------------------------------------- 1 | package playgo 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "os" 7 | "testing" 8 | ) 9 | 10 | var ( 11 | emptyFileName = ".empty.go" 12 | 13 | shares = []struct { 14 | path string 15 | ok bool 16 | }{ 17 | {"", false}, 18 | {"iujo7y9n86gn6gjiwef", false}, 19 | {"README.md", false}, 20 | {emptyFileName, false}, 21 | {"cmd/playgo/main.go", true}, 22 | } 23 | ) 24 | 25 | var mockServer *httptest.Server 26 | 27 | func init() { 28 | // create test empty file 29 | os.OpenFile(emptyFileName, os.O_CREATE, 0755) 30 | 31 | mux := http.NewServeMux() 32 | mux.HandleFunc("/share", func(w http.ResponseWriter, req *http.Request) {}) 33 | 34 | mockServer = httptest.NewServer(mux) 35 | playgroundHost = mockServer.URL 36 | } 37 | 38 | func TestShare(t *testing.T) { 39 | defer os.Remove(emptyFileName) 40 | 41 | for _, s := range shares { 42 | url, err := Share(s.path) 43 | if (err == nil) != s.ok { 44 | t.Errorf("Share(%s) expected ok=%t, got error=%v", s.path, s.ok, err) 45 | } 46 | 47 | if s.ok && url == "" { 48 | t.Errorf("Share(%s) expected non-empty url", s.path) 49 | } 50 | 51 | if s.ok && url[:4] != "http" { 52 | t.Errorf("Share(%s) expected valid url, got url %s", s.path, url) 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /usage.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plutov/playgo/cbd12ae6217deb9866ebf481a437d7dd377620e6/usage.gif --------------------------------------------------------------------------------