squash merge from dev branch

This commit is contained in:
Steve Brunton
2017-09-04 22:52:14 -04:00
parent 123cd935a3
commit ed916703c6
1058 changed files with 311598 additions and 3 deletions

View File

@@ -0,0 +1,20 @@
package main
import (
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
debug = kingpin.Flag("debug", "Enable debug mode.").Bool()
timeout = kingpin.Flag("timeout", "Timeout waiting for ping.").Default("5s").OverrideDefaultFromEnvar("PING_TIMEOUT").Short('t').Duration()
ip = kingpin.Arg("ip", "IP address to ping.").Required().IP()
count = kingpin.Arg("count", "Number of packets to send").Int()
)
func main() {
kingpin.Version("0.0.1")
kingpin.Parse()
fmt.Printf("Would ping: %s with timeout %s and count %d", *ip, *timeout, *count)
}

View File

@@ -0,0 +1,38 @@
package main
import (
"os"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("chat", "A command-line chat application.")
debug = app.Flag("debug", "Enable debug mode.").Bool()
serverIP = app.Flag("server", "Server address.").Default("127.0.0.1").IP()
register = app.Command("register", "Register a new user.")
registerNick = register.Arg("nick", "Nickname for user.").Required().String()
registerName = register.Arg("name", "Name of user.").Required().String()
post = app.Command("post", "Post a message to a channel.")
postImage = post.Flag("image", "Image to post.").File()
postChannel = post.Arg("channel", "Channel to post to.").Required().String()
postText = post.Arg("text", "Text to post.").Strings()
)
func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
// Register user
case register.FullCommand():
println(*registerNick)
// Post message
case post.FullCommand():
if *postImage != nil {
}
text := strings.Join(*postText, " ")
println("Post:", text)
}
}

View File

@@ -0,0 +1,96 @@
package main
import (
"fmt"
"os"
"github.com/alecthomas/kingpin"
)
func listHosts() []string {
// Provide a dynamic list of hosts from a hosts file or otherwise
// for bash completion. In this example we simply return static slice.
// You could use this functionality to reach into a hosts file to provide
// completion for a list of known hosts.
return []string{"sshhost.example", "webhost.example", "ftphost.example"}
}
type NetcatCommand struct {
hostName string
port int
format string
}
func (n *NetcatCommand) run(c *kingpin.ParseContext) error {
fmt.Printf("Would have run netcat to hostname %v, port %d, and output format %v\n", n.hostName, n.port, n.format)
return nil
}
func configureNetcatCommand(app *kingpin.Application) {
c := &NetcatCommand{}
nc := app.Command("nc", "Connect to a Host").Action(c.run)
nc.Flag("nop-flag", "Example of a flag with no options").Bool()
// You can provide hint options using a function to generate them
nc.Flag("host", "Provide a hostname to nc").
Required().
HintAction(listHosts).
StringVar(&c.hostName)
// You can provide hint options statically
nc.Flag("port", "Provide a port to connect to").
Required().
HintOptions("80", "443", "8080").
IntVar(&c.port)
// Enum/EnumVar options will be turned into completion options automatically
nc.Flag("format", "Define the output format").
Default("raw").
EnumVar(&c.format, "raw", "json")
// You can combine HintOptions with HintAction too
nc.Flag("host-with-multi", "Define a hostname").
HintAction(listHosts).
HintOptions("myhost.com").
String()
// And combine with themselves
nc.Flag("host-with-multi-options", "Define a hostname").
HintOptions("myhost.com").
HintOptions("myhost2.com").
String()
// If you specify HintOptions/HintActions for Enum/EnumVar, the options
// provided for Enum/EnumVar will be overridden.
nc.Flag("format-with-override-1", "Define a format").
HintAction(listHosts).
Enum("option1", "option2")
nc.Flag("format-with-override-2", "Define a format").
HintOptions("myhost.com", "myhost2.com").
Enum("option1", "option2")
}
func addSubCommand(app *kingpin.Application, name string, description string) {
c := app.Command(name, description).Action(func(c *kingpin.ParseContext) error {
fmt.Printf("Would have run command %s.\n", name)
return nil
})
c.Flag("nop-flag", "Example of a flag with no options").Bool()
}
func main() {
app := kingpin.New("completion", "My application with bash completion.")
app.Flag("flag-1", "").String()
app.Flag("flag-2", "").HintOptions("opt1", "opt2").String()
configureNetcatCommand(app)
// Add some additional top level commands
addSubCommand(app, "ls", "Additional top level command to show command completion")
addSubCommand(app, "ping", "Additional top level command to show command completion")
addSubCommand(app, "nmap", "Additional top level command to show command completion")
kingpin.MustParse(app.Parse(os.Args[1:]))
}

View File

@@ -0,0 +1,105 @@
// A curl-like HTTP command-line client.
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
timeout = kingpin.Flag("timeout", "Set connection timeout.").Short('t').Default("5s").Duration()
headers = HTTPHeader(kingpin.Flag("headers", "Add HTTP headers to the request.").Short('H').PlaceHolder("HEADER=VALUE"))
get = kingpin.Command("get", "GET a resource.").Default()
getFlag = get.Flag("test", "Test flag").Bool()
getURL = get.Command("url", "Retrieve a URL.").Default()
getURLURL = getURL.Arg("url", "URL to GET.").Required().URL()
getFile = get.Command("file", "Retrieve a file.")
getFileFile = getFile.Arg("file", "File to retrieve.").Required().ExistingFile()
post = kingpin.Command("post", "POST a resource.")
postData = post.Flag("data", "Key-value data to POST").Short('d').PlaceHolder("KEY:VALUE").StringMap()
postBinaryFile = post.Flag("data-binary", "File with binary data to POST.").File()
postURL = post.Arg("url", "URL to POST to.").Required().URL()
)
type HTTPHeaderValue http.Header
func (h HTTPHeaderValue) Set(value string) error {
parts := strings.SplitN(value, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("expected HEADER=VALUE got '%s'", value)
}
(http.Header)(h).Add(parts[0], parts[1])
return nil
}
func (h HTTPHeaderValue) String() string {
return ""
}
func HTTPHeader(s kingpin.Settings) (target *http.Header) {
target = &http.Header{}
s.SetValue((*HTTPHeaderValue)(target))
return
}
func applyRequest(req *http.Request) error {
req.Header = *headers
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("HTTP request failed: %s", resp.Status)
}
_, err = io.Copy(os.Stdout, resp.Body)
return err
}
func apply(method string, url string) error {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return err
}
return applyRequest(req)
}
func applyPOST() error {
req, err := http.NewRequest("POST", (*postURL).String(), nil)
if err != nil {
return err
}
if len(*postData) > 0 {
for key, value := range *postData {
req.Form.Set(key, value)
}
} else if postBinaryFile != nil {
if headers.Get("Content-Type") != "" {
headers.Set("Content-Type", "application/octet-stream")
}
req.Body = *postBinaryFile
} else {
return errors.New("--data or --data-binary must be provided to POST")
}
return applyRequest(req)
}
func main() {
kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Version("1.0").Author("Alec Thomas")
kingpin.CommandLine.Help = "An example implementation of curl."
switch kingpin.Parse() {
case "get url":
kingpin.FatalIfError(apply("GET", (*getURLURL).String()), "GET failed")
case "post":
kingpin.FatalIfError(applyPOST(), "POST failed")
}
}

View File

@@ -0,0 +1,30 @@
package main
import (
"fmt"
"os"
"gopkg.in/alecthomas/kingpin.v2"
)
// Context for "ls" command
type LsCommand struct {
All bool
}
func (l *LsCommand) run(c *kingpin.ParseContext) error {
fmt.Printf("all=%v\n", l.All)
return nil
}
func configureLsCommand(app *kingpin.Application) {
c := &LsCommand{}
ls := app.Command("ls", "List files.").Action(c.run)
ls.Flag("all", "List all files.").Short('a').BoolVar(&c.All)
}
func main() {
app := kingpin.New("modular", "My modular application.")
configureLsCommand(app)
kingpin.MustParse(app.Parse(os.Args[1:]))
}

View File

@@ -0,0 +1,20 @@
package main
import (
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
debug = kingpin.Flag("debug", "Enable debug mode.").Bool()
timeout = kingpin.Flag("timeout", "Timeout waiting for ping.").OverrideDefaultFromEnvar("PING_TIMEOUT").Required().Short('t').Duration()
ip = kingpin.Arg("ip", "IP address to ping.").Required().IP()
count = kingpin.Arg("count", "Number of packets to send").Int()
)
func main() {
kingpin.Version("0.0.1")
kingpin.Parse()
fmt.Printf("Would ping: %s with timeout %s and count %d", *ip, *timeout, *count)
}