Skip to content

Commit

Permalink
virtctl boiler plate
Browse files Browse the repository at this point in the history
  • Loading branch information
rmohr committed Apr 5, 2017
1 parent fc97311 commit 48cbbad
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cmd/virt-controller/virt-controller
cmd/virt-launcher/virt-launcher
cmd/virt-handler/virt-handler
cmd/virt-api/virt-api
cmd/virtctl/virtctl
manifests/*.yaml
**/bin
bin/*
Expand Down
69 changes: 69 additions & 0 deletions cmd/virtctl/virtctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"os"

"fmt"
flag "github.com/spf13/pflag"
"kubevirt.io/kubevirt/pkg/virtctl"
"log"
)

func main() {

log.SetFlags(0)
log.SetOutput(os.Stderr)

registry := map[string]virtctl.App{
"options": &virtctl.Options{},
}

for cmd, app := range registry {
f := app.FlagSet()
f.Bool("help", false, "Print usage.")
f.MarkHidden("help")
flags, err := Parse(cmd, f)

if err != nil {
if flags == nil {
// No subcommand specified
break
}
}
if flags != nil {
h, _ := flags.GetBool("help")
if h || err != nil {
fmt.Fprint(os.Stderr, app.Usage())
return
}
os.Exit(app.Run(flags))
}
}

Usage()
os.Exit(1)
}

func Parse(cmd string, flags *flag.FlagSet) (*flag.FlagSet, error) {
flags.AddFlagSet((&virtctl.Options{}).FlagSet())
err := flags.Parse(os.Args[1:])
if len(flags.Args()) == 0 {
return nil, fmt.Errorf("No subcommand found")
}
foundCmd := flags.Arg(0)
if foundCmd != cmd {
return nil, nil
}
return flags, err
}

func Usage() {
fmt.Fprintln(os.Stderr,
`virtctl controll VM related operations on your kubernetes cluster.
Basic Commands:
Use "virtctl <command> --help" for more information about a given command.
Use "virtctl options" for a list of global command-line options (applies to all commands).
`)
}
2 changes: 1 addition & 1 deletion hack/config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
binaries="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api"
binaries="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virtctl"
docker_images="$binaries images/haproxy images/iscsi-demo-target-tgtd images/vm-killer"
docker_prefix=kubevirt
docker_tag=${DOCKER_TAG:-latest}
Expand Down
36 changes: 36 additions & 0 deletions pkg/virtctl/virtctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package virtctl

import (
"fmt"
flag "github.com/spf13/pflag"
"os"
)

type App interface {
FlagSet() *flag.FlagSet
Run(flags *flag.FlagSet) int
Usage() string
}

type Options struct {
}

func (o *Options) FlagSet() *flag.FlagSet {

cf := flag.NewFlagSet("options", flag.ExitOnError)
cf.StringP("server", "s", "", "The address and port of the Kubernetes API server")
cf.StringP("namespace", "n", "default", "If present, the namespace scope for this CLI request")
cf.String("kubeconfig", "", "Path to the kubeconfig file to use for CLI requests")
return cf
}

func (o *Options) Run(flags *flag.FlagSet) int {
fmt.Fprintln(os.Stderr, o.Usage())
return 0
}

func (o *Options) Usage() string {
usage := "The following options can be passed to any command:\n\n"
usage += o.FlagSet().FlagUsages()
return usage
}
6 changes: 6 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@
"revision": "b061729afc07e77a8aa4fad0a2fd840958f1942a",
"revisionTime": "2016-09-27T10:08:44Z"
},
{
"checksumSHA1": "WZHF26m8UJ3WU5//TIDeUC3xs4o=",
"path": "github.com/spf13/pflag",
"revision": "d16db1e50e33dff1b6cdf37596cef36742128670",
"revisionTime": "2017-03-27T14:13:44Z"
},
{
"checksumSHA1": "xiderUuvye8Kpn7yX3niiJg32bE=",
"path": "golang.org/x/crypto/ssh/terminal",
Expand Down

0 comments on commit 48cbbad

Please sign in to comment.