forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters