faastruby 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/CHANGELOG.md +11 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +67 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +39 -0
  8. data/Rakefile +2 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/example-blank/Gemfile +5 -0
  12. data/example-blank/handler.rb +4 -0
  13. data/example-blank/spec/handler_spec.rb +11 -0
  14. data/example-blank/spec/helpers/faastruby.rb +23 -0
  15. data/example-blank/spec/spec_helper.rb +2 -0
  16. data/example/Gemfile +5 -0
  17. data/example/handler.rb +17 -0
  18. data/example/spec/handler_spec.rb +20 -0
  19. data/example/spec/helpers/faastruby.rb +23 -0
  20. data/example/spec/spec_helper.rb +2 -0
  21. data/exe/faastruby +5 -0
  22. data/faastruby.gemspec +41 -0
  23. data/lib/faastruby.rb +6 -0
  24. data/lib/faastruby/api.rb +94 -0
  25. data/lib/faastruby/base.rb +53 -0
  26. data/lib/faastruby/cli.rb +42 -0
  27. data/lib/faastruby/cli/commands.rb +61 -0
  28. data/lib/faastruby/cli/commands/function.rb +31 -0
  29. data/lib/faastruby/cli/commands/function/build.rb +65 -0
  30. data/lib/faastruby/cli/commands/function/deploy.rb +62 -0
  31. data/lib/faastruby/cli/commands/function/new.rb +99 -0
  32. data/lib/faastruby/cli/commands/function/remove_from.rb +71 -0
  33. data/lib/faastruby/cli/commands/function/run.rb +135 -0
  34. data/lib/faastruby/cli/commands/function/test.rb +42 -0
  35. data/lib/faastruby/cli/commands/function/update_context.rb +67 -0
  36. data/lib/faastruby/cli/commands/function/upgrade.rb +69 -0
  37. data/lib/faastruby/cli/commands/help.rb +31 -0
  38. data/lib/faastruby/cli/commands/version.rb +13 -0
  39. data/lib/faastruby/cli/commands/workspace.rb +12 -0
  40. data/lib/faastruby/cli/commands/workspace/create.rb +70 -0
  41. data/lib/faastruby/cli/commands/workspace/destroy.rb +66 -0
  42. data/lib/faastruby/cli/commands/workspace/list.rb +54 -0
  43. data/lib/faastruby/cli/credentials.rb +49 -0
  44. data/lib/faastruby/cli/package.rb +46 -0
  45. data/lib/faastruby/function.rb +27 -0
  46. data/lib/faastruby/version.rb +3 -0
  47. data/lib/faastruby/workspace.rb +49 -0
  48. metadata +203 -0
@@ -0,0 +1,66 @@
1
+ module FaaStRuby
2
+ module Command
3
+ module Workspace
4
+ class Destroy < WorkspaceBaseCommand
5
+ def initialize(args)
6
+ @args = args
7
+ @missing_args = []
8
+ FaaStRuby::CLI.error(@missing_args, color: nil) if missing_args.any?
9
+ @workspace_name = @args.shift
10
+ parse_options
11
+ FaaStRuby::Credentials.load_for(@workspace_name)
12
+ @options['credentials_file'] ||= FAASTRUBY_CREDENTIALS
13
+ end
14
+
15
+ def run
16
+ warning unless @options['force']
17
+ FaaStRuby::CLI.error("Cancelled") unless @options['force'] == 'y'
18
+ workspace = FaaStRuby::Workspace.new(name: @workspace_name)
19
+ workspace.destroy
20
+ FaaStRuby::CLI.error(workspace.errors) if workspace.errors.any?
21
+ FaaStRuby::Credentials.remove(@workspace_name, @options['credentials_file'])
22
+ puts "Workspace '#{@workspace_name}' was deleted from the server"
23
+ end
24
+
25
+ private
26
+
27
+ def warning
28
+ print "WARNING: ".red
29
+ puts "This action will permanently remove the workspace '#{@workspace_name}' and all its functions from the server."
30
+ print "Are you sure? [y/N] "
31
+ @options['force'] = STDIN.gets.chomp
32
+ end
33
+
34
+ def missing_args
35
+ if @args.empty?
36
+ @missing_args << "Missing argument: WORKSPACE_NAME".red
37
+ @missing_args << "Usage: faastruby destroy-workspace WORKSPACE_NAME"
38
+ end
39
+ FaaStRuby::CLI.error(["'#{@args.first}' is not a valid workspace name.".red, usage], color: nil) if @args.first =~ /^-.*/
40
+ @missing_args
41
+ end
42
+
43
+ def self.help
44
+ "destroy-workspace".blue + " WORKSPACE_NAME [-y, --yes]"
45
+ end
46
+
47
+ def usage
48
+ "Usage: faastruby #{self.class.help}"
49
+ end
50
+
51
+ def parse_options
52
+ @options = {}
53
+ while @args.any?
54
+ option = @args.shift
55
+ case option
56
+ when '-y'
57
+ @options['force'] = 'y'
58
+ else
59
+ FaaStRuby::CLI.error("Unknown argument: #{option}")
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,54 @@
1
+ module FaaStRuby
2
+ module Command
3
+ module Workspace
4
+ class List < WorkspaceBaseCommand
5
+ def initialize(args)
6
+ @args = args
7
+ @missing_args = []
8
+ FaaStRuby::CLI.error(@missing_args, color: nil) if missing_args.any?
9
+ @workspace_name = @args.shift
10
+ FaaStRuby::Credentials.load_for(@workspace_name)
11
+ end
12
+
13
+ def run
14
+ workspace = FaaStRuby::Workspace.new(name: @workspace_name).fetch
15
+ FaaStRuby::CLI.error(workspace.errors) if workspace.errors.any?
16
+ print_functions_table(workspace.functions)
17
+ end
18
+
19
+ def print_functions_table(functions)
20
+ no_functions unless functions.any?
21
+ rows = functions.map do |function_name|
22
+ [function_name, "#{HOST}/#{@workspace_name}/#{function_name}"]
23
+ end
24
+ table = TTY::Table.new(['FUNCTION','ENDPOINT'], rows)
25
+ puts table.render(:basic)
26
+ end
27
+
28
+ def self.help
29
+ "list-workspace".blue + " WORKSPACE_NAME"
30
+ end
31
+
32
+ def usage
33
+ "Usage: faastruby #{self.class.help}"
34
+ end
35
+
36
+ private
37
+
38
+ def missing_args
39
+ if @args.empty?
40
+ @missing_args << "Missing argument: WORKSPACE_NAME".red
41
+ @missing_args << usage
42
+ end
43
+ FaaStRuby::CLI.error(["'#{@args.first}' is not a valid workspace name.".red, usage], color: nil) if @args.first =~ /^-.*/
44
+ @missing_args
45
+ end
46
+
47
+ def no_functions
48
+ puts "The workspace '#{@workspace_name}' has no functions."
49
+ exit 0
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,49 @@
1
+ module FaaStRuby
2
+ class Credentials # TODO: change it to YAML?
3
+ def self.load_credentials_file
4
+ if File.file?(FAASTRUBY_CREDENTIALS)
5
+ Oj.load(File.read(FAASTRUBY_CREDENTIALS))
6
+ else
7
+ {}
8
+ end
9
+ end
10
+
11
+ def self.add(workspace_name, new_credentials, credentials_file)
12
+ credentials = load_credentials_file
13
+ credentials.merge!({workspace_name => new_credentials})
14
+ save_file(credentials, credentials_file)
15
+ end
16
+
17
+ def self.remove(workspace_name, credentials_file)
18
+ credentials = load_credentials_file
19
+ credentials.delete_if{|k,v| k == workspace_name}
20
+ save_file(credentials, credentials_file)
21
+ end
22
+
23
+ def self.save_file(credentials, credentials_file)
24
+ if File.file?(credentials_file)
25
+ color = :yellow
26
+ symbol = '~'
27
+ else
28
+ color = :green
29
+ symbol = '+'
30
+ end
31
+ File.open(credentials_file, 'w') {|f| f.write Oj.dump(credentials)}
32
+ puts "#{symbol} f #{credentials_file}".colorize(color)
33
+ end
34
+
35
+ def self.load_for(workspace_name)
36
+ credentials = load_from_env(workspace_name) || load_credentials_file
37
+ FaaStRuby::CLI.error("Could not find credentials for '#{workspace_name}' in '#{FAASTRUBY_CREDENTIALS}'") unless credentials.has_key?(workspace_name)
38
+ FaaStRuby.configure do |config|
39
+ config.api_key = credentials[workspace_name]['api_key']
40
+ config.api_secret = credentials[workspace_name]['api_secret']
41
+ end
42
+ end
43
+
44
+ def self.load_from_env(workspace_name)
45
+ return nil unless ENV['FAASTRUBY_API_KEY'] && ENV['FAASTRUBY_API_SECRET']
46
+ {workspace_name => {'api_key' => ENV['FAASTRUBY_API_KEY'], 'api_secret' => ENV['FAASTRUBY_API_SECRET']}}
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,46 @@
1
+ module FaaStRuby
2
+ class Package
3
+ # Initialize with the directory to zip and the location of the output archive.
4
+ def initialize(input_dir, output_file)
5
+ @input_dir = input_dir
6
+ @output_file = output_file
7
+ end
8
+
9
+ # Zip the input directory.
10
+ def build
11
+ entries = Dir.entries(@input_dir)
12
+ entries.delete_if {|e| ['.', '..', '.git', @output_file.split('/').last].include?(e)}
13
+ FileUtils.rm_f(@output_file) # Make sure file doesn't exist
14
+ ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
15
+ write_entries entries, '', zipfile
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ # A helper method to make the recursion work.
22
+ def write_entries(entries, path, zipfile)
23
+ entries.each do |e|
24
+ zipfile_path = path == '' ? e : File.join(path, e)
25
+ disk_file_path = File.join(@input_dir, zipfile_path)
26
+ if File.directory?(disk_file_path)
27
+ recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
28
+ next
29
+ end
30
+ put_into_archive(disk_file_path, zipfile, zipfile_path)
31
+ end
32
+ end
33
+
34
+ def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
35
+ zipfile.mkdir zipfile_path
36
+ subdir = Dir.entries(disk_file_path) - %w(. ..)
37
+ write_entries subdir, zipfile_path, zipfile
38
+ end
39
+
40
+ def put_into_archive(disk_file_path, zipfile, zipfile_path)
41
+ zipfile.get_output_stream(zipfile_path) do |f|
42
+ f.write(File.open(disk_file_path, 'rb').read)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,27 @@
1
+ module FaaStRuby
2
+ class Function < BaseObject
3
+ attr_accessor :name, :workspace, :errors, :context
4
+
5
+ def run(options)
6
+ method = options['method'] || 'get'
7
+ headers = options['headers'] || {}
8
+ response = @api.run(function_name: name, workspace_name: options['workspace_name'], payload: options['body'], method: method, headers: headers, time: options['time'], query: options['query'])
9
+ response
10
+ end
11
+
12
+ def destroy
13
+ response = @api.delete_from_workspace(function: self, workspace: @workspace)
14
+ @errors += response.errors if response.errors.any?
15
+ end
16
+
17
+ def update
18
+ payload = {'context' => context}
19
+ response = @api.update_function_context(function: self, workspace: @workspace, payload: payload)
20
+ @errors += response.errors if response.errors.any?
21
+ unless @errors.any?
22
+ self.context = response.body['context']
23
+ end
24
+ self
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module FaaStRuby
2
+ VERSION = '0.2.0'
3
+ end
@@ -0,0 +1,49 @@
1
+ module FaaStRuby
2
+ class Workspace < BaseObject
3
+
4
+ ##### Class methods
5
+ def self.create(name:, email: nil)
6
+ api = API.new
7
+ workspace = Workspace.new(name: name, email: email, errors: [])
8
+ response = api.create_workspace(workspace_name: name, email: email)
9
+ if response.errors.any?
10
+ workspace.errors += response.errors
11
+ return workspace
12
+ end
13
+ case response.code
14
+ when 422
15
+ workspace.errors += ['(422) Unprocessable Entity', response.body]
16
+ when 200
17
+ workspace.credentials = response.body['credentials']
18
+ else
19
+ workspace.errors << "(#{response.code}) Error"
20
+ end
21
+ return workspace
22
+ end
23
+ ###################
24
+
25
+ ##### Instance methods
26
+ attr_accessor :name, :errors, :functions, :email, :object, :credentials
27
+
28
+ def destroy
29
+ response = @api.destroy_workspace(@name)
30
+ @errors += response.errors if response.errors.any?
31
+ end
32
+
33
+ def deploy(package_file_name)
34
+ response = @api.deploy(workspace_name: @name, package: package_file_name)
35
+ @errors += response.errors if response.errors.any?
36
+ self
37
+ end
38
+
39
+ def fetch
40
+ response = @api.get_workspace_info(@name)
41
+ if response.errors.any?
42
+ @errors += response.errors
43
+ else
44
+ self.assign_attributes(response.body)
45
+ end
46
+ self
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,203 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faastruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Paulo Arruda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-spinner
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-table
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubyzip
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: colorize
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.8'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.16'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.16'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ description:
126
+ email:
127
+ - parrudaj@gmail.com
128
+ executables:
129
+ - faastruby
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - CHANGELOG.md
135
+ - Gemfile
136
+ - Gemfile.lock
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/setup
142
+ - example-blank/Gemfile
143
+ - example-blank/handler.rb
144
+ - example-blank/spec/handler_spec.rb
145
+ - example-blank/spec/helpers/faastruby.rb
146
+ - example-blank/spec/spec_helper.rb
147
+ - example/Gemfile
148
+ - example/handler.rb
149
+ - example/spec/handler_spec.rb
150
+ - example/spec/helpers/faastruby.rb
151
+ - example/spec/spec_helper.rb
152
+ - exe/faastruby
153
+ - faastruby.gemspec
154
+ - lib/faastruby.rb
155
+ - lib/faastruby/api.rb
156
+ - lib/faastruby/base.rb
157
+ - lib/faastruby/cli.rb
158
+ - lib/faastruby/cli/commands.rb
159
+ - lib/faastruby/cli/commands/function.rb
160
+ - lib/faastruby/cli/commands/function/build.rb
161
+ - lib/faastruby/cli/commands/function/deploy.rb
162
+ - lib/faastruby/cli/commands/function/new.rb
163
+ - lib/faastruby/cli/commands/function/remove_from.rb
164
+ - lib/faastruby/cli/commands/function/run.rb
165
+ - lib/faastruby/cli/commands/function/test.rb
166
+ - lib/faastruby/cli/commands/function/update_context.rb
167
+ - lib/faastruby/cli/commands/function/upgrade.rb
168
+ - lib/faastruby/cli/commands/help.rb
169
+ - lib/faastruby/cli/commands/version.rb
170
+ - lib/faastruby/cli/commands/workspace.rb
171
+ - lib/faastruby/cli/commands/workspace/create.rb
172
+ - lib/faastruby/cli/commands/workspace/destroy.rb
173
+ - lib/faastruby/cli/commands/workspace/list.rb
174
+ - lib/faastruby/cli/credentials.rb
175
+ - lib/faastruby/cli/package.rb
176
+ - lib/faastruby/function.rb
177
+ - lib/faastruby/version.rb
178
+ - lib/faastruby/workspace.rb
179
+ homepage: https://faastruby.io
180
+ licenses:
181
+ - MIT
182
+ metadata: {}
183
+ post_install_message:
184
+ rdoc_options: []
185
+ require_paths:
186
+ - lib
187
+ required_ruby_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: 2.4.0
192
+ required_rubygems_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ requirements: []
198
+ rubyforge_project:
199
+ rubygems_version: 2.7.7
200
+ signing_key:
201
+ specification_version: 4
202
+ summary: FaaStRuby CLI - Manage workspaces and functions hosted at faastruby.io.
203
+ test_files: []