From d41c4b0638c5997c6f102897d8f88d86643794b9 Mon Sep 17 00:00:00 2001 From: rwxrob Date: Thu, 24 Mar 2022 08:32:32 -0400 Subject: [PATCH] Add initial commands --- README.md | 48 +++++++++++---------------- foo.go | 82 ---------------------------------------------- foo/main.go | 5 --- foo_test.go | 32 ------------------ go.mod | 20 ++++++++++- go.sum | 25 ++++++++++++++ go.work | 16 ++------- go.work.sum | 12 +++++++ main.go | 20 +++++++++++ own.go | 21 ------------ testdata/bar/file1 | 0 testdata/bar/file2 | 0 12 files changed, 98 insertions(+), 183 deletions(-) delete mode 100644 foo.go delete mode 100644 foo/main.go delete mode 100644 foo_test.go create mode 100644 go.sum create mode 100644 go.work.sum create mode 100644 main.go delete mode 100644 own.go delete mode 100644 testdata/bar/file1 delete mode 100644 testdata/bar/file2 diff --git a/README.md b/README.md index 8892b64..1e738e9 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,30 @@ -# Bonzai™ Sample `foo` Command (Template) +# Personal Bonzai Command Tree (Monolith) -*Create a new GitHub project using this template and change this -README.md to match your project. Make all your template changes before -making your first commit.* - -![WIP](https://img.shields.io/badge/status-wip-red) -![Go Version](https://img.shields.io/github/go-mod/go-version/rwxrob/foo) -[![GoDoc](https://godoc.org/github.com/rwxrob/foo?status.svg)](https://godoc.org/github.com/rwxrob/foo) +[![GoDoc](https://godoc.org/github.com/rwxrob/cmds?status.svg)](https://godoc.org/github.com/rwxrob/cmds) [![License](https://img.shields.io/badge/license-Apache2-brightgreen.svg)](LICENSE) -## Install - -This command can be installed as a standalone program or composed into -a Bonzai command tree. +These days I prefer to maintain a single Go monolith command utility +with everything I would have used shell scripts for before. I created +[Bonzai](https://github.com/rwxrob/bonzai) specifically for this sort of +thing. This way I just have to copy a single binary over to whatever +system I'm working on and I have all of my favorite functionality on +*any* device that Go supports with zero compatibility hassles and +installation dependencies. It just works, and by works I mean +*anywhere*. Hell, I don't even need a container (but can easily make a +FROM SCRATCH container with nothing but `cmds` in it). If I want a +subset of the commands I just trim the tree and compose them into a +different monolith --- in minutes. -Standalone +## Install ``` -go install github.com/rwxrob/foo/foo@latest +go install github.com/rwxrob/cmds@latest ``` -Composed +I have `z` hard link as well to keep things easy to type. -```go -package cmds - -import ( - "github.com/rwxrob/bonzai" - "github.com/rwxrob/foo" -) - -var Cmd = &bonzai.Cmd{ - Name: `cmds`, - Commands: []*bonzai.Cmd{help.Cmd, foo.Cmd}, -} +``` +ln "$GOBIN/cmds" "$GOBIN/z" ``` ## Tab Completion @@ -43,7 +34,8 @@ To activate bash completion just use the `complete -C` option from your completion is done by the program itself. ``` -complete -C foo foo +complete -C cmds cmds +complete -C z z ``` If you don't have bash or tab completion check use the shortcut diff --git a/foo.go b/foo.go deleted file mode 100644 index 3737bd6..0000000 --- a/foo.go +++ /dev/null @@ -1,82 +0,0 @@ -package foo - -import ( - "log" - - "github.com/rwxrob/bonzai" - "github.com/rwxrob/bonzai/comp" - "github.com/rwxrob/bonzai/inc/help" -) - -var Cmd = &bonzai.Cmd{ - - Name: `foo`, - Summary: `just a sample foo command`, - Usage: `[B|bar|own|h|help]`, - Version: `v0.0.1`, - Copyright: `Copyright 2021 Robert S Muhlestein`, - License: `Apache-2.0`, - Commands: []*bonzai.Cmd{help.Cmd, Bar, own}, - - Description: ` - The foo commands do foo stuff. You can start the description here - and wrap it to look nice and it will just work. Descriptions are - written in BonzaiMark™, a simplification of CommonMark that that - mostly follows Go documentation guidelines`, - - Other: map[string]string{ - `foo`: `something about foo`, - `another`: `something about another command`, - }, - - // no Call since has Commands, if had Call would only call if - // commands didn't match -} - -// Commands can be grouped into the same file or separately, public or -// private. Public let's others compose specific subcommands (foo.Bar), -// private just keeps it composed and only available within this Bonzai -// command. - -// Aliases are not commands but will be replaced by their target names. - -var Bar = &bonzai.Cmd{ - Name: `bar`, - Aliases: []string{"B", "notbar"}, // to make a point - Commands: []*bonzai.Cmd{help.Cmd, file}, - - // Call first-class functions can be highly detailed, refer to an - // existing function someplace else, or can call high-level package - // library functions. Developers are encouraged to consider well where - // they maintain the core logic of their applications. Often, it will - // not be here within the bonzai.Cmd definition. One use case for - // decoupled first-class Call functions is when creating multiple - // binaries for different target languages. In such cases this - // bonzai.Cmd definition is essentially just a wrapper for - // documentation and other language-specific embedded assets. - - Call: func(_ *bonzai.Cmd, _ ...string) error { // note conventional _ - log.Printf("would bar stuff") - return nil - }, -} - -// Different completion methods are be set including the expected -// standard ones from bash and other shells. Not that these completion -// methods only work if the shell supports completion (including -// the Bonzai Shell, which can be set as the default Cmd to provide rich -// shell interactivity where normally no shell is available, such as in -// FROM SCRATCH containers that use a Bonzai tree as the core binary). - -var file = &bonzai.Cmd{ - Name: `file`, - Commands: []*bonzai.Cmd{help.Cmd}, - Completer: comp.File, - Call: func(x *bonzai.Cmd, args ...string) error { - if len(args) == 0 { - return x.UsageError() - } - log.Printf("would show file information about %v", args[0]) - return nil - }, -} diff --git a/foo/main.go b/foo/main.go deleted file mode 100644 index 1862548..0000000 --- a/foo/main.go +++ /dev/null @@ -1,5 +0,0 @@ -package main - -import "github.com/rwxrob/foo" - -func main() { foo.Cmd.Run() } diff --git a/foo_test.go b/foo_test.go deleted file mode 100644 index e4d928b..0000000 --- a/foo_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package foo - -import ( - "bytes" - "log" - "os" - "testing" -) - -// Unlike other Go projects, Bonzai commands don't really benefit from -// Go's example-based tests (which normally would be in package -// foo_test). Instead, testing should be against the first-class Call -// functions directly (with nil callers) or the high-level functions -// from a package library into which they call themselves (which might -// have their own example and other tests). - -func TestBar(t *testing.T) { - - // capture the output - buf := new(bytes.Buffer) - log.SetFlags(0) - log.SetOutput(buf) - defer log.SetFlags(log.Flags()) - defer log.SetOutput(os.Stderr) - - Bar.Call(nil) - - t.Log(buf) - if buf.String() != "would bar stuff\n" { - t.Fail() - } -} diff --git a/go.mod b/go.mod index d1d8cdf..3df73f3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,21 @@ -module github.com/rwxrob/bonzai-foo +module github.com/rwxrob/z go 1.18 + +require ( + github.com/rwxrob/bonzai v0.0.19 + github.com/rwxrob/yaml2json v0.1.0 +) + +require ( + github.com/rwxrob/fn v0.1.0 // indirect + github.com/rwxrob/fs v0.2.0 // indirect + github.com/rwxrob/json v0.4.1 // indirect + github.com/rwxrob/structs v0.5.0 // indirect + github.com/rwxrob/term v0.1.2 // indirect + github.com/rwxrob/to v0.2.1 // indirect + golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect + golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect + golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2dd360b --- /dev/null +++ b/go.sum @@ -0,0 +1,25 @@ +github.com/rwxrob/bonzai v0.0.19 h1:GvSUifZqqnjhIAEDGHnq2v7xyJxHhoBa589zrJgiVYI= +github.com/rwxrob/bonzai v0.0.19/go.mod h1:gkNuRMH3DrSpVR4OJBPxxxt26srZijD9a/Y9uc1p8Ko= +github.com/rwxrob/fn v0.1.0 h1:vlQOPxZ77IlXSIDu2eIkuQgMtInlMNRHqwgKfkNAHAA= +github.com/rwxrob/fn v0.1.0/go.mod h1:omPqOqEB+dDna09z5pi5YFxq4IZqDvv3wFPUCES5LvY= +github.com/rwxrob/fs v0.2.0 h1:f0AO9BgsWlYGwiBJH7W60k3AJh0rGOtYZuozkUCQVRw= +github.com/rwxrob/fs v0.2.0/go.mod h1:vO8AeluD7rnrO7zC54745xTEBFgHPUpHL0hbp1NnsVo= +github.com/rwxrob/json v0.4.1 h1:b4ToZe4mrQO8rRL/kRFglzZszyZZnGv6JRHj6jrI3f4= +github.com/rwxrob/json v0.4.1/go.mod h1:DU3TQKCWY4bK7sQ0wu80cRmTs96b6M//OYvT7Eg2mJA= +github.com/rwxrob/structs v0.5.0 h1:pjLsfyYHS+gB1CtzRj3H39wRYL4lI5pTpFf8kl91guw= +github.com/rwxrob/structs v0.5.0/go.mod h1:2gIte2ZI9zUok6q6F3v3l41ZXo7Zg5Kf1GUTP2+pXyQ= +github.com/rwxrob/term v0.1.2 h1:w71H8seLUS/E5PKtg5VjSlEqv5xPXLYwwQ8FZ+zb3OA= +github.com/rwxrob/term v0.1.2/go.mod h1:IVE7hG+VQlM4R+kS4pY6uhfMHoG0QECrZF7d7bKcdsk= +github.com/rwxrob/to v0.2.1 h1:ZYBNEa8LJT5VDQUHm2wSwiRf61xSqU87UqbYvitV3eY= +github.com/rwxrob/to v0.2.1/go.mod h1:8qdgCWkh50Avs8sRpV6/P7lAQgVf3KLRSKMZahV/W48= +github.com/rwxrob/yaml2json v0.1.0 h1:9ZyxAHZaL1n88tmTDKddz/o2vx3j3ygPuUFfmV85BNs= +github.com/rwxrob/yaml2json v0.1.0/go.mod h1:LZv0PcOqzj7q7afI3YRiPogmLHAdv579f/YM/6vcQrg= +golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s= +golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs= +golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/go.work b/go.work index 9315fe2..3350b9b 100644 --- a/go.work +++ b/go.work @@ -1,19 +1,7 @@ -go 1.18 - -// Add relative paths to repos of things you don't want to `go get` -// every time. Always include . or example tests requiring full -// references to package imports will fail. Read go work -h for more. -// Packages upon which bonzai depends have been included for convenience -// but can be removed without worry by most. +go 1.19 use ( . ../bonzai - ../fn - ../fs - ../json - ../scan - ../structs - ../term - ../to + ../yaml2json ) diff --git a/go.work.sum b/go.work.sum new file mode 100644 index 0000000..9bbbe1c --- /dev/null +++ b/go.work.sum @@ -0,0 +1,12 @@ +github.com/rwxrob/to v0.1.1 h1:CTwNL00Vm9j3fCHSKVYQwGnV7r/DFZdmNhI7vA1ktQw= +github.com/rwxrob/to v0.1.1/go.mod h1:8qdgCWkh50Avs8sRpV6/P7lAQgVf3KLRSKMZahV/W48= +golang.org/x/crypto v0.0.0-20220307211146-efcb8507fb70 h1:syTAU9FwmvzEoIYMqcPHOcVm4H3U5u90WsvuYgwpETU= +golang.org/x/crypto v0.0.0-20220307211146-efcb8507fb70/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..ef93c74 --- /dev/null +++ b/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/rwxrob/bonzai" + "github.com/rwxrob/bonzai/inc/help" + "github.com/rwxrob/yaml2json" +) + +func main() { + Cmd.Run() +} + +var Cmd = &bonzai.Cmd{ + Name: `z`, + Summary: `rwxrob's bonzai command tree`, + Version: `v0.0.1`, + Copyright: `Copyright 2021 Robert S Muhlestein`, + License: `Apache-2.0`, + Commands: []*bonzai.Cmd{help.Cmd, yaml2json.Cmd}, +} diff --git a/own.go b/own.go deleted file mode 100644 index 94d870e..0000000 --- a/own.go +++ /dev/null @@ -1,21 +0,0 @@ -package foo - -// Go treats all files as if they are, more or less, in the same large -// file. Create separate files to help you and others find the code you -// need quickly. - -import ( - "log" - - // if typing bonzai becomes tedious use capital Z as a convention - - Z "github.com/rwxrob/bonzai" -) - -var own = &Z.Cmd{ - Name: `own`, - Call: func(caller *Z.Cmd, none ...string) error { - log.Print("I'm in my own file.") - return nil - }, -} diff --git a/testdata/bar/file1 b/testdata/bar/file1 deleted file mode 100644 index e69de29..0000000 diff --git a/testdata/bar/file2 b/testdata/bar/file2 deleted file mode 100644 index e69de29..0000000