Class: Slugforge::Commands::Wrangler
- Inherits:
-
SubCommand
- Object
- Thor
- Slugforge::Command
- SubCommand
- Slugforge::Commands::Wrangler
- Defined in:
- lib/slugforge/commands/wrangler.rb
Instance Method Summary collapse
- #delete(name_part) ⇒ Object
- #list ⇒ Object
- #pull(name_part) ⇒ Object
- #purge ⇒ Object
- #push(file) ⇒ Object
Methods inherited from SubCommand
Methods inherited from Slugforge::Command
Methods included from Helper
Constructor Details
This class inherits a constructor from Slugforge::Command
Instance Method Details
#delete(name_part) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/slugforge/commands/wrangler.rb', line 93 def delete(name_part) slug = find_slug(name_part) if [:yes] || (ask("Are you sure you wish to delete '#{slug.attributes[:name]}'? [yN]").downcase == 'y') slug.destroy logger.say_status :destroy, slug.key, :red else logger.say_status :keep, slug.key, :green end end |
#list ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/slugforge/commands/wrangler.rb', line 49 def list raise error_class, "count must be greater than 0" if ![:all] && [:count] <= 0 begin project_name = self.project_name rescue Thor::Error # This is the only case where we don't care if project is not found. end slugs = (project_name.nil? ? self.files : self.slugs(project_name)).values raise error_class, "No slugs found for #{project_name}" if slugs.first.nil? total = slugs.length sorting = [:sort].split(':') field = sorting.first.to_sym direction = (sorting.last || 'desc') unless slugs.first.class.attributes.include?(field) raise error_class, "unknown attribute for sorting: #{field}. Available fields: #{slugs.first.class.attributes * ', '}" end slugs = slugs.sort_by { |f| f.attributes[field] } slugs = slugs.reverse! if direction != 'asc' slugs = slugs.slice(0...[:count]) unless [:all] logger.say "Slugs for #{project_name} (#{slugs.size}", nil, false logger.say " of #{total}", nil, false unless [:all] logger.say ")" tag_manager.(project_name) slugs.each do |slug| = tag_manager.(project_name, slug.key) logger.say " #{.size > 0 ? ' (' + .join(', ') + ')' : ''} ", :yellow logger.say "#{slug.attributes[:name]} ", :green logger.say "- " logger.say "#{slug.attributes[:pretty_age]} ago ", :cyan logger.say "(#{set_color(slug.attributes[:pretty_length], :magenta)})" end end |
#pull(name_part) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/slugforge/commands/wrangler.rb', line 26 def pull(name_part) verify_project_name! slug = name_part ? find_slug(name_part) : find_latest_slug logger.say_status :fetch, "#{slug.attributes[:name]} (#{slug.attributes[:pretty_length]})", :yellow logger.say "Note: process will block until download completes." # This should block until the body is downloaded. # We open the file for writing afterwards to prevent creating empty files. slug.body File.open(slug.attributes[:name], 'w+') { |f| f.write(slug.body) } logger.say_status :fetched, "pull complete, saved to #{slug.attributes[:name]}" end |
#purge ⇒ Object
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 |
# File 'lib/slugforge/commands/wrangler.rb', line 110 def purge raise error_class, "keep must be greater than 0" if ![:all] && [:keep] <= 0 slugs = self.slugs(project_name).values if [:all] if ![:yes] && ask("Are you sure you wish to delete #{slugs.size} slugs for #{project_name}? [yN]").downcase == 'y' [:yes] = true end if [:yes] slugs.each do |slug| slug.destroy logger.say_status :destroy, slug.key, :red end end elsif slugs.size > [:keep] slugs.slice([:keep]..-1).each do |slug| if [:yes] || (ask("Are you sure you wish to delete #{slug.attributes[:name]}? [Yn]").downcase != 'n') slug.destroy logger.say_status :destroy, slug.key, :red else logger.say_status :keep, slug.key, :green end end else logger.say "Aborting, only #{slugs.size} slugs for #{project_name} and we want to keep #{[:keep]}" end end |
#push(file) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/slugforge/commands/wrangler.rb', line 8 def push(file) verify_project_name! unless File.exist?(file) raise error_class, "file does not exist" end dest = "#{project_name}/#{file}" logger.say_status :upload, "slug #{file} to #{project_name}", :yellow s3.put_object(aws_bucket, dest, File.read(file)) logger.say_status :uploaded, "slug saved" if [:tag] logger.say_status :build, "applying tag '#{[:tag]}' to your fancy new build", :green invoke Slugforge::Commands::Tag, [:set, [:tag], dest], [] end end |