fife 0.0.1.alpha → 0.1.1
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +27 -4
- data/lib/fife/operations/close.rb +1 -0
- data/lib/fife/operations.rb +10 -0
- data/lib/fife/pipe.rb +29 -6
- data/lib/fife/storage/sftp.rb +35 -8
- data/lib/fife/version.rb +1 -1
- data/lib/fife.rb +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ba441e8b0b9a3491cb9878ab9d6791e1b9789f3
|
4
|
+
data.tar.gz: 30d4bbb2a0dd03dd70a845f8d3f952a432979a14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c88d307cd22d1ee490a579d947e833ef246cc17abb2fe33f7ac9dc8c509da15b11101f1260262e085e715d6ca6ff39daef642f4a3b626936d49333b9faa6dab7
|
7
|
+
data.tar.gz: 767cbfd8fc3cc569df285c339c772562f9d3dc072fbb833579cac47b223b39e74ed42cd5637111e6923087e6b00afff7da88ec8be5627f92f474ea8f5c22d39a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -69,7 +69,7 @@ Currently, `Fife` ships with 4 operations: `:noop`, `:name`, `:store` and `:clos
|
|
69
69
|
* noop
|
70
70
|
Performs no operation on the IO, and returns the IO directly.
|
71
71
|
* close
|
72
|
-
Closes the
|
72
|
+
Closes the IO if not already closed.
|
73
73
|
* store
|
74
74
|
Stores the content of the IO.
|
75
75
|
* name
|
@@ -89,7 +89,7 @@ If you feel lambdas are not enough for your job,
|
|
89
89
|
you can easily define your own operations.
|
90
90
|
|
91
91
|
```ruby
|
92
|
-
class
|
92
|
+
class MyOperation
|
93
93
|
def initialize(arg1, arg2)
|
94
94
|
# Initialize the Operation
|
95
95
|
end
|
@@ -98,10 +98,15 @@ class Fife::Operations::MyOperation
|
|
98
98
|
# Handle the io and return some IO instances
|
99
99
|
end
|
100
100
|
end
|
101
|
+
|
102
|
+
# Register it
|
103
|
+
Fife::Operations.register(:my_op, MyOperation)
|
101
104
|
```
|
105
|
+
|
102
106
|
Then you can use it like
|
107
|
+
|
103
108
|
```ruby
|
104
|
-
Fife(io_ary).pipe(:
|
109
|
+
Fife(io_ary).pipe(:my_op, 1, 2)
|
105
110
|
```
|
106
111
|
|
107
112
|
### Storage
|
@@ -120,10 +125,28 @@ the remote filename depends on the name of the IO.
|
|
120
125
|
(I know it's nasty, but it's still under developing)
|
121
126
|
|
122
127
|
```ruby
|
123
|
-
storage = Fife::Storage::Sftp.new
|
128
|
+
storage = Fife::Storage::Sftp.new do
|
129
|
+
host 'localhost'
|
130
|
+
user 'me'
|
131
|
+
remote_dir '/path/to/my/remote/dir'
|
132
|
+
naming -> io {"lorem_ipsum_#{rand}"}
|
133
|
+
ssh_options password: 's3cret'
|
134
|
+
end
|
124
135
|
Fife(io_ary).pipe(:store, storage)
|
125
136
|
```
|
126
137
|
|
138
|
+
### Abort on fail
|
139
|
+
By default, **Fife** ignores all failures and keeps on executing further operations on succeeded operations.
|
140
|
+
This behavior can be changed by passing `abort_on_fail: true` to `Kernel#Fife`:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
Fife(io_ary, abort_on_fail: true)
|
144
|
+
```
|
145
|
+
|
146
|
+
When `abort_on_fail` is turned on, if some operation raises error, no further operation will be executed,
|
147
|
+
and an `Fife::Pipe::Aborted` will be raised.
|
148
|
+
You can get the real causes by accessing `Fife::Pipe::Aborted#causes`
|
149
|
+
|
127
150
|
## Development
|
128
151
|
|
129
152
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/fife/operations.rb
CHANGED
@@ -5,4 +5,14 @@ module Fife::Operations
|
|
5
5
|
autoload :Close
|
6
6
|
autoload :Name
|
7
7
|
autoload :Store
|
8
|
+
|
9
|
+
@registered = ActiveSupport::HashWithIndifferentAccess.new
|
10
|
+
|
11
|
+
def self.register(name, operation_class)
|
12
|
+
@registered[name] = operation_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.[](name)
|
16
|
+
@registered[name] ||= const_get(name.to_s.camelize)
|
17
|
+
end
|
8
18
|
end
|
data/lib/fife/pipe.rb
CHANGED
@@ -1,22 +1,45 @@
|
|
1
1
|
class Fife::Pipe
|
2
2
|
|
3
|
-
|
3
|
+
class Aborted < RuntimeError
|
4
|
+
attr_reader :causes
|
4
5
|
|
5
|
-
|
6
|
-
|
6
|
+
def initialize(causes=[])
|
7
|
+
@causes = causes
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :ios, :abort_on_fail
|
12
|
+
|
13
|
+
def initialize(ios, abort_on_fail)
|
14
|
+
@ios = ios
|
15
|
+
@abort_on_fail = abort_on_fail
|
7
16
|
end
|
8
17
|
|
9
18
|
def pipe(op, *args)
|
10
|
-
op = Fife::Operations
|
19
|
+
op = Fife::Operations[op].new(*args) unless op.respond_to? :call
|
11
20
|
output = []
|
12
21
|
|
13
22
|
ios.map { |io|
|
14
23
|
Thread.new do
|
15
|
-
output <<
|
24
|
+
output << catch(:halt) do
|
25
|
+
begin
|
26
|
+
op.call(io.tap(&:rewind))
|
27
|
+
rescue => e
|
28
|
+
throw :halt, e
|
29
|
+
end
|
30
|
+
end
|
16
31
|
end
|
17
32
|
}.each(&:join)
|
18
33
|
|
19
|
-
|
34
|
+
errors, output = output.tap(&:flatten!).partition{|o| o.is_a?(StandardError)}
|
35
|
+
|
36
|
+
should_abort = abort_on_fail && (errors.any? || output.any?(&:nil?))
|
37
|
+
if should_abort
|
38
|
+
ios.each { |io| io.close unless io.closed? }
|
39
|
+
raise Aborted, errors
|
40
|
+
end
|
41
|
+
|
42
|
+
self.class.new(output.tap(&:compact!), abort_on_fail)
|
20
43
|
end
|
21
44
|
|
22
45
|
end
|
data/lib/fife/storage/sftp.rb
CHANGED
@@ -2,20 +2,47 @@ require 'net/sftp'
|
|
2
2
|
require 'pathname'
|
3
3
|
require 'fileutils'
|
4
4
|
|
5
|
+
class Net::SFTP::Session
|
6
|
+
def mkdir_p!(pathname)
|
7
|
+
pathname = Pathname(pathname) unless pathname.is_a? Pathname
|
8
|
+
pathname.descend do |path|
|
9
|
+
mkdir!(path) rescue nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
5
14
|
class Fife::Storage::Sftp
|
6
|
-
attr_reader :host, :user, :remote_dir, :ssh_options
|
7
15
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
16
|
+
class UninitializedAttributes < RuntimeError; end
|
17
|
+
|
18
|
+
REQUIRED_ATTRIBUTES = [:host, :user, :naming, :ssh_options, :remote_dir]
|
19
|
+
|
20
|
+
def remote_dir(*value)
|
21
|
+
return @remote_dir if value.empty?
|
22
|
+
@remote_dir = Pathname(value[0])
|
23
|
+
end
|
24
|
+
|
25
|
+
(REQUIRED_ATTRIBUTES - [:remote_dir]).each do |attr|
|
26
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
27
|
+
def #{attr}(*value)
|
28
|
+
return @#{attr} if value.empty?
|
29
|
+
@#{attr} = value[0]
|
30
|
+
end
|
31
|
+
RUBY
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(&block)
|
35
|
+
instance_eval &block
|
36
|
+
uninitialized_attributes = REQUIRED_ATTRIBUTES.select{|attr| send(attr).nil?}
|
37
|
+
raise UninitializedAttributes, uninitialized_attributes unless uninitialized_attributes.empty?
|
12
38
|
end
|
13
39
|
|
14
40
|
def store(io)
|
15
|
-
|
16
|
-
|
41
|
+
remote_path = remote_dir.join(@naming.call(io))
|
42
|
+
dir = remote_path.dirname
|
17
43
|
Net::SFTP.start(host, user, ssh_options) do |sftp|
|
18
|
-
sftp.
|
44
|
+
sftp.mkdir_p!(dir)
|
45
|
+
sftp.file.open(remote_path, 'w') { |f| IO.copy_stream(io.tap(&:rewind), f) }
|
19
46
|
end
|
20
47
|
io.close
|
21
48
|
end
|
data/lib/fife/version.rb
CHANGED
data/lib/fife.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fife
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- aetherus
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -124,9 +124,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
124
|
version: '0'
|
125
125
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
126
|
requirements:
|
127
|
-
- - "
|
127
|
+
- - ">="
|
128
128
|
- !ruby/object:Gem::Version
|
129
|
-
version:
|
129
|
+
version: '0'
|
130
130
|
requirements: []
|
131
131
|
rubyforge_project:
|
132
132
|
rubygems_version: 2.4.8
|