cmd/hyperkube: add make-symlinks flag

This commit is contained in:
Patrick Baxter
2016-04-27 18:07:59 -07:00
parent 3d435b56be
commit 4d35231bd6
2 changed files with 39 additions and 5 deletions

View File

@@ -45,6 +45,7 @@ type HyperKube struct {
baseFlags *pflag.FlagSet baseFlags *pflag.FlagSet
out io.Writer out io.Writer
helpFlagVal bool helpFlagVal bool
makeSymlinksFlagVal bool
} }
// AddServer adds a server to the HyperKube object. // AddServer adds a server to the HyperKube object.
@@ -75,6 +76,8 @@ func (hk *HyperKube) Flags() *pflag.FlagSet {
hk.baseFlags.SetOutput(ioutil.Discard) hk.baseFlags.SetOutput(ioutil.Discard)
hk.baseFlags.SetNormalizeFunc(utilflag.WordSepNormalizeFunc) hk.baseFlags.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
hk.baseFlags.BoolVarP(&hk.helpFlagVal, "help", "h", false, "help for "+hk.Name) hk.baseFlags.BoolVarP(&hk.helpFlagVal, "help", "h", false, "help for "+hk.Name)
hk.baseFlags.BoolVar(&hk.makeSymlinksFlagVal, "make-symlinks", false, "create a symlink for each server in current directory")
hk.baseFlags.MarkHidden("make-symlinks") // hide this flag from appearing in servers' usage output
// These will add all of the "global" flags (defined with both the // These will add all of the "global" flags (defined with both the
// flag and pflag packages) to the new flag set we have. // flag and pflag packages) to the new flag set we have.
@@ -117,7 +120,8 @@ func (hk *HyperKube) Printf(format string, i ...interface{}) {
func (hk *HyperKube) Run(args []string) error { func (hk *HyperKube) Run(args []string) error {
// If we are called directly, parse all flags up to the first real // If we are called directly, parse all flags up to the first real
// argument. That should be the server to run. // argument. That should be the server to run.
baseCommand := path.Base(args[0]) command := args[0]
baseCommand := path.Base(command)
serverName := baseCommand serverName := baseCommand
if serverName == hk.Name { if serverName == hk.Name {
args = args[1:] args = args[1:]
@@ -133,6 +137,10 @@ func (hk *HyperKube) Run(args []string) error {
return err return err
} }
if hk.makeSymlinksFlagVal {
return hk.MakeSymlinks(command)
}
verflag.PrintAndExitIfRequested() verflag.PrintAndExitIfRequested()
args = baseFlags.Args() args = baseFlags.Args()
@@ -200,7 +208,32 @@ Servers
{{range .Servers}} {{range .Servers}}
{{.Name}} {{.Name}}
{{.Long | trim | wrap " "}}{{end}} {{.Long | trim | wrap " "}}{{end}}
Call '{{.Name}} --make-symlinks' to create symlinks for each server in the local directory.
Call '{{.Name}} <server> --help' for help on a specific server. Call '{{.Name}} <server> --help' for help on a specific server.
` `
util.ExecuteTemplate(hk.Out(), tt, hk) util.ExecuteTemplate(hk.Out(), tt, hk)
} }
// MakeSymlinks will create a symlink for each registered hyperkube server in the local directory.
func (hk *HyperKube) MakeSymlinks(command string) error {
wd, err := os.Getwd()
if err != nil {
return err
}
var errs bool
for _, s := range hk.servers {
link := path.Join(wd, s.Name())
err := os.Symlink(command, link)
if err != nil {
errs = true
hk.Println(err)
}
}
if errs {
return errors.New("Error creating one or more symlinks.")
}
return nil
}

View File

@@ -232,6 +232,7 @@ lock-file
log-flush-frequency log-flush-frequency
long-running-request-regexp long-running-request-regexp
low-diskspace-threshold-mb low-diskspace-threshold-mb
make-symlinks
manifest-url manifest-url
manifest-url-header manifest-url-header
masquerade-all masquerade-all