• Stars
    star
    525
  • Rank 82,472 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Gosh - a pluggable framework for building command shell programs

Gosh - A pluggable interactive shell written Go

Gosh (or Go shell) is a framework that uses Go's plugin system to create for building interactive console-based shell programs. A gosh shell is comprised of a collection of Go plugins which implement one or more commands. When gosh starts, it searches directory ./plugins for available shared object files that implement command plugins.

Getting started

Pre-requisites

  • Go 1.8 or above
  • Linux
  • Mac OSX

Gosh makes it easy to create shell programs. First, download or clone this repository. For a quick start, run the following:

go run shell/gosh.go

This will produce the following output:

                        888
                        888
                        888
 .d88b.  .d88b. .d8888b 88888b.
d88P"88bd88""88b88K     888 "88b
888  888888  888"Y8888b.888  888
Y88b 888Y88..88P     X88888  888
 "Y88888 "Y88P"  88888P'888  888
     888
Y8b d88P
 "Y88P"

No commands found

After the splashscreen is displayed, gosh informs you that no commands found, as expected. Next, exit the gosh shell (Ctrl-C) and let us compile the example plugins that comes with the source code.

go build -buildmode=plugin  -o plugins/sys_command.so plugins/syscmd.go

The previous command will compile plugins/syscmd.go and outputs shared object plugins/sys_command.so, as a Go plugin file. Verify the shared object file was created:

> ls -lh plugins/
total 3.2M
-rw-rw-r-- 1  4.5K Mar 19 18:23 syscmd.go
-rw-rw-r-- 1  3.2M Mar 19 19:14 sys_command.so
-rw-rw-r-- 1  1.4K Mar 19 18:23 testcmd.go

Now, when gosh is restarted, it will dynamically load the commands implemented in the shared object file:

> go run shell/gosh.go
...

Loaded 4 command(s)...
Type help for available commands

gosh>

As indicated, typing help lists all available commands in the shell:

gosh> help

help: prints help information for other commands.

Available commands
------------------
      prompt:	sets a new shell prompt
         sys:	sets a new shell prompt
        help:	prints help information for other commands.
        exit:	exits the interactive shell immediately

Use "help <command-name>" for detail about the specified command

A command

A Gosh Command is represented by type api/Command:

type Command interface {
	Name() string
	Usage() string
	ShortDesc() string
	LongDesc() string
	Exec(context.Context, []string) (context.Context, error)
}

The Gosh framework searches for Go plugin files in the ./plugins directory. Each package plugin must export a variable named Commands which is of type :

type Commands interface {
  ...
	Registry() map[string]Command
}

Type Commands type returns a list of Command via the Registry().

The following shows example command file plugins/testcmd.go. It implements two commands via types helloCmd and goodbyeCmd. The commands are exported via type testCmds using method Registry():

package main

import (
	"context"
	"fmt"
	"io"

	"github.com/vladimirvivien/gosh/api"
)

type helloCmd string

func (t helloCmd) Name() string      { return string(t) }
func (t helloCmd) Usage() string     { return `hello` }
func (t helloCmd) ShortDesc() string { return `prints greeting "hello there"` }
func (t helloCmd) LongDesc() string  { return t.ShortDesc() }
func (t helloCmd) Exec(ctx context.Context, args []string) (context.Context, error) {
	out := ctx.Value("gosh.stdout").(io.Writer)
	fmt.Fprintln(out, "hello there")
	return ctx, nil
}

type goodbyeCmd string

func (t goodbyeCmd) Name() string      { return string(t) }
func (t goodbyeCmd) Usage() string     { return t.Name() }
func (t goodbyeCmd) ShortDesc() string { return `prints message "bye bye"` }
func (t goodbyeCmd) LongDesc() string  { return t.ShortDesc() }
func (t goodbyeCmd) Exec(ctx context.Context, args []string) (context.Context, error) {
	out := ctx.Value("gosh.stdout").(io.Writer)
	fmt.Fprintln(out, "bye bye")
	return ctx, nil
}

// command module
type testCmds struct{}

func (t *testCmds) Init(ctx context.Context) error {
	out := ctx.Value("gosh.stdout").(io.Writer)
	fmt.Fprintln(out, "test module loaded OK")
	return nil
}

func (t *testCmds) Registry() map[string]api.Command {
	return map[string]api.Command{
		"hello":   helloCmd("hello"),
		"goodbye": goodbyeCmd("goodbye"),
	}
}

var Commands testCmds

License

MIT

More Repositories

1

go-cshared-examples

Calling Go Functions from Other Languages using C Shared Libraries
Dart
849
star
2

automi

A stream processing API for Go (alpha)
Go
791
star
3

ktop

A top-like tool for your Kubernetes clusters
Go
676
star
4

go-plugin-example

Playing around with Go 1.8 plugin system
Go
318
star
5

go-grpc

A collection of gRPC and Go examples showcasing features of the framework
Go
240
star
6

learning-go

Source code repository for my book "Learning Go Programming"
Go
232
star
7

go4vl

A Go library for working with the Video for Linux API (V4L2).
C
228
star
8

go-networking

Code sample for Learning Network Programming with Go
Go
213
star
9

gowfs

A Go client binding for Hadoop HDFS using WebHDFS.
Go
134
star
10

clamshell-cli

A framework to build command-line console applications in Java
Java
134
star
11

k8s-client-examples

Building stuff with the Kubernetes API
Go
116
star
12

gexe

Script-like OS interaction wrapped in the security and type safety of the Go programming language
Go
71
star
13

iot-dev

Example IoT projects
Go
68
star
14

jmx-cli

[Project Inactive] Jmx-Cli is a command-line interface console for JMX
Java
66
star
15

go-ntp-client

A Network Time Protocol client in Go
Go
50
star
16

gomes

Pure Go Framework API for Apache Mesos
Go
33
star
17

workbench

My code collection for testing new ideas, blog examples, etc
Java
32
star
18

go-tar

Examples using archive/tar compress/gz Go packages
Go
17
star
19

go-binary

Examples using encoding/binary package
Go
16
star
20

streaming-runtime-go

Go
11
star
21

docker.io-recipes

Some favorite Docker.Io recipes
9
star
22

go-tutorials

A place for quick Go tutorials
Go
5
star
23

dapr-examples

Examples of Dapr distributed services in Go
Go
5
star
24

startype

Roundtrip automatic conversion of Starlark-Go API types to regular Go types and back🤩
Go
4
star
25

go-algorithms

Classic CS algorithms examples in Go
Go
4
star
26

embedding-starlark

Examples of how to embed Starlark in Go programs using the Starlark-Go project
Go
4
star
27

mesos-http

Example of Mesos HTTP API
Protocol Buffer
3
star
28

jmx-logger

JMX Logger for JUL and Log4J (old project & little support)
Java
3
star
29

go-httpmux-example

Example to show use of the new enhanced http.ServeMux router in Go v1.22.0 or later
Go
2
star
30

kob

kob simplifies the programmatic construction of Kubernetes API object graphs
Go
2
star
31

gophercon2022

GopherCon 2022 - reveal.js presentation
JavaScript
2
star
32

timeapp

A simple application to print time based on configured time layout (perfect Kubernetes sample app)
Go
2
star
33

go-in-10

Go
2
star
34

mqt

MQT = Mesos Query Tool
Go
1
star
35

cloudy-apps

Cloud native application examples
Go
1
star
36

emojiis

Emojiis is a Go module for emoji icon search
Go
1
star
37

knative-workbench

Playing around with knative examples
Go
1
star
38

libstorage-client

Sample code on writing libstorage client code
Go
1
star
39

mango

Playground for an automated build tool in Go
Go
1
star
40

pourover

simple http reverse proxy
Go
1
star
41

vladimirvivien

1
star
42

go-tour

Examples and test code I use to tour the Go language and packages
Go
1
star
43

homebrew-oss-tools

Homebrew repository for distributing OSS binaries.
Ruby
1
star
44

go-workbench

A playground for Go proof of concepts
Go
1
star
45

horizon

Framework for building distributed apps
Go
1
star
46

e2eframework-controller-example

Repository for showing how to test Kubebuilder's Cronjob example controller using the e2e-framework - https://github.com/kubernetes-sigs/e2e-framework
Go
1
star