Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.

Commit 4579777

Browse files
committed
Genesis
0 parents  commit 4579777

File tree

10 files changed

+392
-0
lines changed

10 files changed

+392
-0
lines changed

.sail/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM codercom/ubuntu-dev-go
2+
RUN sudo apt-get install -y htop
3+
LABEL project_root "~/go/src/go.coder.com"
4+
5+
# Modules break much of Go's tooling.
6+
ENV GO111MODULE=off

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2019 Coder
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# cli
2+
3+
A minimal, command-oriented CLI packages.
4+
5+
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://godoc.org/go.coder.com/cli)
6+
7+
## Features
8+
9+
- Very small, simple API.
10+
- Uses stdlib as much as possible.
11+
- 0 dependencies.
12+
13+
## Examples
14+
15+
See `examples/` for more.
16+
17+
### Simple CLI
18+
```go
19+
package main
20+
21+
import (
22+
"flag"
23+
"fmt"
24+
25+
"go.coder.com/cli"
26+
)
27+
28+
type cmd struct {
29+
verbose bool
30+
}
31+
32+
func (c *cmd) Run(fl *flag.FlagSet) {
33+
if c.verbose {
34+
fmt.Println("verbose enabled")
35+
}
36+
fmt.Println("we run")
37+
}
38+
39+
func (c *cmd) Spec() cli.CommandSpec {
40+
return cli.CommandSpec{
41+
Name: "simple-example",
42+
Usage: "[flags]",
43+
Desc: `This is a simple example of the cli package.`,
44+
}
45+
}
46+
47+
func (c *cmd) RegisterFlags(fl *flag.FlagSet) {
48+
fl.BoolVar(&c.verbose, "v", false, "sets verbose mode")
49+
}
50+
51+
func main() {
52+
cli.RunRoot(&cmd{})
53+
}
54+
55+
```
56+
57+
### Subcommands
58+
59+
```go
60+
package main
61+
62+
import (
63+
"flag"
64+
"fmt"
65+
66+
"go.coder.com/cli"
67+
)
68+
69+
type subcmd struct {
70+
}
71+
72+
func (c *subcmd) Run(fl *flag.FlagSet) {
73+
fmt.Println("subcommand invoked")
74+
}
75+
76+
func (c *subcmd) Spec() cli.CommandSpec {
77+
return cli.CommandSpec{
78+
Name: "sub",
79+
Usage: "",
80+
Desc: `This is a simple subcommand.`,
81+
}
82+
}
83+
84+
type cmd struct {
85+
message string
86+
}
87+
88+
func (c *cmd) Run(fl *flag.FlagSet) {
89+
// This root command has no default action, so print the help.
90+
fl.Usage()
91+
}
92+
93+
func (c *cmd) Spec() cli.CommandSpec {
94+
return cli.CommandSpec{
95+
Name: "subcommand",
96+
Usage: "[flags]",
97+
Desc: `This is a simple example of subcommands.`,
98+
}
99+
}
100+
101+
func (c *cmd) Subcommands() []cli.Command {
102+
return []cli.Command{
103+
&subcmd{},
104+
}
105+
}
106+
107+
func main() {
108+
cli.RunRoot(&cmd{message: "subcommands"})
109+
}
110+
111+
112+
```

command.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cli
2+
3+
import (
4+
"flag"
5+
"strings"
6+
)
7+
8+
// CommandSpec describes a Command's usage.
9+
//
10+
// It should not list flags.
11+
type CommandSpec struct {
12+
// Name is the name of the command.
13+
// It should be the leaf name of the entire command. E.g `run` for the full
14+
// command `sail run`.
15+
Name string
16+
// Usage is the command's usage string.
17+
// E.g "[flags] <path>"
18+
Usage string
19+
// Desc is the description of the command.
20+
// The first line is used as an abbreviated description.
21+
Desc string
22+
}
23+
24+
// ShortDesc returns the first line of Desc.
25+
func (c CommandSpec) ShortDesc() string {
26+
return strings.Split(c.Desc, "\n")[0]
27+
}
28+
29+
// Command describes a command or subcommand.
30+
type Command interface {
31+
// Spec returns metadata about the command.
32+
Spec() CommandSpec
33+
// Run invokes the command's main routine with parsed flags.
34+
Run(fl *flag.FlagSet)
35+
}
36+
37+
// ParentCommand is an optional interface for commands that have subcommands.
38+
//
39+
// A ParentCommand may pass itself into children as it creates them in order to
40+
// pass high-level configuration and state.
41+
type ParentCommand interface {
42+
Subcommands() []Command
43+
}
44+
45+
// FlaggedCommand is an optional interface for commands that have flags.
46+
type FlaggedCommand interface {
47+
// RegisterFlags lets the command register flags which be sent to Handle.
48+
RegisterFlags(fl *flag.FlagSet)
49+
}

doc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Package cli provides a thin CLI abstraction around the standard flag package.
2+
// It is minimal, command-struct-oriented, and trades off "power" for flexibility
3+
// and clarity at the caller level.
4+
//
5+
// It pretends that Go's single dash (-flag) support doesn't exist, and renders
6+
// helps with --.
7+
//
8+
// Optional interface adherence can be asserted with a statement like
9+
// var _ interface {
10+
// cli.Command
11+
// cli.FlaggedCommand
12+
// cli.ParentCommand
13+
// } = new(rootCmd)
14+
//
15+
// This package is exported since we think it could be interesting to the community,
16+
// but the API is subject to change in support of sail.
17+
package cli

examples/simple/main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
7+
"go.coder.com/cli"
8+
)
9+
10+
type cmd struct {
11+
verbose bool
12+
}
13+
14+
func (c *cmd) Run(fl *flag.FlagSet) {
15+
if c.verbose {
16+
fmt.Println("verbose enabled")
17+
}
18+
fmt.Println("we run")
19+
}
20+
21+
func (c *cmd) Spec() cli.CommandSpec {
22+
return cli.CommandSpec{
23+
Name: "simple-example",
24+
Usage: "[flags]",
25+
Desc: `This is a simple example of the cli package.`,
26+
}
27+
}
28+
29+
func (c *cmd) RegisterFlags(fl *flag.FlagSet) {
30+
fl.BoolVar(&c.verbose, "v", false, "sets verbose mode")
31+
}
32+
33+
func main() {
34+
cli.RunRoot(&cmd{})
35+
}

examples/subcommands/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
7+
"go.coder.com/cli"
8+
)
9+
10+
type subcmd struct {
11+
}
12+
13+
func (c *subcmd) Run(fl *flag.FlagSet) {
14+
fmt.Println("subcommand invoked")
15+
}
16+
17+
func (c *subcmd) Spec() cli.CommandSpec {
18+
return cli.CommandSpec{
19+
Name: "sub",
20+
Usage: "",
21+
Desc: `This is a simple subcommand.`,
22+
}
23+
}
24+
25+
type cmd struct {
26+
message string
27+
}
28+
29+
func (c *cmd) Run(fl *flag.FlagSet) {
30+
// This root command has no default action, so print the help.
31+
fl.Usage()
32+
}
33+
34+
func (c *cmd) Spec() cli.CommandSpec {
35+
return cli.CommandSpec{
36+
Name: "subcommand",
37+
Usage: "[flags]",
38+
Desc: `This is a simple example of subcommands.`,
39+
}
40+
}
41+
42+
func (c *cmd) Subcommands() []cli.Command {
43+
return []cli.Command{
44+
&subcmd{},
45+
}
46+
}
47+
48+
func main() {
49+
cli.RunRoot(&cmd{message: "subcommands"})
50+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module go.coder.com/cli
2+
3+
go 1.12

help.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cli
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io"
7+
"unicode/utf8"
8+
)
9+
10+
func flagDashes(name string) string {
11+
if utf8.RuneCountInString(name) > 1 {
12+
return "--"
13+
}
14+
return "-"
15+
}
16+
17+
func renderFlagHelp(fl *flag.FlagSet, w io.Writer) {
18+
fmt.Fprintf(w, "%v flags:\n", fl.Name())
19+
var count int
20+
fl.VisitAll(func(f *flag.Flag) {
21+
count++
22+
if f.DefValue == "" {
23+
fmt.Fprintf(w, "\t%v%v\t%v\n", flagDashes(f.Name), f.Name, f.Usage)
24+
} else {
25+
fmt.Fprintf(w, "\t%v%v\t%v\t(%v)\n", flagDashes(f.Name), f.Name, f.Usage, f.DefValue)
26+
}
27+
})
28+
if count == 0 {
29+
fmt.Fprintf(w, "\n")
30+
}
31+
}
32+
33+
// renderHelp generates a command's help page.
34+
func renderHelp(cmd Command, fl *flag.FlagSet, w io.Writer) {
35+
// Render usage and description.
36+
fmt.Fprintf(w, "Usage: %v %v\n\n",
37+
fl.Name(), cmd.Spec().Usage,
38+
)
39+
fmt.Fprintf(w, "%v\n\n", cmd.Spec().Desc)
40+
41+
// Render flag help.
42+
renderFlagHelp(fl, w)
43+
44+
// Render subcommand summaries.
45+
pc, ok := cmd.(ParentCommand)
46+
if ok {
47+
if len(pc.Subcommands()) > 0 {
48+
// Give some space from flags.
49+
fmt.Fprintf(w, "\n")
50+
fmt.Fprint(w, "Commands:\n")
51+
}
52+
for _, cmd := range pc.Subcommands() {
53+
fmt.Fprintf(w, "\t%v\t%v\n", cmd.Spec().Name, cmd.Spec().ShortDesc())
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)