Class: ShopifyCli::AdminAPI::PopulateResourceCommand

Inherits:
SubCommand
  • Object
show all
Defined in:
lib/shopify-cli/admin_api/populate_resource_command.rb

Constant Summary collapse

DEFAULT_COUNT =
5

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Command

#ctx, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

call_help, #initialize, options, prerequisite_task, run_prerequisites, subcommand, subcommand_registry

Methods included from Feature::Set

#hidden?, #hidden_feature

Constructor Details

This class inherits a constructor from ShopifyCli::Command

Class Attribute Details

.input_typeObject

Returns the value of attribute input_type.



12
13
14
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 12

def input_type
  @input_type
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



9
10
11
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 9

def input
  @input
end

Class Method Details

.call(args, command_name, _parent_command) ⇒ Object

we override the call classmethod here because we parse options at runtime



15
16
17
18
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 15

def call(args, command_name, _parent_command)
  cmd = new(@ctx)
  cmd.call(args, command_name)
end

.helpObject



20
21
22
23
24
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 20

def help
  cmd = new(@ctx)
  output = cmd.display_parent_help + "\n"
  output + cmd.display_parent_extended_help
end

Instance Method Details

#admin_urlObject



140
141
142
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 140

def admin_url
  "https://#{Project.current.env.shop}/admin/"
end

#call(args, _) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 27

def call(args, _)
  return unless Project.current
  Tasks::EnsureEnv.call(@ctx)
  @args = args
  @input = Hash.new
  @count = DEFAULT_COUNT
  @help = false
  input_options
  resource_options.parse(@args)

  if @help
    output = display_parent_extended_help
    output += "\n#{@ctx.message('core.populate.options.header', camel_case_resource_type)}\n"
    output += resource_options.help
    return @ctx.puts(output)
  end

  @shop ||= Project.current.env.shop || get_shop(@ctx)

  if @silent
    spin_group = CLI::UI::SpinGroup.new
    spin_group.add(@ctx.message('core.populate.populating', @count, camel_case_resource_type)) do |spinner|
      populate
      spinner.update_title(completion_message)
    end
    spin_group.wait
  else
    populate
    @ctx.puts(completion_message)
  end
end

#completion_messageObject



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 127

def completion_message
  plural = @count > 1 ? "s" : ""
  @ctx.message(
    'core.populate.completion_message',
    @count,
    "#{camel_case_resource_type}#{plural}",
    Project.current.env.shop,
    camel_case_resource_type,
    admin_url,
    snake_case_resource_type
  )
end

#defaultsObject

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 63

def defaults
  raise NotImplementedError
end

#display_parent_extended_helpObject



71
72
73
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 71

def display_parent_extended_help
  parent_command_klass.respond_to?(:extended_help) ? parent_command_klass.extended_help : ""
end

#display_parent_helpObject



67
68
69
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 67

def display_parent_help
  parent_command_klass.respond_to?(:help) ? parent_command_klass.help : ""
end

#input_optionsObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 102

def input_options
  schema.type(self.class.input_type)['inputFields'].each do |field|
    resource_options.on(
      "--#{field['name']}=#{field['defaultValue']}",
      field['description']
    ) do |value|
      @input[field['name']] = value
    end
  end
end

#messageObject

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 59

def message
  raise NotImplementedError
end

#populateObject



96
97
98
99
100
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 96

def populate
  @count.times do
    run_mutation(defaults.merge(@input))
  end
end

#priceObject



144
145
146
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 144

def price
  format('%.2f', rand(1..10))
end

#resource_optionsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 75

def resource_options
  @resource_options ||= OptionParser.new do |opts|
    opts.banner = ""
    opts.on(
      "-c #{DEFAULT_COUNT}",
      "--count=#{DEFAULT_COUNT}",
      @ctx.message('core.populate.options.count_help')
    ) do |value|
      @count = value.to_i
    end

    opts.on('-h', '--help', 'print help') do |value|
      @help = value
    end

    opts.on("--silent") { |v| @silent = v }

    opts.on('--shop=', '-s') { |value| @shop = value }
  end
end

#run_mutation(data) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 117

def run_mutation(data)
  kwargs = { input: data }
  kwargs[:shop] = @shop
  resp = AdminAPI.query(
    @ctx, "create_#{snake_case_resource_type}", **kwargs
  )
  @ctx.abort(resp['errors']) if resp['errors']
  @ctx.done(message(resp['data'])) unless @silent
end

#schemaObject



113
114
115
# File 'lib/shopify-cli/admin_api/populate_resource_command.rb', line 113

def schema
  @schema ||= AdminAPI::Schema.get(@ctx)
end