cli.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. require 'thor'
  2. require 'abbrev'
  3. module ShopifyAPI
  4. class Cli < Thor
  5. include Thor::Actions
  6. class ConfigFileError < StandardError
  7. end
  8. desc "list", "list available connections"
  9. def list
  10. available_connections.each do |c|
  11. prefix = default?(c) ? " * " : " "
  12. puts prefix + c
  13. end
  14. end
  15. desc "add CONNECTION", "create a config file for a connection named CONNECTION"
  16. def add(connection)
  17. file = config_file(connection)
  18. if File.exist?(file)
  19. raise ConfigFileError, "There is already a config file at #{file}"
  20. else
  21. config = {'protocol' => 'https'}
  22. config['domain'] = ask("Domain? (leave blank for #{connection}.myshopify.com)")
  23. config['domain'] = "#{connection}.myshopify.com" if config['domain'].blank?
  24. puts "\nopen https://#{config['domain']}/admin/api in your browser to get API credentials\n"
  25. config['api_key'] = ask("API key?")
  26. config['password'] = ask("Password?")
  27. create_file(file, config.to_yaml)
  28. end
  29. if available_connections.one?
  30. default(connection)
  31. end
  32. end
  33. desc "remove CONNECTION", "remove the config file for CONNECTION"
  34. def remove(connection)
  35. file = config_file(connection)
  36. if File.exist?(file)
  37. remove_file(default_symlink) if default?(connection)
  38. remove_file(file)
  39. else
  40. no_config_file_error(file)
  41. end
  42. end
  43. desc "edit [CONNECTION]", "open the config file for CONNECTION with your default editor"
  44. def edit(connection=nil)
  45. file = config_file(connection)
  46. if File.exist?(file)
  47. if ENV['EDITOR'].present?
  48. system(ENV['EDITOR'], file)
  49. else
  50. puts "Please set an editor in the EDITOR environment variable"
  51. end
  52. else
  53. no_config_file_error(file)
  54. end
  55. end
  56. desc "show [CONNECTION]", "output the location and contents of the CONNECTION's config file"
  57. def show(connection=nil)
  58. connection ||= default_connection
  59. file = config_file(connection)
  60. if File.exist?(file)
  61. puts file
  62. puts `cat #{file}`
  63. else
  64. no_config_file_error(file)
  65. end
  66. end
  67. desc "default [CONNECTION]", "show the default connection, or make CONNECTION the default"
  68. def default(connection=nil)
  69. if connection
  70. target = config_file(connection)
  71. if File.exist?(target)
  72. remove_file(default_symlink)
  73. `ln -s #{target} #{default_symlink}`
  74. else
  75. no_config_file_error(target)
  76. end
  77. end
  78. if File.exist?(default_symlink)
  79. puts "Default connection is #{default_connection}"
  80. else
  81. puts "There is no default connection set"
  82. end
  83. end
  84. desc "console [CONNECTION]", "start an API console for CONNECTION"
  85. def console(connection=nil)
  86. file = config_file(connection)
  87. config = YAML.load(File.read(file))
  88. puts "using #{config['domain']}"
  89. ShopifyAPI::Base.site = site_from_config(config)
  90. require 'irb'
  91. require 'irb/completion'
  92. ARGV.clear
  93. IRB.start
  94. end
  95. tasks.keys.abbrev.each do |shortcut, command|
  96. map shortcut => command.to_sym
  97. end
  98. private
  99. def shop_config_dir
  100. @shop_config_dir ||= File.join(ENV['HOME'], '.shopify', 'shops')
  101. end
  102. def default_symlink
  103. @default_symlink ||= File.join(shop_config_dir, 'default')
  104. end
  105. def config_file(connection)
  106. if connection
  107. File.join(shop_config_dir, "#{connection}.yml")
  108. else
  109. default_symlink
  110. end
  111. end
  112. def site_from_config(config)
  113. protocol = config['protocol'] || 'https'
  114. api_key = config['api_key']
  115. password = config['password']
  116. domain = config['domain']
  117. ShopifyAPI::Base.site = "#{protocol}://#{api_key}:#{password}@#{domain}/admin"
  118. end
  119. def available_connections
  120. @available_connections ||= begin
  121. pattern = File.join(shop_config_dir, "*.yml")
  122. Dir.glob(pattern).map { |f| File.basename(f, ".yml") }
  123. end
  124. end
  125. def default_connection_target
  126. @default_connection_target ||= File.readlink(default_symlink)
  127. end
  128. def default_connection
  129. @default_connection ||= File.basename(default_connection_target, ".yml")
  130. end
  131. def default?(connection)
  132. default_connection == connection
  133. end
  134. def no_config_file_error(filename)
  135. raise ConfigFileError, "There is no config file at #{filename}"
  136. end
  137. end
  138. end