cmd_gui.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (C) 2014 Audrius Butkevičius
  2. package main
  3. import (
  4. "fmt"
  5. "strings"
  6. "github.com/AudriusButkevicius/cli"
  7. )
  8. func init() {
  9. cliCommands = append(cliCommands, cli.Command{
  10. Name: "gui",
  11. HideHelp: true,
  12. Usage: "GUI command group",
  13. Subcommands: []cli.Command{
  14. {
  15. Name: "dump",
  16. Usage: "Show all GUI configuration settings",
  17. Requires: &cli.Requires{},
  18. Action: guiDump,
  19. },
  20. {
  21. Name: "get",
  22. Usage: "Get a GUI configuration setting",
  23. Requires: &cli.Requires{"setting"},
  24. Action: guiGet,
  25. },
  26. {
  27. Name: "set",
  28. Usage: "Set a GUI configuration setting",
  29. Requires: &cli.Requires{"setting", "value"},
  30. Action: guiSet,
  31. },
  32. {
  33. Name: "unset",
  34. Usage: "Unset a GUI configuration setting",
  35. Requires: &cli.Requires{"setting"},
  36. Action: guiUnset,
  37. },
  38. },
  39. })
  40. }
  41. func guiDump(c *cli.Context) {
  42. cfg := getConfig(c).GUI
  43. writer := newTableWriter()
  44. fmt.Fprintln(writer, "Enabled:\t", cfg.Enabled, "\t(enabled)")
  45. fmt.Fprintln(writer, "Use HTTPS:\t", cfg.UseTLS(), "\t(tls)")
  46. fmt.Fprintln(writer, "Listen Addresses:\t", cfg.Address(), "\t(address)")
  47. if cfg.User != "" {
  48. fmt.Fprintln(writer, "Authentication User:\t", cfg.User, "\t(username)")
  49. fmt.Fprintln(writer, "Authentication Password:\t", cfg.Password, "\t(password)")
  50. }
  51. if cfg.APIKey != "" {
  52. fmt.Fprintln(writer, "API Key:\t", cfg.APIKey, "\t(apikey)")
  53. }
  54. writer.Flush()
  55. }
  56. func guiGet(c *cli.Context) {
  57. cfg := getConfig(c).GUI
  58. arg := c.Args()[0]
  59. switch strings.ToLower(arg) {
  60. case "enabled":
  61. fmt.Println(cfg.Enabled)
  62. case "tls":
  63. fmt.Println(cfg.UseTLS())
  64. case "address":
  65. fmt.Println(cfg.Address())
  66. case "user":
  67. if cfg.User != "" {
  68. fmt.Println(cfg.User)
  69. }
  70. case "password":
  71. if cfg.User != "" {
  72. fmt.Println(cfg.Password)
  73. }
  74. case "apikey":
  75. if cfg.APIKey != "" {
  76. fmt.Println(cfg.APIKey)
  77. }
  78. default:
  79. die("Invalid setting: " + arg + "\nAvailable settings: enabled, tls, address, user, password, apikey")
  80. }
  81. }
  82. func guiSet(c *cli.Context) {
  83. cfg := getConfig(c)
  84. arg := c.Args()[0]
  85. val := c.Args()[1]
  86. switch strings.ToLower(arg) {
  87. case "enabled":
  88. cfg.GUI.Enabled = parseBool(val)
  89. case "tls":
  90. cfg.GUI.RawUseTLS = parseBool(val)
  91. case "address":
  92. validAddress(val)
  93. cfg.GUI.RawAddress = val
  94. case "user":
  95. cfg.GUI.User = val
  96. case "password":
  97. cfg.GUI.Password = val
  98. case "apikey":
  99. cfg.GUI.APIKey = val
  100. default:
  101. die("Invalid setting: " + arg + "\nAvailable settings: enabled, tls, address, user, password, apikey")
  102. }
  103. setConfig(c, cfg)
  104. }
  105. func guiUnset(c *cli.Context) {
  106. cfg := getConfig(c)
  107. arg := c.Args()[0]
  108. switch strings.ToLower(arg) {
  109. case "user":
  110. cfg.GUI.User = ""
  111. case "password":
  112. cfg.GUI.Password = ""
  113. case "apikey":
  114. cfg.GUI.APIKey = ""
  115. default:
  116. die("Invalid setting: " + arg + "\nAvailable settings: user, password, apikey")
  117. }
  118. setConfig(c, cfg)
  119. }