Class: FaaStRuby::Command::Workspace::CP

Inherits:
BaseCommand show all
Defined in:
lib/faastruby/cli/commands/workspace/cp.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

#has_user_logged_in?, #help, #load_credentials, #load_yaml, #say, spin, #spin, #write_file

Constructor Details

#initialize(args) ⇒ CP

Returns a new instance of CP.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/faastruby/cli/commands/workspace/cp.rb', line 10

def initialize(args)
  @args = args
  help
  @source_file = @args.shift
  @workspace_name, @relative_path = @args.shift.split(':')
  validate_command
  @package_file = Tempfile.new('package')
  @tmpdir = Dir.mktmpdir("package")
  # FaaStRuby::CLI.error(@missing_args, color: nil) if missing_args.any?
  load_credentials
end

Class Method Details

.helpObject



47
48
49
# File 'lib/faastruby/cli/commands/workspace/cp.rb', line 47

def self.help
  "cp SOURCE_FILE WORKSPACE_NAME:/DESTINATION/PATH"
end

Instance Method Details

#build_packageObject



38
39
40
41
42
43
44
45
# File 'lib/faastruby/cli/commands/workspace/cp.rb', line 38

def build_package
  FileUtils.cp @source_file, @tmpdir
  output_file = @package_file.path
  FaaStRuby::Package.new(@tmpdir, output_file).build
  # FaaStRuby::Command::Function::Build.build(@source_file, output_file, @function_name, true)
  @package_file.close
  output_file
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/faastruby/cli/commands/workspace/cp.rb', line 22

def run
  destination_url = "#{FaaStRuby.workspace_host_for(@workspace_name)}/#{@relative_path}"
  spinner = say("[#{@source_file}] Copying file to '#{destination_url}'...", quiet: true)
  workspace = FaaStRuby::Workspace.new(name: @workspace_name)
  package = build_package
  workspace.upload_file(package, relative_path: @relative_path)
  FaaStRuby::CLI.error(workspace.errors) if workspace.errors.any?
  # spinner.stop("Done!")
  puts "* [#{@source_file}] Upload OK".green
  puts "* [#{@source_file}] URL: #{destination_url}".green
ensure
  FileUtils.remove_entry @tmpdir
  @package_file.unlink
end

#usageObject



51
52
53
54
# File 'lib/faastruby/cli/commands/workspace/cp.rb', line 51

def usage
  puts "\n# Deploy static file SOURCE_FILE to workspace path /DESTINATION/PATH.\n"
  puts "\nUsage: faastruby #{self.class.help}\n\n"
end