cmd_base.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package console
  11. import (
  12. "bytes"
  13. "fmt"
  14. "devt.de/krotik/common/stringutil"
  15. "devt.de/krotik/eliasdb/api"
  16. "devt.de/krotik/eliasdb/api/ac"
  17. )
  18. // Command: ver
  19. // ============
  20. /*
  21. CommandVer is a command name.
  22. */
  23. const CommandVer = "ver"
  24. /*
  25. CmdVer displays descriptions of other commands.
  26. */
  27. type CmdVer struct {
  28. }
  29. /*
  30. Name returns the command name (as it should be typed)
  31. */
  32. func (c *CmdVer) Name() string {
  33. return CommandVer
  34. }
  35. /*
  36. ShortDescription returns a short description of the command (single line)
  37. */
  38. func (c *CmdVer) ShortDescription() string {
  39. return "Displays server version information."
  40. }
  41. /*
  42. LongDescription returns an extensive description of the command (can be multiple lines)
  43. */
  44. func (c *CmdVer) LongDescription() string {
  45. return "Displays server version information."
  46. }
  47. /*
  48. Run executes the command.
  49. */
  50. func (c *CmdVer) Run(args []string, capi CommandConsoleAPI) error {
  51. res, err := capi.Req(api.EndpointAbout, "GET", nil)
  52. if err == nil {
  53. data := res.(map[string]interface{})
  54. fmt.Fprintln(capi.Out(), fmt.Sprintf("%v %v (REST versions: %v)",
  55. data["product"], data["version"], data["api_versions"]))
  56. }
  57. return err
  58. }
  59. // Command: whoami
  60. // ===============
  61. /*
  62. CommandWhoAmI is a command name.
  63. */
  64. const CommandWhoAmI = "whoami"
  65. /*
  66. CmdWhoAmI returns the current login status.
  67. */
  68. type CmdWhoAmI struct {
  69. }
  70. /*
  71. Name returns the command name (as it should be typed)
  72. */
  73. func (c *CmdWhoAmI) Name() string {
  74. return CommandWhoAmI
  75. }
  76. /*
  77. ShortDescription returns a short description of the command (single line)
  78. */
  79. func (c *CmdWhoAmI) ShortDescription() string {
  80. return "Returns the current login status."
  81. }
  82. /*
  83. LongDescription returns an extensive description of the command (can be multiple lines)
  84. */
  85. func (c *CmdWhoAmI) LongDescription() string {
  86. return "Returns the current login status."
  87. }
  88. /*
  89. Run executes the command.
  90. */
  91. func (c *CmdWhoAmI) Run(args []string, capi CommandConsoleAPI) error {
  92. res, err := capi.Req(ac.EndpointWhoAmI, "GET", nil)
  93. if err == nil {
  94. var out string
  95. o := res.(map[string]interface{})
  96. if o["logged_in"].(bool) {
  97. out = fmt.Sprintf("%s", o["username"])
  98. } else {
  99. out = "Nobody - not logged in"
  100. }
  101. fmt.Fprintln(capi.Out(), out)
  102. }
  103. return err
  104. }
  105. // Command: export
  106. // ===============
  107. /*
  108. CommandExport is a command name.
  109. */
  110. const CommandExport = "export"
  111. /*
  112. CmdExport exports the data which is currently in the export buffer.
  113. */
  114. type CmdExport struct {
  115. exportFunc func([]string, *bytes.Buffer) error
  116. }
  117. /*
  118. Name returns the command name (as it should be typed)
  119. */
  120. func (c *CmdExport) Name() string {
  121. return CommandExport
  122. }
  123. /*
  124. ShortDescription returns a short description of the command (single line)
  125. */
  126. func (c *CmdExport) ShortDescription() string {
  127. return "Exports the last output."
  128. }
  129. /*
  130. LongDescription returns an extensive description of the command (can be multiple lines)
  131. */
  132. func (c *CmdExport) LongDescription() string {
  133. return "Exports the data which is currently in the export buffer. The export " +
  134. "buffer is filled with the previous command output in a machine readable form."
  135. }
  136. /*
  137. Run executes the command.
  138. */
  139. func (c *CmdExport) Run(args []string, capi CommandConsoleAPI) error {
  140. return c.exportFunc(args, capi.ExportBuffer())
  141. }
  142. // Command: login
  143. // ==============
  144. /*
  145. CommandLogin is a command name.
  146. */
  147. const CommandLogin = "login"
  148. /*
  149. CmdLogin placeholder for the login command.
  150. */
  151. type CmdLogin struct {
  152. }
  153. /*
  154. Name returns the command name (as it should be typed)
  155. */
  156. func (c *CmdLogin) Name() string {
  157. return CommandLogin
  158. }
  159. /*
  160. ShortDescription returns a short description of the command (single line)
  161. */
  162. func (c *CmdLogin) ShortDescription() string {
  163. return "Log in as a user."
  164. }
  165. /*
  166. LongDescription returns an extensive description of the command (can be multiple lines)
  167. */
  168. func (c *CmdLogin) LongDescription() string {
  169. return "Log in as a user."
  170. }
  171. /*
  172. Run executes the command.
  173. */
  174. func (c *CmdLogin) Run(args []string, capi CommandConsoleAPI) error {
  175. return nil // Functionality is implemented in the command processor
  176. }
  177. // Command: logout
  178. // ===============
  179. /*
  180. CommandLogout is a command name.
  181. */
  182. const CommandLogout = "logout"
  183. /*
  184. CmdLogout placeholder for the logout command.
  185. */
  186. type CmdLogout struct {
  187. }
  188. /*
  189. Name returns the command name (as it should be typed)
  190. */
  191. func (c *CmdLogout) Name() string {
  192. return CommandLogout
  193. }
  194. /*
  195. ShortDescription returns a short description of the command (single line)
  196. */
  197. func (c *CmdLogout) ShortDescription() string {
  198. return "Log out the current user."
  199. }
  200. /*
  201. LongDescription returns an extensive description of the command (can be multiple lines)
  202. */
  203. func (c *CmdLogout) LongDescription() string {
  204. return "Log out the current user."
  205. }
  206. /*
  207. Run executes the command.
  208. */
  209. func (c *CmdLogout) Run(args []string, capi CommandConsoleAPI) error {
  210. return nil // Functionality is implemented in the command processor
  211. }
  212. // Command: help
  213. // =============
  214. /*
  215. CommandHelp is a command name.
  216. */
  217. const CommandHelp = "help"
  218. /*
  219. CmdHelp displays descriptions of other commands.
  220. */
  221. type CmdHelp struct {
  222. }
  223. /*
  224. Name returns the command name (as it should be typed)
  225. */
  226. func (c *CmdHelp) Name() string {
  227. return CommandHelp
  228. }
  229. /*
  230. ShortDescription returns a short description of the command (single line)
  231. */
  232. func (c *CmdHelp) ShortDescription() string {
  233. return "Display descriptions for all available commands."
  234. }
  235. /*
  236. LongDescription returns an extensive description of the command (can be multiple lines)
  237. */
  238. func (c *CmdHelp) LongDescription() string {
  239. return "Display descriptions for all available commands."
  240. }
  241. /*
  242. Run executes the command.
  243. */
  244. func (c *CmdHelp) Run(args []string, capi CommandConsoleAPI) error {
  245. cmds := capi.Commands()
  246. if len(args) > 0 {
  247. name := args[0]
  248. for _, cmd := range cmds {
  249. if cmd.Name() == name {
  250. capi.ExportBuffer().WriteString(cmd.LongDescription())
  251. fmt.Fprintln(capi.Out(), cmd.LongDescription())
  252. return nil
  253. }
  254. }
  255. return fmt.Errorf("Unknown command: %s", name)
  256. }
  257. var tab []string
  258. tab = append(tab, "Command")
  259. tab = append(tab, "Description")
  260. for _, cmd := range cmds {
  261. tab = append(tab, cmd.Name())
  262. tab = append(tab, cmd.ShortDescription())
  263. }
  264. capi.ExportBuffer().WriteString(stringutil.PrintCSVTable(tab, 2))
  265. fmt.Fprint(capi.Out(), stringutil.PrintStringTable(tab, 2))
  266. return nil
  267. }