Class: Kitsune::Kit::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/kitsune/kit/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


30
# File 'lib/kitsune/kit/cli.rb', line 30

def self.exit_on_failure? = true

.normalized_arguments(arguments) ⇒ Object



43
44
45
46
47
48
# File 'lib/kitsune/kit/cli.rb', line 43

def self.normalized_arguments(arguments)
  values = arguments.dup
  return ["help", values.first] if values.length == 2 && values.last == "help" && tasks.key?(values.first)

  values
end

.start(given_args = ARGV, config = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/kitsune/kit/cli.rb', line 32

def self.start(given_args = ARGV, config = {})
  config[:shell] ||= Thor::Base.shell.new
  dispatch(nil, normalized_arguments(given_args), nil, config)
rescue Thor::Error => e
  config[:debug] || ENV["THOR_DEBUG"] == "1" ? raise(e) : config[:shell].error(e.message)

  exit(2)
rescue Errno::EPIPE
  exit(0)
end

Instance Method Details

#applyObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/kitsune/kit/cli.rb', line 110

def apply
  execute do
    app, reporter = build_application
    plan_result = Workflows::BuildPlan.new(
      config: app.config,
      operations: app.operations,
      event_bus: app.event_bus
    ).call
    plan = plan_result.value
    print_plan(plan) unless json?

    if options[:dry_run]
      render_apply_terminal(reporter, plan_result, app, "Dry run: no changes were applied.")
      next 0
    end

    unless plan.changed?
      result = Result.success([], metadata: { unchanged: true })
      render_apply_terminal(reporter, result, app, "No changes to apply.")
      next 0
    end

    confirm_apply!(plan)
    result = Workflows::ApplyPlan.new(
      config: app.config,
      operations: app.operations,
      state_store: app.state_store,
      event_bus: app.event_bus
    ).call(plan: plan)
    render_apply_success(reporter, result, app)
    0
  end
end

#dns(action) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/kitsune/kit/cli.rb', line 225

def dns(action)
  execute do
    app, reporter = build_application
    operation = app.operations.find { |candidate| candidate.resource == "dns" }
    raise Errors::ConfigurationError, "no DNS domains are configured" unless operation

    result = dispatch_resource_action(app, operation, action)
    flush_json(reporter, result, "dns.#{action}", app.config.environment) if json? && result.is_a?(Result)
    0
  end
end

#docker(action) ⇒ Object



293
294
295
296
297
298
299
300
301
# File 'lib/kitsune/kit/cli.rb', line 293

def docker(action)
  execute do
    app, reporter = build_application
    operation = app.operations.find { |candidate| candidate.resource == "docker" }
    result = dispatch_docker_action(app, operation, action)
    flush_json(reporter, result, "docker.#{action}", app.config.environment) if json? && result.is_a?(Result)
    0
  end
end

#doctorObject



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

def doctor
  execute do
    app, reporter = build_application
    result = Workflows::Doctor.new(
      config: app.config,
      provider: app.provider,
      state_store: app.state_store,
      transport_factory: app.transport_factory,
      operations: app.operations,
      event_bus: app.event_bus
    ).call
    if json?
      flush_json(reporter, result, "doctor", app.config.environment)
    else
      puts
      print_checks(result.value)
    end
    result.success? ? 0 : 1
  end
end

#env(action, name = nil) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/kitsune/kit/cli.rb', line 238

def env(action, name = nil)
  execute do
    selection = Workflows::EnvironmentSelection.new(root: options[:root])
    value = case action
            when "list" then selection.list
            when "current" then selection.current
            when "use" then selection.use(name)
            else raise Errors::ConfigurationError, "unknown environment action: #{action}"
            end
    if json?
      flush_json(
        build_reporter(build_secret_filter), Result.success(value), "env.#{action}", selection.current
      )
    elsif value.is_a?(Array)
      value.each { |environment| puts environment }
    else
      puts value
    end
    0
  end
end

#initObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kitsune/kit/cli.rb', line 59

def init
  execute do
    files = Workflows::InitializeProject.new(root: options[:root]).call(force: options[:force])
    if json?
      flush_json(build_reporter(build_secret_filter), Result.success(files), "init", "development")
    else
      puts "Created Kitsune Kit configuration:"
      files.each { |file| puts "  #{file}" }
      puts "Next: edit .kitsune/config.yml, export DO_API_TOKEN, then run `kit doctor`."
    end
    0
  end
end

#planObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kitsune/kit/cli.rb', line 96

def plan
  execute do
    app, reporter = build_application
    result = Workflows::BuildPlan.new(
      config: app.config,
      operations: app.operations,
      event_bus: app.event_bus
    ).call
    json? ? flush_json(reporter, result, "plan", app.config.environment) : print_plan(result.value)
    0
  end
end

#resume(run_id = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kitsune/kit/cli.rb', line 145

def resume(run_id = nil)
  execute do
    app, reporter = build_application
    confirm_resume!(run_id)
    result = Workflows::ApplyPlan.new(
      config: app.config,
      operations: app.operations,
      state_store: app.state_store,
      event_bus: app.event_bus
    ).call(resume_from: run_id || true)
    flush_json(reporter, result, "resume", app.config.environment) if json?
    0
  end
end

#rollbackObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/kitsune/kit/cli.rb', line 180

def rollback
  execute do
    app, reporter = build_application
    confirm_named_action!("rollback", app.config.environment, option: :yes)
    result = Workflows::Rollback.new(
      config: app.config,
      operations: app.operations.drop(1),
      state_store: app.state_store,
      event_bus: app.event_bus
    ).call
    flush_json(reporter, result, "rollback", app.config.environment) if json?
    0
  end
end

#server(action) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/kitsune/kit/cli.rb', line 199

def server(action)
  execute do
    app, reporter = build_application
    result = dispatch_server_action(app, action)
    flush_json(reporter, result, "server.#{action}", app.config.environment) if json? && result.is_a?(Result)
    0
  end
end

#service(type, action) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/kitsune/kit/cli.rb', line 212

def service(type, action)
  execute do
    app, reporter = build_application
    operation = find_service_operation(app, type, action)
    result = dispatch_service_action(app, operation, type, action)
    if json? && result.is_a?(Result)
      flush_json(reporter, result, "service.#{type}.#{action}", app.config.environment)
    end
    0
  end
end

#statusObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/kitsune/kit/cli.rb', line 161

def status
  execute do
    app, reporter = build_application
    result = Workflows::InspectEnvironment.new(
      config: app.config,
      provider: app.provider,
      state_store: app.state_store,
      event_bus: app.event_bus
    ).call
    if json?
      flush_json(reporter, result, "status", app.config.environment)
    else
      print_environment_status(result.value)
    end
    0
  end
end

#support(action) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/kitsune/kit/cli.rb', line 261

def support(action)
  execute do
    raise Errors::ConfigurationError, "unknown support action: #{action}" unless action == "bundle"

    app, = build_application
    filter = configured_secret_filter(app.config)
    doctor = Workflows::Doctor.new(
      config: app.config,
      provider: app.provider,
      state_store: app.state_store,
      transport_factory: app.transport_factory,
      operations: app.operations
    )
    path = Workflows::SupportBundle.new(
      root: options[:root], config: app.config, state_store: app.state_store, doctor: doctor,
      secret_filter: filter
    ).call
    if json?
      flush_json(
        build_reporter(filter), Result.success({ path: path }), "support.bundle", app.config.environment
      )
    else
      puts "Support bundle created: #{path}"
      puts "\nRedacted contents (inspect before sharing):"
      puts File.read(path)
      puts "Kitsune Kit did not upload anything."
    end
    0
  end
end

#uiObject



304
305
306
307
308
# File 'lib/kitsune/kit/cli.rb', line 304

def ui
  execute do
    build_tui.call
  end
end

#versionObject



51
52
53
# File 'lib/kitsune/kit/cli.rb', line 51

def version
  puts "Kitsune Kit #{VERSION}"
end