Class: Cf::Badge
- Inherits:
-
Thor
- Object
- Thor
- Cf::Badge
- Includes:
- BadgeYamlValidator, Config
- Defined in:
- lib/cf/cli/badge.rb
Instance Method Summary collapse
- #create(badge_name) ⇒ Object
- #delete(badge_name) ⇒ Object
- #generate(badge_name = nil) ⇒ Object
- #list(name = nil) ⇒ Object
- #update(badge_name) ⇒ Object
Methods included from BadgeYamlValidator
Methods included from Config
#config_file, #find_home, #get_api_key, #load_config, #save_config, #set_api_key, #set_target_uri
Instance Method Details
#create(badge_name) ⇒ Object
48 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 89 90 91 92 93 94 95 96 97 |
# File 'lib/cf/cli/badge.rb', line 48 def create(badge_name) yaml_source = "#{Dir.pwd}/#{badge_name}/badge.yml" unless File.exist?(yaml_source) say "A file with name badge.yml inside #{badge_name} directory does not exists.", :red return end errors = validate(yaml_source) unless errors.present? set_target_uri(false) set_api_key(yaml_source) CF.account_name = CF::Account.info['name'] badge_hash = YAML::load(File.read(yaml_source).strip) name = badge_hash["name"] #check whether the badge form exist or not. If the badge form path looks invalid then it tries to recover and then again check and then also its does not exit then it raise errors. if badge_hash["form"].class == String unless File.exist?(badge_hash["form"]) badge_hash["form"] = "#{badge_name}/#{badge_hash["form"]}" unless File.exist?(badge_hash["form"]) say "Invalid path for badge form.", :red exit(1) end end end do_exist = CF::Badge.show(name.parameterize) if do_exist && do_exist["code"]==200 say "A badge named #{name} already exists.", :yellow override = agree("Do you want to override ? [y/n] ") override == true ? badge_hash.merge!(:confirmed => true) : ((say "Badge creation aborted!", :red) and exit(1)) end say "Creating badge '#{name}'" badge = CF::Badge.new(badge_hash) if badge.errors.present? say "Errors: \n", :red badge.errors.each do |error| say(error, :red) end exit(1) else say "A new badge named '#{name}' created sucessfully!" end else say "Following errors has been encountered in your badge.yml file: #{errors.join(",")}" , :red end end |
#delete(badge_name) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/cf/cli/badge.rb', line 173 def delete(badge_name) #set badge basic credentials line_source = Dir.pwd yaml_source = "#{line_source}/line.yml" set_target_uri(false) set_api_key(yaml_source) set_target_uri(false) CF.account_name = CF::Account.info['name'] #list all badges if name is not present else list the specific badge if badge_name badge_decendents = CF::Badge.check_for_decendents(badge_name) if badge_decendents say("\n!!! Warning !!!\n The badge #{badge_name} has following number of tasks, lines and workers asscociated with it.\n", :yellow) badge_table = table do |t| t.headings = [ 'Tasks', 'Lines', 'Workers'] t << [ badge_decendents[:tasks], badge_decendents[:lines_count], badge_decendents[:workers_count]] end say(badge_table) delete_forcefully = agree("Do you still want to delete this badge? [y/n] ") if delete_forcefully badge_resp = CF::Badge.destroy(badge_name,.merge(:force => true)) else say "Badge deletion aborted!", :yellow exit(1) end else say "Deleting badge named '#{badge_name}'" badge_resp = CF::Badge.destroy(badge_name) end badge_resp["code"] == 200 ? (say "The badge named #{badge_name} deleted sucessfully!", :green) : (say badge_resp["error"]["message"], :red) else say "Name of the badge for deletion is missing.", :red end end |
#generate(badge_name = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cf/cli/badge.rb', line 27 def generate(badge_name = nil) badge_name = badge_name.nil? ? "web_research" : badge_name.chomp(File.extname(badge_name)) source_destination = "#{Dir.pwd}" if .force? FileUtils.rm_rf(badge_name, :verbose => true) end if File.exist?(badge_name) say "Skipping #{badge_name} because it already exists.", :red else say "Generating a new badge template #{badge_name}", :green Cf::NewBadge.start([badge_name, source_destination]) say "Badge template generated successfully.", :green say "Modify files inside #{badge_name} directory and create badge with: cf badge create", :yellow end end |
#list(name = nil) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/cf/cli/badge.rb', line 213 def list(name = nil) #set badge basic credentials line_source = Dir.pwd yaml_source = "#{line_source}/line.yml" set_target_uri(false) set_api_key(yaml_source) set_target_uri(false) CF.account_name = CF::Account.info['name'] #list all badges if name is not present else list the specific badge if name badge_resp = CF::Badge.list(name) else badge_resp = CF::Badge.list end #show list of badges in the tabular view if badges = badge_resp["badges"] say "Listing badges within your account" , :green badges.sort! { |a, b| a['name'] <=> b['name'] } badge_table = table do |t| t.headings = ["Name", 'Number Of Tasks', 'Lines Associated', 'Workers', 'Description'] badges.each do |badge| badge = Hashie::Mash.new(badge) t << [badge.name, badge.num_of_tasks, badge.lines_associated, badge.workers, badge.description] end end say(badge_table) else say badge_resp["error"]["message"], :red end end |
#update(badge_name) ⇒ Object
102 103 104 105 106 107 108 109 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/cf/cli/badge.rb', line 102 def update(badge_name) yaml_source = "#{Dir.pwd}/#{badge_name}/badge.yml" unless File.exist?(yaml_source) say "A file with name badge.yml inside #{badge_name} directory does not exists.", :red return end errors = validate(yaml_source) unless errors.present? set_target_uri(false) set_api_key(yaml_source) CF.account_name = CF::Account.info['name'] badge_hash = YAML::load(File.read(yaml_source).strip) #check whether the badge form exist or not. If the badge form path looks invalid then it tries to recover and then again check and then also its does not exit then it raise errors. if badge_hash["form"].class == String unless File.exist?(badge_hash["form"]) badge_hash["form"] = "#{badge_name}/#{badge_hash["form"]}" unless File.exist?(badge_hash["form"]) say "Invalid path for badge form.", :red exit(1) end end end do_exist = CF::Badge.list(badge_name.parameterize) #check whetether the badge do exist and has its decendents if do then retain_current_worker_badge = true so that worker and lines associated get retain if the badge get updated other wise the badge act as a new badge if do_exist && do_exist["code"]==200 descendents = CF::Badge.check_for_decendents(badge_name.parameterize) if descendents say("\n!!! Warning !!!\n The badge '#{badge_name}' has the following number of tasks, lines and workers asscociated with it.\n", :yellow) badge_table = table do |t| t.headings = [ 'Tasks', 'Lines', 'Workers'] t << [ descendents[:tasks], descendents[:lines_count], descendents[:workers_count]] end say(badge_table) say("\n!!! Warning !!!\n Retaining the workers will keep the workers associated with the badge.\n", :yellow) update_forcefully = agree("Do you want to retain the workers? [y/n] ") badge_hash.merge!(:retain_workers => false) if update_forcefully == false end #now update the badge say "Updating badge named '#{badge_name}'" badge = CF::Badge.update(badge_name,badge_hash) if badge && badge.code == 200 say "A badge named '#{badge_name}' updated sucessfully!" else if badge.code == 404 say "Badge does not exist." elsif badge.code == 422 say badge["error"]["message"], :red else say "A badge named '#{badge_name}' can not be updated", :red end exit(1) end else say "A badge named '#{badge_name}' does not exist!", :red exit(1) end else say "Following errors has been encountered in your badge.yml file: #{errors.join(",")}" , :red end end |