github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commandregistry/registry.go (about) 1 package commandregistry 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "unicode/utf8" 8 9 "code.cloudfoundry.org/cli/cf" 10 "code.cloudfoundry.org/cli/cf/flags" 11 . "code.cloudfoundry.org/cli/cf/i18n" 12 "code.cloudfoundry.org/cli/util/configv3" 13 14 . "code.cloudfoundry.org/cli/cf/terminal" 15 ) 16 17 var _ = initI18nFunc() 18 var Commands = NewRegistry() 19 20 func initI18nFunc() bool { 21 config, err := configv3.LoadConfig() 22 if err != nil { 23 fmt.Println(FailureColor("FAILED")) 24 fmt.Println("Error read/writing config: ", err.Error()) 25 os.Exit(1) 26 } 27 T = Init(config) 28 return true 29 } 30 31 type registry struct { 32 cmd map[string]Command 33 alias map[string]string 34 } 35 36 func NewRegistry() *registry { 37 return ®istry{ 38 cmd: make(map[string]Command), 39 alias: make(map[string]string), 40 } 41 } 42 43 func Register(cmd Command) { 44 m := cmd.MetaData() 45 Commands.cmd[m.Name] = cmd 46 47 Commands.alias[m.ShortName] = m.Name 48 } 49 50 func (r *registry) FindCommand(name string) Command { 51 if _, ok := r.cmd[name]; ok { 52 return r.cmd[name] 53 } 54 55 if alias, exists := r.alias[name]; exists { 56 return r.cmd[alias] 57 } 58 59 return nil 60 } 61 62 func (r *registry) CommandExists(name string) bool { 63 if strings.TrimSpace(name) == "" { 64 return false 65 } 66 67 var ok bool 68 69 if _, ok = r.cmd[name]; !ok { 70 alias, exists := r.alias[name] 71 72 if exists { 73 _, ok = r.cmd[alias] 74 } 75 } 76 77 return ok 78 } 79 80 func (r *registry) ListCommands() []string { 81 keys := make([]string, len(r.cmd)) 82 83 i := 0 84 for k := range r.cmd { 85 keys[i] = k 86 i++ 87 } 88 89 return keys 90 } 91 92 func (r *registry) SetCommand(cmd Command) { 93 r.cmd[cmd.MetaData().Name] = cmd 94 } 95 96 func (r *registry) RemoveCommand(cmdName string) { 97 delete(r.cmd, cmdName) 98 } 99 100 func (r *registry) TotalCommands() int { 101 return len(r.cmd) 102 } 103 104 func (r *registry) MaxCommandNameLength() int { 105 maxNameLen := 0 106 for name := range r.cmd { 107 if nameLen := utf8.RuneCountInString(name); nameLen > maxNameLen { 108 maxNameLen = nameLen 109 } 110 } 111 return maxNameLen 112 } 113 114 func (r *registry) Metadatas() []CommandMetadata { 115 var m []CommandMetadata 116 117 for _, c := range r.cmd { 118 m = append(m, c.MetaData()) 119 } 120 121 return m 122 } 123 124 func (r *registry) CommandUsage(cmdName string) string { 125 cmd := r.FindCommand(cmdName) 126 127 return CLICommandUsagePresenter(cmd).Usage() 128 } 129 130 type usagePresenter struct { 131 cmd Command 132 } 133 134 func CLICommandUsagePresenter(cmd Command) *usagePresenter { 135 return &usagePresenter{ 136 cmd: cmd, 137 } 138 } 139 140 func (u *usagePresenter) Usage() string { 141 metadata := u.cmd.MetaData() 142 143 output := T("NAME:") + "\n" 144 output += " " + metadata.Name + " - " + metadata.Description + "\n\n" 145 146 output += T("USAGE:") + "\n" 147 output += " " + strings.Replace(strings.Join(metadata.Usage, ""), "CF_NAME", cf.Name, -1) + "\n" 148 149 if len(metadata.Examples) > 0 { 150 output += "\n" 151 output += fmt.Sprintf("%s:\n", T("EXAMPLES")) 152 for _, e := range metadata.Examples { 153 output += fmt.Sprintf(" %s\n", strings.Replace(e, "CF_NAME", cf.Name, -1)) 154 } 155 } 156 157 if metadata.ShortName != "" { 158 output += "\n" + T("ALIAS:") + "\n" 159 output += " " + metadata.ShortName + "\n" 160 } 161 162 if metadata.Flags != nil { 163 output += "\n" + T("OPTIONS:") + "\n" 164 output += flags.NewFlagContext(metadata.Flags).ShowUsage(3) 165 } 166 167 return output 168 }