Class: ShopifyCLI::AdminAPI::PopulateResourceCommand

Inherits:
Command::SubCommand 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, check_node_version, check_ruby_version, check_version, #initialize, options, prerequisite_task, recommend_default_node_range, recommend_default_ruby_range, recommend_node, recommend_ruby, 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



139
140
141
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 139

def admin_url
  "https://#{@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
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 27

def call(args, _)
  @args = args
  @input = Hash.new
  @count = DEFAULT_COUNT
  @help = false
  @skip_shop_confirmation = 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

  ShopifyCLI::Tasks::ConfirmStore.call(@ctx) unless @skip_shop_confirmation
  @shop = AdminAPI.get_shop_or_abort(@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



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 126

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

#defaultsObject

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 62

def defaults
  raise NotImplementedError
end

#display_parent_extended_helpObject



70
71
72
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 70

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

#display_parent_helpObject



66
67
68
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 66

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

#input_optionsObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 101

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)


58
59
60
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 58

def message
  raise NotImplementedError
end

#populateObject



95
96
97
98
99
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 95

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

#priceObject



143
144
145
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 143

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

#resource_optionsObject



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

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("--skip-shop-confirmation") { |v| @skip_shop_confirmation = v }
  end
end

#run_mutation(data) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 116

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



112
113
114
# File 'lib/shopify_cli/admin_api/populate_resource_command.rb', line 112

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