Class: CoderCompanion::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/codercompanion.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, location) ⇒ CLI

Returns a new instance of CLI.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/codercompanion.rb', line 119

def initialize(options = {}, location)
    @options = options
    @src_folder = location
    @cache_handler = CacheHandler.new options[:dry_run]

    CoderCompanion.no_color = options[:no_color]
    CoderCompanion.silent_run = options[:silent_run]
    
    print_greeting
    @config = parse_config_file
    verify_config @config

    @api = API.new({
        :access_key     => @config["project"]["access_key"],
        :access_id      => @config["user"]["access_id"],
        :host           => @config["host"]
    })
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



117
118
119
# File 'lib/codercompanion.rb', line 117

def api
  @api
end

#cache_handlerObject (readonly)

Returns the value of attribute cache_handler.



117
118
119
# File 'lib/codercompanion.rb', line 117

def cache_handler
  @cache_handler
end

#configObject (readonly)

Returns the value of attribute config.



117
118
119
# File 'lib/codercompanion.rb', line 117

def config
  @config
end

#languageObject (readonly)

Returns the value of attribute language.



117
118
119
# File 'lib/codercompanion.rb', line 117

def language
  @language
end

#optionsObject (readonly)

Returns the value of attribute options.



117
118
119
# File 'lib/codercompanion.rb', line 117

def options
  @options
end

#src_folderObject (readonly)

Returns the value of attribute src_folder.



117
118
119
# File 'lib/codercompanion.rb', line 117

def src_folder
  @src_folder
end

Class Method Details

.display_supported_languagesObject



231
232
233
234
235
236
237
# File 'lib/codercompanion.rb', line 231

def self.display_supported_languages
    languages = CoderCompanion::Languages.supported_languages
    puts VisualMessage.supported_languages_header
    languages.each do |lang|
        puts VisualMessage.supported_language_row lang
    end
end

Instance Method Details

#dry_run?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/codercompanion.rb', line 139

def dry_run?
    options[:dry_run]
end

#excludedObject



153
154
155
# File 'lib/codercompanion.rb', line 153

def excluded
    options[:exclude]
end

#fresh_runBoolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/codercompanion.rb', line 149

def fresh_run
    options[:fresh_run]
end

#output_fileString

Returns:

  • (String)


144
145
146
# File 'lib/codercompanion.rb', line 144

def output_file
    options[:output_file]
end

#parse_config_fileObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/codercompanion.rb', line 161

def parse_config_file
    @@visit_config_location_message = ::CoderCompanion.error_format("Please visit ") + ::CoderCompanion.error_url_format("http://codercompanion.com") + ::CoderCompanion.error_format(" for more info")
    
    # Using config_filename otherwise default to 'codercmp.conf' in pwd
    config_filename = options[:config_file] ? options[:config_file] : "#{CONFIG_FILE_NAME}"

    file = nil
    begin
        file = File.open(config_filename)
    rescue => e
        ::CoderCompanion.j_print e.to_s
        raise CoderCompanionException.new(::CoderCompanion.error_format("An error occured while trying to open the configuration file"))
    end

    begin
        return JSON.parse(file.read)
    rescue => e
        ::CoderCompanion.j_print ::CoderCompanion.error_format e.to_s
        raise CoderCompanionException.new(::CoderCompanion.error_format("could not parse your config file. ") + @@visit_config_location_message)
    end
    
end


157
158
159
# File 'lib/codercompanion.rb', line 157

def print_greeting
    ::CoderCompanion.j_print VisualMessage.get_greeting_text
end

#runObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
# File 'lib/codercompanion.rb', line 239

def run
    if self.dry_run?
        CoderCompanion.j_print VisualMessage.dry_run_text
    end

    cache_handler.load_cache(fresh_run)
        
    p = CoderCompanion::ParserFactory.get_parser(language)

    # Running test for different languages
    uploadable = p.create_project_json(src_folder, excluded, cache_handler)

    if options[:show_output]
        ap uploadable
    end

    if !uploadable
        ::CoderCompanion.j_print ::CoderCompanion.error_format("Something went wrong while trying to create uploadable")
        return
    end


    if !dry_run? 
        #puts pretty_inspect(uploadable)
        # Maybe wrap this in a begin--rescue block
        # Uncomment this

        file_rel_path = './test_upload.codercompanion'
        if output_file
            ::CoderCompanion.j_print ::CoderCompanion.notification_format("saving output to #{output_file}")
            file_rel_path = output_file
        end
        File.open(file_rel_path, 'w+') {|f| f.write(uploadable.to_json) }
        
        if output_file
            ::CoderCompanion.j_print ::CoderCompanion.success_format("Successfully saved the output")
            return
        end
        #zip file here

        ::CoderCompanion.j_print ::CoderCompanion.notification_format("uploading to #{config["host"]}...")

        # Uncomment this
        # Upload file to be processed
        upload(file_rel_path)

        File.delete(file_rel_path)
        cache_handler.write_cached_data
        ::CoderCompanion.j_print ::CoderCompanion.success_format("Uploaded your changes successfully")
    end
end

#upload(file) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/codercompanion.rb', line 291

def upload(file)
    response = api.get_temporary_access_key
    unless response
        File.delete(file)
        raise CoderCompanion::CoderCompanionException, "Failed to upload project"
    end

    response_data = JSON.parse(response)["data"]
    credentials = response_data["credentials"]
    region = response_data["region"]
    s3_bucket = response_data["s3_bucket"]
    s3_client = CoderCompanion::S3Client.new(
        :session_token => credentials["session_token"], 
        :access_key_id => credentials["access_key_id"], 
        :secret_access_key => credentials["secret_access_key"], 
        :region => region, 
        :s3_bucket => s3_bucket 
    )

    current_time = Time.now.utc.strftime("%Y-%m-%d:%H:%M:%S")
    key = "#{config["user"]["access_id"]}/#{config["project"]["access_key"]}/#{current_time}/upload"

    # put object using the s3_client
    result = s3_client.put_object(file, key)
    unless result
        File.delete(file)
        raise CoderCompanion::CoderCompanionException, "Failed to upload file to S3"
    end 

    # notify the api that we have uploaded the file to s3
    response = api.notify_s3_upload(key)
    unless response
        File.delete(file)
        raise CoderCompanion::CoderCompanionException, "Failed to complete upload. It is possible our servers are down"
    end
end

#verify_config(config) ⇒ Object



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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/codercompanion.rb', line 184

def verify_config config
    ::CoderCompanion.j_print ::CoderCompanion.notification_format("Verifying Config file...")
    error = false
    if config
        if config["project"]
            if config["project"]["access_key"] == nil || config["project"]["access_key"].match(/^\s*$/)
                ::CoderCompanion.j_print ::CoderCompanion.error_format("No access_key for project specified.")
                error = true
            end

            is_supported = true
            @language = config["project"]["language"]
            if language == nil || language.match(/^\s*$/)
                ::CoderCompanion.j_print ::CoderCompanion.error_format("No language specified in your config file.")
                error = true
            elsif !Languages.is_supported?(language)
                is_supported = false
                ::CoderCompanion.j_print ::CoderCompanion.error_format("#{language} is not a supported language")
                ::CoderCompanion.j_print ::CoderCompanion.notification_format("To see a list of supported languages run: \n\tcodercmp --language\n")
                error = true
            end
        else
            ::CoderCompanion.j_print ::CoderCompanion.error_format("Project not found in config file. ") 
            error = true
        end
        if config["user"]
            if config["user"]["access_id"] == nil || config["user"]["access_id"] == ""
                ::CoderCompanion.j_print ::CoderCompanion.error_format("No access_id for project specified.")
                error = true
            end
        else
            ::CoderCompanion.j_print ::CoderCompanion.error_format("User info not found")
            error = true
        end
        if config["host"] == nil || config["host"] == ""
            ::CoderCompanion.j_print ::CoderCompanion.error_format("Host not found")
            error = true
        end
    end

    if error
        raise CoderCompanionException.new(@@visit_config_location_message)
    end

    ::CoderCompanion.j_print ::CoderCompanion.success_format("Config file Successfully Verified")
end