Class: Slugforge::Commands::Wrangler

Inherits:
SubCommand show all
Defined in:
lib/slugforge/commands/wrangler.rb

Instance Method Summary collapse

Methods inherited from SubCommand

banner

Methods inherited from Slugforge::Command

#initialize, start

Methods included from Helper

included

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 options[: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

#listObject

Raises:

  • (error_class)


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 !options[:all] && options[: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   = options[: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...options[:count]) unless options[:all]

  logger.say "Slugs for #{project_name} (#{slugs.size}", nil, false
  logger.say " of #{total}", nil, false unless options[:all]
  logger.say ")"

  tag_manager.memoize_slugs_for_tags(project_name)
  slugs.each do |slug|
    tags = tag_manager.tags_for_slug(project_name, slug.key)
    logger.say " #{tags.size > 0 ? ' (' + tags.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

#purgeObject

Raises:

  • (error_class)


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 !options[:all] && options[:keep] <= 0

  slugs = self.slugs(project_name).values

  if options[:all]
    if !options[:yes] && ask("Are you sure you wish to delete #{slugs.size} slugs for #{project_name}? [yN]").downcase == 'y'
      options[:yes] = true
    end

    if options[:yes]
      slugs.each do |slug|
        slug.destroy
        logger.say_status :destroy, slug.key, :red
      end
    end
  elsif slugs.size > options[:keep]
    slugs.slice(options[:keep]..-1).each do |slug|
      if options[: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 #{options[: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 options[:tag]
    logger.say_status :build, "applying tag '#{options[:tag]}' to your fancy new build", :green
    invoke Slugforge::Commands::Tag, [:set, options[:tag], dest], []
  end
end