Class: Cyclid::Cli::Stage

Inherits:
Thor
  • Object
show all
Defined in:
lib/cyclid/cli/stage.rb

Overview

‘stage’ sub-command

Instance Method Summary collapse

Instance Method Details

#create(filename) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cyclid/cli/stage.rb', line 70

def create(filename)
  stage_file = File.expand_path(filename)
  raise 'Cannot open file' unless File.exist?(stage_file)

  stage_type = if options[:yaml]
                 'yaml'
               elsif options[:json]
                 'json'
               else
                 # Detect format
                 match = stage_file.match(/\A.*\.(json|yml|yaml)\z/)
                 match[1]
               end
  stage_type = 'yaml' if stage_type == 'yml'

  # Do a client-side sanity check by attempting to parse the file; it
  # will fail-fast if the file has a syntax error
  stage = File.read(stage_file)
  stage_data = if stage_type == 'yaml'
                 YAML.safe_load(stage)
               elsif stage_type == 'json'
                 JSON.parse(stage)
               else
                 raise 'Unknown or unsupported file type'
               end

  # Inject the version if it was passed on the command line
  stage_data['version'] = options[:version] if options[:version]

  client.stage_create(client.config.organization, stage_data)
rescue StandardError => ex
  abort "Failed to create stage: #{ex}"
end

#edit(name) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cyclid/cli/stage.rb', line 109

def edit(name)
  stages = client.stage_get(client.config.organization, name)

  # XXX This is a hack. The API returns all stages from this endpoint;
  # we might need to add or extend the API to return "latest" only.
  stage = stages.last

  stage = invoke_editor(stage)
  client.stage_modify(client.config.organization, stage)
rescue StandardError => ex
  abort "Failed to edit stage: #{ex}"
end

#listObject



22
23
24
25
26
27
28
29
# File 'lib/cyclid/cli/stage.rb', line 22

def list
  stages = client.stage_list(client.config.organization)
  stages.each do |stage|
    Formatter.puts "#{stage[:name]} v#{stage[:version]}"
  end
rescue StandardError => ex
  abort "Failed to get stages: #{ex}"
end

#show(name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cyclid/cli/stage.rb', line 32

def show(name)
  stages = client.stage_get(client.config.organization, name)

  # Pretty print the stage details
  stages.each do |stage|
    Formatter.colorize 'Name', stage['name']
    Formatter.colorize 'Version', stage['version']
    Formatter.colorize 'Steps'
    stage['steps'].each do |step|
      Formatter.colorize"\t\tAction", step['action']
      step.delete('action')
      step.each do |k, v|
        Formatter.colorize "\t\t#{k.capitalize}: ", v.to_s
      end
    end
  end
rescue StandardError => ex
  abort "Failed to get stage: #{ex}"
end