Class: ShopifyAPI::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/shopify_api/cli.rb

Defined Under Namespace

Classes: ConfigFileError

Instance Method Summary collapse

Instance Method Details

#add(connection) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shopify_api/cli.rb', line 20

def add(connection)
  file = config_file(connection)
  if File.exist?(file)
    raise ConfigFileError, "There is already a config file at #{file}"
  else
    config = {'protocol' => 'https'}
    config['domain']   = ask("Domain? (leave blank for #{connection}.myshopify.com)")
    config['domain']   = "#{connection}.myshopify.com" if config['domain'].blank?
    puts "\nopen https://#{config['domain']}/admin/api in your browser to get API credentials\n"
    config['api_key']  = ask("API key?")
    config['password'] = ask("Password?")
    create_file(file, config.to_yaml)
  end
  if available_connections.one?
    default(connection)
  end
end

#console(connection = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/shopify_api/cli.rb', line 94

def console(connection=nil)
  file = config_file(connection)
  
  config = YAML.load(File.read(file))
  puts "using #{config['domain']}"
  ShopifyAPI::Base.site = site_from_config(config)
  
  require 'irb'
  require 'irb/completion'
  ARGV.clear
  IRB.start
end

#default(connection = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/shopify_api/cli.rb', line 76

def default(connection=nil)
  if connection
    target = config_file(connection)
    if File.exist?(target)
      remove_file(default_symlink)
      `ln -s #{target} #{default_symlink}`
    else
      no_config_file_error(target)
    end
  end
  if File.exist?(default_symlink)
    puts "Default connection is #{default_connection}"
  else
    puts "There is no default connection set"
  end
end

#edit(connection = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shopify_api/cli.rb', line 50

def edit(connection=nil)
  file = config_file(connection)
  if File.exist?(file)
    if ENV['EDITOR'].present?
      system(ENV['EDITOR'], file)
    else
      puts "Please set an editor in the EDITOR environment variable"
    end
  else
    no_config_file_error(file)
  end
end

#listObject



12
13
14
15
16
17
# File 'lib/shopify_api/cli.rb', line 12

def list
  available_connections.each do |c|
    prefix = default?(c) ? " * " : "   "
    puts prefix + c
  end
end

#remove(connection) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/shopify_api/cli.rb', line 39

def remove(connection)
  file = config_file(connection)
  if File.exist?(file)
    remove_file(default_symlink) if default?(connection)
    remove_file(file)
  else
    no_config_file_error(file)
  end
end

#show(connection = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/shopify_api/cli.rb', line 64

def show(connection=nil)
  connection ||= default_connection
  file = config_file(connection)
  if File.exist?(file)
    puts file
    puts `cat #{file}`
  else
    no_config_file_error(file)
  end
end