1
0

setup_tools.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. require 'open3'
  2. require 'io/console'
  3. require 'securerandom'
  4. require 'shellwords'
  5. require 'active_support/core_ext/object/blank'
  6. module SetupTools
  7. def capture(cmd, opts = {})
  8. if opts.delete(:no_stderr)
  9. o, s = Open3.capture2(cmd, opts)
  10. else
  11. o, s = Open3.capture2e(cmd, opts)
  12. end
  13. o.strip
  14. end
  15. def grab_config_with_cmd!(cmd, opts = {})
  16. config_data = capture(cmd, opts)
  17. $config = {}
  18. if config_data !~ /has no config vars/
  19. config_data.split("\n").map do |line|
  20. next if line =~ /^\s*(#|$)/ # skip comments and empty lines
  21. first_equal_sign = line.index('=')
  22. raise "Invalid line found in config: #{line}" unless first_equal_sign
  23. $config[line.slice(0, first_equal_sign)] = line.slice(first_equal_sign + 1, line.length)
  24. end
  25. end
  26. end
  27. def print_config
  28. if $config.length > 0
  29. puts
  30. puts "Your current config:"
  31. $config.each do |key, value|
  32. puts ' ' + key + ' ' * (25 - [key.length, 25].min) + '= ' + value
  33. end
  34. end
  35. end
  36. def set_defaults!
  37. unless $config['APP_SECRET_TOKEN']
  38. puts "Setting up APP_SECRET_TOKEN..."
  39. set_value 'APP_SECRET_TOKEN', SecureRandom.hex(64)
  40. end
  41. set_value 'RAILS_ENV', "production"
  42. set_value 'FORCE_SSL', "true"
  43. set_value 'USE_GRAPHVIZ_DOT', 'dot'
  44. unless $config['INVITATION_CODE']
  45. puts "You need to set an invitation code for your Huginn instance. If you plan to share this instance, you will"
  46. puts "tell this code to anyone who you'd like to invite. If you won't share it, then just set this to something"
  47. puts "that people will not guess."
  48. invitation_code = nag("What code would you like to use?")
  49. set_value 'INVITATION_CODE', invitation_code
  50. end
  51. end
  52. def confirm_app_name(app_name)
  53. unless yes?("Your app name is '#{app_name}'. Is this correct?", default: :yes)
  54. puts "Well, then I'm not sure what to do here, sorry."
  55. exit 1
  56. end
  57. end
  58. # expects set_env(key, value) to be defined.
  59. def set_value(key, value, options = {})
  60. if $config[key].nil? || $config[key] == '' || ($config[key] != value && options[:force] != false)
  61. puts "Setting #{key} to #{value}" unless options[:silent]
  62. puts set_env(key, value)
  63. $config[key] = value
  64. end
  65. end
  66. def ask(question, opts = {})
  67. print question + " "
  68. STDOUT.flush
  69. (opts[:noecho] ? STDIN.noecho(&:gets) : gets).strip
  70. end
  71. def nag(question, opts = {})
  72. answer = ''
  73. while answer.length == 0
  74. answer = ask(question, opts)
  75. end
  76. answer
  77. end
  78. def yes?(question, opts = {})
  79. if opts[:default].to_s[0...1] == "y"
  80. (ask(question + " (Y/n)").presence || "yes") =~ /^y/i
  81. else
  82. ask(question + " (y/n)") =~ /^y/i
  83. end
  84. end
  85. end