gobra

package module
v0.0.0-...-cd37d0a Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2018 License: MIT Imports: 15 Imported by: 2

README

Gobra

Gobra generates web interfaces to interact with cobra

Usage

There are two parts to Gobra: generating client-side interface and serving a HTTP API.

Generating client-side HTML

Gobra has a CommandFromCobra struct which takes in two arguments: a cobra.Command and a string which is the address for the server the client-side will connect to. If the address is left blank, the client will connect to the same server it resides.

Calling CommandFromCobra.Render() will return an HTML string, which you can use to insert into your already existing webpage.

Running the API

Gobra also has a Server struct which takes in 4 arguments: cobra.Command, port number, AllowCORS and Frontless.

The cobra.Command argument should be the same one that you give to CommandFromGobra. AllowCORS is a bool determines whether the API will have Access-Allow-Control-Origin: * or not. If Frontless is true, Gobra will not serve the index.html file from the folder it's run.

Example

Here is an example in the case where you would run both the client-side and API on the same server:

import (
	"github.com/ctessum/gobra"
	"html/template"
	"github.com/ctessum/gobra/example/cmd"
)

func main () {
	const wrapper = `
		[Your webpage's HTML here]
			{{.}}
		[...]
	`
	output := template.Must(template.New("outputPage").Parse(wrapper))

	cmd := &gobra.CommandFromCobra{ cmd.Root, "" }

	val, err := cmd.Render();
	if err != nil {
		panic(err)
	}

	// We dump this HTML file into the current folder
	f, err := os.Create("index.html")
	output.Execute(f, template.HTML(val))

	// The server will start serving the index.html file we've just made
	// on port 8080, and the API will be served from /gobra as well.
	server := gobra.Server { cmd.Root, 8080, false, false }
	server.Start()
}

API endpoints

If you decide to use Gobra only as a server, the API endpoint works like so:

If the command you want to run is: app math add --num1=3 --num2=6

You would want to make a GET request to: //<serverAddress>/app/math/add?num1=3&num2=6

In case you'd like to upload a file to the server, the endpoint /upload is for this purpose. Send a POST request with the file under the field data, and it'll return you with a JSON including the local filepath under path.

Your cobra Flag must be registered using MakeFlagUploadable for the web interface to enable a file upload field

Documentation

Overview

Package gobra is an HTML-based graphical user interface (GUI) for the cobra command line interface (CLI; github.com/spf13/cobra).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// Root is the Cobra command tree root
	Root *cobra.Command

	// ServerAddress is the address that the front-end will communicate with.
	ServerAddress string

	// Allow Cross-Origin. If set to true, everyone can use the Gobra instance on client-side
	// Set this to true if you're planning to expose the API to public.
	AllowCORS bool

	// HTML is an HTML template.
	// If this is not nil, it will be served as an HTML front end.
	HTML *template.Template

	// FileUploadFunc is a function that stores uploaded files and returns the
	// stored location. The default FileUploadFunc saves files in a temporary
	// directory.
	FileUploadFunc func(data io.Reader, name string) (filename string, err error)

	// PreRun, if not nil, will be run before executing the given commands with
	// the given flags.
	PreRun func(commands *[]string, flags *url.Values) error
	// contains filtered or unexported fields
}

Server struct/class that holds configuration for a Cobra back-end instance

func (*Server) MakeFlagUploadable

func (s *Server) MakeFlagUploadable(names ...string)

MakeFlagUploadable registers the given flag name(s) as allowing file uploads. Once a flag is registered, a file input will appear next to it in the user interface, allowing the user to upload a file for the flag argument.

func (*Server) Render

func (s *Server) Render(w io.Writer) error

Render renders the view of the command. If the HTML field of the receiver is not nil, it will render the whole page, otherwise it will just render the gobra section.

func (*Server) Start

func (s *Server) Start() error

Start starts the server.

func (*Server) Write

func (s *Server) Write(p []byte) (n int, err error)

Write method makes Server implements io.Writer. It also transforms the input bytes to string then sends it to the websocket.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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