cmd_base.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. fmt.Fprintln(capi.Out(), fmt.Sprintf("Connected to: %v", capi.Url()))
  52. res, err := capi.Req(api.EndpointAbout, "GET", nil)
  53. if err == nil {
  54. data := res.(map[string]interface{})
  55. fmt.Fprintln(capi.Out(), fmt.Sprintf("%v %v (REST versions: %v)",
  56. data["product"], data["version"], data["api_versions"]))
  57. }
  58. return err
  59. }
  60. // Command: whoami
  61. // ===============
  62. /*
  63. CommandWhoAmI is a command name.
  64. */
  65. const CommandWhoAmI = "whoami"
  66. /*
  67. CmdWhoAmI returns the current login status.
  68. */
  69. type CmdWhoAmI struct {
  70. }
  71. /*
  72. Name returns the command name (as it should be typed)
  73. */
  74. func (c *CmdWhoAmI) Name() string {
  75. return CommandWhoAmI
  76. }
  77. /*
  78. ShortDescription returns a short description of the command (single line)
  79. */
  80. func (c *CmdWhoAmI) ShortDescription() string {
  81. return "Returns the current login status."
  82. }
  83. /*
  84. LongDescription returns an extensive description of the command (can be multiple lines)
  85. */
  86. func (c *CmdWhoAmI) LongDescription() string {
  87. return "Returns the current login status."
  88. }
  89. /*
  90. Run executes the command.
  91. */
  92. func (c *CmdWhoAmI) Run(args []string, capi CommandConsoleAPI) error {
  93. res, err := capi.Req(ac.EndpointWhoAmI, "GET", nil)
  94. if err == nil {
  95. var out string
  96. o := res.(map[string]interface{})
  97. if o["logged_in"].(bool) {
  98. out = fmt.Sprintf("%s", o["username"])
  99. } else {
  100. out = "Nobody - not logged in"
  101. }
  102. fmt.Fprintln(capi.Out(), out)
  103. }
  104. return err
  105. }
  106. // Command: export
  107. // ===============
  108. /*
  109. CommandExport is a command name.
  110. */
  111. const CommandExport = "export"
  112. /*
  113. CmdExport exports the data which is currently in the export buffer.
  114. */
  115. type CmdExport struct {
  116. exportFunc func([]string, *bytes.Buffer) error
  117. }
  118. /*
  119. Name returns the command name (as it should be typed)
  120. */
  121. func (c *CmdExport) Name() string {
  122. return CommandExport
  123. }
  124. /*
  125. ShortDescription returns a short description of the command (single line)
  126. */
  127. func (c *CmdExport) ShortDescription() string {
  128. return "Exports the last output."
  129. }
  130. /*
  131. LongDescription returns an extensive description of the command (can be multiple lines)
  132. */
  133. func (c *CmdExport) LongDescription() string {
  134. return "Exports the data which is currently in the export buffer. The export " +
  135. "buffer is filled with the previous command output in a machine readable form."
  136. }
  137. /*
  138. Run executes the command.
  139. */
  140. func (c *CmdExport) Run(args []string, capi CommandConsoleAPI) error {
  141. return c.exportFunc(args, capi.ExportBuffer())
  142. }
  143. // Command: login
  144. // ==============
  145. /*
  146. CommandLogin is a command name.
  147. */
  148. const CommandLogin = "login"
  149. /*
  150. CmdLogin placeholder for the login command.
  151. */
  152. type CmdLogin struct {
  153. }
  154. /*
  155. Name returns the command name (as it should be typed)
  156. */
  157. func (c *CmdLogin) Name() string {
  158. return CommandLogin
  159. }
  160. /*
  161. ShortDescription returns a short description of the command (single line)
  162. */
  163. func (c *CmdLogin) ShortDescription() string {
  164. return "Log in as a user."
  165. }
  166. /*
  167. LongDescription returns an extensive description of the command (can be multiple lines)
  168. */
  169. func (c *CmdLogin) LongDescription() string {
  170. return "Log in as a user."
  171. }
  172. /*
  173. Run executes the command.
  174. */
  175. func (c *CmdLogin) Run(args []string, capi CommandConsoleAPI) error {
  176. return nil // Functionality is implemented in the command processor
  177. }
  178. // Command: logout
  179. // ===============
  180. /*
  181. CommandLogout is a command name.
  182. */
  183. const CommandLogout = "logout"
  184. /*
  185. CmdLogout placeholder for the logout command.
  186. */
  187. type CmdLogout struct {
  188. }
  189. /*
  190. Name returns the command name (as it should be typed)
  191. */
  192. func (c *CmdLogout) Name() string {
  193. return CommandLogout
  194. }
  195. /*
  196. ShortDescription returns a short description of the command (single line)
  197. */
  198. func (c *CmdLogout) ShortDescription() string {
  199. return "Log out the current user."
  200. }
  201. /*
  202. LongDescription returns an extensive description of the command (can be multiple lines)
  203. */
  204. func (c *CmdLogout) LongDescription() string {
  205. return "Log out the current user."
  206. }
  207. /*
  208. Run executes the command.
  209. */
  210. func (c *CmdLogout) Run(args []string, capi CommandConsoleAPI) error {
  211. return nil // Functionality is implemented in the command processor
  212. }
  213. // Command: help
  214. // =============
  215. /*
  216. CommandHelp is a command name.
  217. */
  218. const CommandHelp = "help"
  219. /*
  220. CmdHelp displays descriptions of other commands.
  221. */
  222. type CmdHelp struct {
  223. }
  224. /*
  225. Name returns the command name (as it should be typed)
  226. */
  227. func (c *CmdHelp) Name() string {
  228. return CommandHelp
  229. }
  230. /*
  231. ShortDescription returns a short description of the command (single line)
  232. */
  233. func (c *CmdHelp) ShortDescription() string {
  234. return "Display descriptions for all available commands."
  235. }
  236. /*
  237. LongDescription returns an extensive description of the command (can be multiple lines)
  238. */
  239. func (c *CmdHelp) LongDescription() string {
  240. return "Display descriptions for all available commands."
  241. }
  242. /*
  243. Run executes the command.
  244. */
  245. func (c *CmdHelp) Run(args []string, capi CommandConsoleAPI) error {
  246. cmds := capi.Commands()
  247. if len(args) > 0 {
  248. name := args[0]
  249. for _, cmd := range cmds {
  250. if cmd.Name() == name {
  251. capi.ExportBuffer().WriteString(cmd.LongDescription())
  252. fmt.Fprintln(capi.Out(), cmd.LongDescription())
  253. return nil
  254. }
  255. }
  256. return fmt.Errorf("Unknown command: %s", name)
  257. }
  258. var tab []string
  259. tab = append(tab, "Command")
  260. tab = append(tab, "Description")
  261. for _, cmd := range cmds {
  262. tab = append(tab, cmd.Name())
  263. tab = append(tab, cmd.ShortDescription())
  264. }
  265. capi.ExportBuffer().WriteString(stringutil.PrintCSVTable(tab, 2))
  266. fmt.Fprint(capi.Out(), stringutil.PrintStringTable(tab, 2))
  267. return nil
  268. }