goplay

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2023 License: MIT Imports: 6 Imported by: 0

README

goplay

cmd tool to interact with go playground server, see https://github.com/golang/playground to learn more about playground server.

use server play.golang.org by default if no address provided.

install

install cmd tool

$ go install github.com/246859/goplay/cmd/goplay@latest

you can also import goplay to use the client directly

$ go get -u github.com/246859/goplay@latest

usage

help

$ goplay -h
cmd tool to interact with go playground server,
see https://github.com/246859/goplay to learn more about goplay

Usage:
  goplay [flags]
  goplay [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  fmt         fmt code snippet
  health      check whether the playground server is healthy
  help        Help about any command
  run         compile and run code snippet in playground
  share       share your code to go playground
  version     get go version of the playground server
  view        view the specified code snippet

Flags:
  -d, --address string     specified the go playground address (default "https://play.golang.org")
  -h, --help               help for goplay
  -p, --proxy string       proxy address
  -t, --timeout duration   http request timeout (default 20s)
  -v, --version            show goplay version

Use "goplay [command] --help" for more information about a command.

health check

$ goplay health
ok

specify target server

$ goplay -d https://play.golang.org health
ok

check version

$ goplay version
name: Go 1.21
release: go1.21
version: go1.21.4

share your local code to playground

$ goplay share main.go
T9_8fv9CyRh

view specified code snippet in playground

$ goplay view T9_8fv9CyRh
package main

import "fmt"

func main() {
        fmt.Println("hello goplay!")
}

compile your local code in playground

$ goplay run main.go
#1
hello goplay!

you can compile with snippet id

$ goplay run -i T9_8fv9CyRh
#1
hello goplay!

and work with pipeline

$ cat main.go | goplay run
#1
hello goplay!

use client directly

package main

import (
	"github.com/246859/goplay"
	"fmt"
)

func main() {
	client, err := goplay.NewClient(goplay.Options{
		Address: Address,
		Proxy:   Proxy,
		Timeout: Timeout,
	})
	if err != nil {
		panic(err)
	}
	bytes, err := client.View("T9_8fv9CyRh")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(bytes))
}

output

package main

import "fmt"

func main() {
        fmt.Println("hello goplay!")
}

Documentation

Index

Constants

View Source
const (
	FmtUrl            = "/fmt"
	ShareUrl          = "/share"
	VersionUrl        = "/version"
	CompileUrl        = "/compile"
	HealthUrl         = "/_ah/health"
	ViewUrl           = "/p/%s.go"
	DefaultPlayground = "https://play.golang.org"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the go playground http client see https://github.com/golang/playground to learn more about playground server.

func NewClient

func NewClient(option Options, opts ...Option) (*Client, error)

func (*Client) CompileRaw

func (client *Client) CompileRaw(raw []byte, vet bool) (CompileResult, error)

CompileRaw compiles the give code snippet, and returns the result

func (*Client) CompileSnippet

func (client *Client) CompileSnippet(id string, vet bool) (CompileResult, error)

CompileSnippet compiles the specified code snippet, and returns the result

func (*Client) Fmt

func (client *Client) Fmt(id string, fixImport bool) ([]byte, error)

Fmt fmt the specified code snippet and return FmtResult

func (*Client) FmtRaw

func (client *Client) FmtRaw(raw []byte, fixImport bool) ([]byte, error)

FmtRaw fmt the give code snippet and return FmtResult

func (*Client) HealCheck

func (client *Client) HealCheck() (bool, error)

HealCheck checks whether playground server is available

func (*Client) Share

func (client *Client) Share(raw []byte) (string, error)

Share shares the code snippet to go playground and returns snippet id.

func (*Client) Version

func (client *Client) Version() (Version, error)

Version returns the version of playground server

func (*Client) View

func (client *Client) View(id string) ([]byte, error)

View returns the playground code snippet specified by the given id

type CompileResult

type CompileResult struct {
	Errors      string
	Events      []Event
	Status      int
	IsTest      bool
	TestsFailed int

	// VetErrors, if non-empty, contains any vet errors. It is
	// only populated if request.WithVet was true.
	VetErrors string `json:",omitempty"`
	// VetOK reports whether vet ran & passed. It is only
	// populated if request.WithVet was true. Only one of
	// VetErrors or VetOK can be non-zero.
	VetOK bool `json:",omitempty"`
}

type Event

type Event struct {
	Message string
	Kind    string        // "stdout" or "stderr"
	Delay   time.Duration // time to wait before printing Message
}

type FmtResult

type FmtResult struct {
	Body  string
	Error string
}

type Option

type Option func(opt *Options)

func WithAddress

func WithAddress(addr string) Option

func WithProxy

func WithProxy(proxy string) Option

type Options

type Options struct {
	// playground server address
	Address string
	// proxy url
	Proxy string

	// http request timeout duration
	Timeout time.Duration
}

type Version

type Version struct {
	Version, Release, Name string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL