Keine Beschreibung http://j1x-huginn.herokuapp.com

setup_tools.rb 2.5KB

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