server.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // CAUTION: If you update code in this file, you may need to also update code
  14. // in contrib/mesos/cmd/km/server.go
  15. package main
  16. import (
  17. "io/ioutil"
  18. "strings"
  19. "k8s.io/kubernetes/pkg/util"
  20. "k8s.io/kubernetes/pkg/util/flag"
  21. "github.com/spf13/pflag"
  22. )
  23. type serverRunFunc func(s *Server, args []string) error
  24. // Server describes a server that this binary can morph into.
  25. type Server struct {
  26. SimpleUsage string // One line description of the server.
  27. Long string // Longer free form description of the server
  28. Run serverRunFunc // Run the server. This is not expected to return.
  29. flags *pflag.FlagSet // Flags for the command (and all dependents)
  30. name string
  31. hk *HyperKube
  32. }
  33. // Usage returns the full usage string including all of the flags.
  34. func (s *Server) Usage() error {
  35. tt := `{{if .Long}}{{.Long | trim | wrap ""}}
  36. {{end}}Usage:
  37. {{.SimpleUsage}} [flags]
  38. Available Flags:
  39. {{.Flags.FlagUsages}}`
  40. return util.ExecuteTemplate(s.hk.Out(), tt, s)
  41. }
  42. // Name returns the name of the command as derived from the usage line.
  43. func (s *Server) Name() string {
  44. if s.name != "" {
  45. return s.name
  46. }
  47. name := s.SimpleUsage
  48. i := strings.Index(name, " ")
  49. if i >= 0 {
  50. name = name[:i]
  51. }
  52. return name
  53. }
  54. // Flags returns a flagset for this server
  55. func (s *Server) Flags() *pflag.FlagSet {
  56. if s.flags == nil {
  57. s.flags = pflag.NewFlagSet(s.Name(), pflag.ContinueOnError)
  58. s.flags.SetOutput(ioutil.Discard)
  59. s.flags.SetNormalizeFunc(flag.WordSepNormalizeFunc)
  60. }
  61. return s.flags
  62. }