balmora 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f7d56a33f3e8d7e5e35beeb227e3881023ad7ca
4
- data.tar.gz: 0a4e21d622e6a0c78c0ad5956d0b07b15221525e
3
+ metadata.gz: 9cf8833c64f0f3895e288f09db093d2c365a577d
4
+ data.tar.gz: 405a2c51a8036a656e186d4414d187761a521b5b
5
5
  SHA512:
6
- metadata.gz: 766373a8b25c96fc88762099c517f6bbd8c66d1f404c95c4ed8dbdfe2286da69664c1b694d513fd55de58536d090d810a7ae4cd5e2996085af7c366866a8a8d8
7
- data.tar.gz: ec88c1d20741eda666acf3083a63293d2b1730915cf0f3674b80bd624d80cb30e90ad3809cdb13287186d7ffb5e56cf1f0771b3df9c377cf82143cd562cdfbac
6
+ metadata.gz: 5924a77e1e27a7fba1d0b997ca1f9ffaa3894351fa10fe74a31eec61c501e0e2de4d5b0790d71b2d15c286c245326b329ad8d2974598c8e52ce17b6ef08d3961
7
+ data.tar.gz: 1716e508647620798eb6b07bff6e1de6a4cc939b02329b5800782f7e1d33649ce2c76f30384ae722d9dd2314bbba4c69b3c64a7c01c8371b979fc4b2ca5d1d52
data/bin/balmora CHANGED
File without changes
@@ -7,6 +7,7 @@ class Balmora::Arguments
7
7
 
8
8
  result = {}
9
9
  index = 0
10
+ tail = nil
10
11
  while index < argv.length
11
12
  arg = argv[index]
12
13
 
@@ -19,13 +20,20 @@ class Balmora::Arguments
19
20
  end
20
21
 
21
22
  if new_index.nil?()
22
- return result, argv[index..-1]
23
+ tail = argv[index..-1]
24
+ break
23
25
  end
24
26
 
25
27
  index = new_index + 1
26
28
  end
27
29
 
28
- return result, nil
30
+ options.each() { |key, value|
31
+ if value[:flag] && !result.has_key?(key)
32
+ result[key] = false
33
+ end
34
+ }
35
+
36
+ return result, tail
29
37
  end
30
38
 
31
39
  protected
data/lib/balmora/cli.rb CHANGED
@@ -33,6 +33,12 @@ class Balmora::Cli
33
33
  description: 'Quite mode; reports only errors',
34
34
  },
35
35
 
36
+ dry: {
37
+ shortcut: 'd',
38
+ flag: true,
39
+ description: 'Dry run; will not run modify commands but report it to log',
40
+ },
41
+
36
42
  verbose: {
37
43
  shortcut: 'v',
38
44
  flag: true,
@@ -18,7 +18,7 @@ class Balmora::Command::Exec < Balmora::Command
18
18
  exec = [@shell.expression(exec)]
19
19
  end
20
20
 
21
- @shell.system(exec)
21
+ @shell.system(exec, change: true)
22
22
  end
23
23
 
24
24
  end
@@ -2,7 +2,23 @@ class Balmora::Command::File < Balmora::Command
2
2
 
3
3
  class Error < StandardError; end
4
4
 
5
- OPTIONS = '-T'
5
+ def self.resolve_path(shell, source, storage, path)
6
+ if !source.nil?()
7
+ return shell.expand(source)
8
+ end
9
+
10
+ storage = shell.expand(storage)
11
+
12
+ if path.start_with?('~/')
13
+ result = ::File.join(storage, path[1..-1])
14
+ elsif path.start_with?('/')
15
+ result = ::File.join(storage, path[1..-1])
16
+ else
17
+ result = ::File.join(storage, path)
18
+ end
19
+
20
+ return result
21
+ end
6
22
 
7
23
  def init()
8
24
  super()
@@ -14,18 +30,17 @@ class Balmora::Command::File < Balmora::Command
14
30
  end
15
31
 
16
32
  def options()
17
- return super().concat([:file, :source, :storage, :action, :check_equal,
33
+ return super().concat([:file, :source, :storage, :action, :always,
18
34
  :options])
19
35
  end
20
36
 
21
37
  def run()
22
- if !_run_required?()
38
+ if @always == true || _source_equals_to_target?()
23
39
  return nil
24
40
  end
25
41
 
26
42
  _create_target_path()
27
43
  _copy_file()
28
- # _change_file_owner()
29
44
  end
30
45
 
31
46
  def verify()
@@ -53,51 +68,21 @@ class Balmora::Command::File < Balmora::Command
53
68
  protected
54
69
 
55
70
  def _create_target_path()
56
- @shell.run!(['mkdir', '-p', ::File.dirname(_target_path())], verbose: false)
71
+ check = ['test', '-e', ::File.dirname(_target_path())]
72
+ if @shell.run(check, verbose: false)[0] == 0
73
+ return
74
+ end
75
+
76
+ mkdir = ['mkdir', '-p', ::File.dirname(_target_path())]
77
+ @shell.run!(mkdir, change: true)
57
78
  end
58
79
 
59
80
  def _copy_file()
60
- @shell.run!(['cp', option(:options) || OPTIONS, _source_path(),
61
- _target_path()])
81
+ @shell.run!(['cp', option(:options) || '-T', _source_path(),
82
+ _target_path()], change: true)
62
83
  end
63
84
 
64
- # def _change_file_owner()
65
- # new_owner = _get_target_required_owner()
66
- # if _get_target_actual_owner() == new_owner
67
- # return
68
- # end
69
-
70
- # @shell.run!(['chown', new_owner, '-R', _target_path()])
71
- # end
72
-
73
- # def _get_target_actual_owner()
74
- # owner =
75
- # @shell.run!(['stat', '-c', '%U:%G', _target_path()], verbose: false).
76
- # strip()
77
-
78
- # return owner
79
- # end
80
-
81
- # def _get_target_required_owner()
82
- # owner =
83
- # if @action == 'pull'
84
- # @owner
85
- # elsif @action == 'push'
86
- # @storage_owner
87
- # else
88
- # raise Error.new("Can not get owner for action #{@action}")
89
- # end
90
-
91
- # owner = owner || @shell.user_id()
92
-
93
- # return owner
94
- # end
95
-
96
- def _run_required?()
97
- # if @check_equal == false
98
- # return true
99
- # end
100
-
85
+ def _source_equals_to_target?()
101
86
  command = [
102
87
  'test', '-e', _source_path(), _expr('&&'),
103
88
  *@shell.sudo(), 'test', '-e', _target_path(), _expr('&&'),
@@ -106,7 +91,7 @@ class Balmora::Command::File < Balmora::Command
106
91
  _expr('"$('), *@shell.sudo(), *_target_contents(), _expr('| md5sum)" ]'),
107
92
  ]
108
93
 
109
- return @shell.run(command, verbose: false)[0] != 0
94
+ return @shell.run(command, verbose: false)[0] == 0
110
95
  end
111
96
 
112
97
  def _source_contents()
@@ -129,14 +114,6 @@ class Balmora::Command::File < Balmora::Command
129
114
  return @shell.expression(expression)
130
115
  end
131
116
 
132
- def _owner()
133
- return @owner || ENV['USER']
134
- end
135
-
136
- def _storage_owner()
137
- return @storage_owner || ENV['USER']
138
- end
139
-
140
117
  private
141
118
 
142
119
  def _files()
@@ -148,21 +125,7 @@ class Balmora::Command::File < Balmora::Command
148
125
  end
149
126
 
150
127
  def _resolve_source_path()
151
- if !@source.nil?()
152
- return @shell.expand(@source)
153
- end
154
-
155
- storage = @shell.expand(@storage)
156
-
157
- if @file.start_with?('~/')
158
- result = ::File.join(storage, @file[1..-1])
159
- elsif @file.start_with?('/')
160
- result = ::File.join(storage, @file[1..-1])
161
- else
162
- result = ::File.join(storage, @file)
163
- end
164
-
165
- return result
128
+ return File.resolve_path(@shell, @source, @storage, @file)
166
129
  end
167
130
 
168
131
  end
@@ -62,8 +62,8 @@ class Balmora::Command::Files < Balmora::Command
62
62
  path = @shell.expand(path)
63
63
  end
64
64
 
65
- command = ['test', '-d', path, @shell.expression('&&'), 'find', path,
66
- '-type', 'f']
65
+ command = ['test', '-d', path, @shell.expression('&&'), *@shell.sudo(),
66
+ 'find', path, '-type', 'f']
67
67
 
68
68
  dir_status, dir_files = @shell.run(command, verbose: false)
69
69
  if dir_status != 0
@@ -0,0 +1,106 @@
1
+ class Balmora::Command::Link < Balmora::Command
2
+
3
+ class Error < StandardError; end
4
+
5
+ def init()
6
+ super()
7
+
8
+ @link = @variables.inject(@link)
9
+ @source = @variables.inject(@source)
10
+ @storage = @variables.inject(@storage)
11
+ end
12
+
13
+ def options()
14
+ return super().concat([:link, :source, :storage])
15
+ end
16
+
17
+ def run()
18
+ if _exists?(_target())
19
+ if _link_exists?()
20
+ return
21
+ end
22
+
23
+ if !_exists?(_source())
24
+ @shell.run!(['mkdir', '-p', ::File.dirname(_source())], change: true)
25
+ @shell.run!(['mv', _target(), _source()], change: true)
26
+ elsif _equals?(_source(), _target())
27
+ @shell.run!(['rm', '-rf', _target()], change: true)
28
+ end
29
+ end
30
+
31
+ _create_target_path()
32
+ @shell.run!(['ln', @options || '-s', _source(), _target()], change: true)
33
+ end
34
+
35
+ def _exists?(file)
36
+ status, _ = @shell.run(['test', '-e', file], verbose: false)
37
+ return status == 0
38
+ end
39
+
40
+ def _link_exists?(d = false)
41
+ stat_status, stat_result = @shell.run(['stat', _target()],
42
+ verbose: false)
43
+
44
+ if stat_status != 0
45
+ return false
46
+ end
47
+
48
+ if d
49
+ p([_target, stat_status, stat_result])
50
+ end
51
+
52
+ if !stat_result.include?(_source())
53
+ return false
54
+ end
55
+
56
+ return true
57
+ end
58
+
59
+ def verify()
60
+ if @link.nil?()
61
+ raise Error.new('"link" should be defined')
62
+ end
63
+
64
+ if !@storage.nil?() && !@source.nil?()
65
+ raise Error.new('"storage" and "source" could not be defined together')
66
+ end
67
+
68
+ if @storage.nil?() && @source.nil?()
69
+ raise Error.new('"storage" or "source" should be defined')
70
+ end
71
+ end
72
+
73
+ def _equals?(file1, file2)
74
+ command = [
75
+ 'test', '-e', file1, _expr('&&'),
76
+ *@shell.sudo(), 'test', '-e', file2, _expr('&&'),
77
+ _expr('[ "$('), *@shell.sudo(), 'cat', file1, _expr('| md5sum'),
78
+ _expr(')" = '),
79
+ _expr('"$('), *@shell.sudo(), 'cat', file2, _expr('| md5sum)" ]')
80
+ ]
81
+
82
+ return @shell.run(command, verbose: false)[0] == 0
83
+ end
84
+
85
+ protected
86
+
87
+ def _expr(string)
88
+ return @shell.expression(string)
89
+ end
90
+
91
+ def _create_target_path()
92
+ @shell.run!(
93
+ ['mkdir', '-p', ::File.dirname(@shell.expand(@link))],
94
+ verbose: false
95
+ )
96
+ end
97
+
98
+ def _source()
99
+ return Balmora::Command::File.resolve_path(@shell, @source, @storage, @link)
100
+ end
101
+
102
+ def _target()
103
+ return @shell.expand(@link)
104
+ end
105
+
106
+ end
@@ -0,0 +1,45 @@
1
+ class Balmora::Command::Links < Balmora::Command
2
+
3
+ class Error < StandardError; end
4
+
5
+ def init()
6
+ super()
7
+
8
+ @links = @variables.inject(@links)
9
+ @storage = @variables.inject(@storage)
10
+ end
11
+
12
+ def options()
13
+ return super().concat([:links, :storage])
14
+ end
15
+
16
+ def verify()
17
+ if @links.nil?()
18
+ raise Error.new('"links" should be defined')
19
+ end
20
+
21
+ if @storage.nil?()
22
+ raise Error.new('"storage" should be defined')
23
+ end
24
+ end
25
+
26
+ def run()
27
+ @links.each() { |link|
28
+ command = _get_command(link)
29
+ @balmora.run_command(@state, command)
30
+ }
31
+ end
32
+
33
+ def _get_command(link)
34
+ command = (@options || {}).merge(command: 'link', link: link)
35
+
36
+ if link.instance_of?(::Hash)
37
+ command.merge!(link)
38
+ end
39
+
40
+ command.merge!(storage: @storage)
41
+
42
+ return command
43
+ end
44
+
45
+ end
@@ -1,3 +1,5 @@
1
+ require 'balmora/command/pacman'
2
+
1
3
  class Balmora::Command::Yaourt < Balmora::Command::Pacman
2
4
 
3
5
  def initialize(state, command)
@@ -74,7 +74,7 @@ class Balmora::Config
74
74
  @old = _deep_clone(@config)
75
75
 
76
76
  Dir.chdir(File.dirname(@path)) {
77
- @config = _load(@path)
77
+ @config = _load(File.basename(@path))
78
78
  }
79
79
 
80
80
  @config[:config_dir] = File.dirname(@path)
@@ -31,7 +31,7 @@ class Balmora::Context < Balmora::Command
31
31
  end
32
32
 
33
33
  def options()
34
- return [:operator, :value]
34
+ return [:operator, :operand]
35
35
  end
36
36
 
37
37
  def verify()
@@ -39,8 +39,8 @@ class Balmora::Context < Balmora::Command
39
39
  raise Error.new('"operator" should be defined')
40
40
  end
41
41
 
42
- if @value.nil?()
43
- raise Error.new('"value" should be defined')
42
+ if @operand.nil?()
43
+ raise Error.new('"operand" should be defined')
44
44
  end
45
45
  end
46
46
 
@@ -57,17 +57,17 @@ class Balmora::Context < Balmora::Command
57
57
 
58
58
  case operator
59
59
  when 'match'
60
- return _not(is_not, result.match(@value) != nil)
60
+ return _not(is_not, result.match(@operand) != nil)
61
61
  when 'equal'
62
- return _not(is_not, result == @value)
62
+ return _not(is_not, result == @operand)
63
63
  when 'greater'
64
- return _not(is_not, result > @value)
64
+ return _not(is_not, result > @operand)
65
65
  when 'greater-or-equal'
66
- return _not(is_not, result >= @value)
66
+ return _not(is_not, result >= @operand)
67
67
  when 'lesser'
68
- return _not(is_not, result < @value)
68
+ return _not(is_not, result < @operand)
69
69
  when 'lesser-or-equal'
70
- return _not(is_not, result <= @value)
70
+ return _not(is_not, result <= @operand)
71
71
  end
72
72
 
73
73
  raise Error.new("Unknown operator #{operator}")
@@ -0,0 +1,19 @@
1
+ class Balmora::Context::Value < Balmora::Context
2
+
3
+ def options()
4
+ return super().concat([:value])
5
+ end
6
+
7
+ def verify()
8
+ if !@value
9
+ raise Error.new('"value" should be defined')
10
+ end
11
+
12
+ super()
13
+ end
14
+
15
+ def run()
16
+ return @variables.inject(@value)
17
+ end
18
+
19
+ end
@@ -25,11 +25,10 @@ class Balmora::Contexts
25
25
  context: 'exec-result',
26
26
  exec: context,
27
27
  operator: 'equal',
28
- value: 0,
28
+ operand: 0,
29
29
  }
30
30
  end
31
31
 
32
-
33
32
  context_class = @extension.get(Balmora::Context, context[:context])
34
33
  context_instance = context_class.new(@state, context)
35
34
  context_instance.init()
@@ -67,10 +66,4 @@ class Balmora::Contexts
67
66
  return result
68
67
  end
69
68
 
70
- end
71
-
72
- # [
73
- # {"context": "test -e folder/file1; echo $?", "operator": "equal", "value": "0"},
74
- # "or",
75
- # {"context": "test -e folder/file2; echo $?", "operator": "equal", "value": "0"},
76
- # ]
69
+ end
@@ -7,10 +7,10 @@ module Balmora::Extension::FileSecret
7
7
  def _copy_file()
8
8
  if @action == 'pull'
9
9
  @shell.run!(_source_contents() + [_expr('|'), *@shell.sudo(), 'tee',
10
- _target_path()])
10
+ _target_path()], change: true)
11
11
  else
12
12
  @shell.run!(['cat', _source_path(), _expr('|'), *_encrypt(), _expr('|'),
13
- *@shell.sudo(), 'tee', _target_path()])
13
+ *@shell.sudo(), 'tee', _target_path()], change: true)
14
14
  end
15
15
  end
16
16
 
@@ -1,10 +1,21 @@
1
1
  require 'logger'
2
+ require 'term/ansicolor'
2
3
 
3
4
  class Balmora::Logger < Logger
4
5
 
5
6
  def self.factory(state)
6
7
  logger = self.new(STDOUT)
7
8
 
9
+ logger.formatter = proc { |severity, _, _, msg|
10
+ if severity == 'DEBUG'
11
+ puts(Term::ANSIColor.blue() { msg })
12
+ elsif severity == 'INFO'
13
+ puts(Term::ANSIColor.green() { msg })
14
+ elsif severity == 'ERROR'
15
+ puts(Term::ANSIColor.red() { msg })
16
+ end
17
+ }
18
+
8
19
  if state.options[:debug] == true && state.options[:quite] == true
9
20
  raise Error.new("Options --quite and --verbose can not be set " +
10
21
  "simulataneously")
data/lib/balmora/shell.rb CHANGED
@@ -6,9 +6,9 @@ class Balmora::Shell
6
6
 
7
7
  attr_reader :status
8
8
 
9
- def initialize(message, status)
9
+ def initialize(message, status = nil)
10
10
  super(message)
11
- @status = status
11
+ @status = status || 1
12
12
  end
13
13
 
14
14
  end
@@ -27,11 +27,13 @@ class Balmora::Shell
27
27
  attr_reader :home, :user_id
28
28
 
29
29
  def self.factory(state)
30
- return self.new(state.logger)
30
+ return self.new(state.logger, state.options[:dry])
31
31
  end
32
32
 
33
- def initialize(logger, home = nil)
33
+ def initialize(logger, dry, home = nil)
34
34
  @logger = logger
35
+ @dry = dry
36
+
35
37
  @run = ::Object.method(:'`')
36
38
  @system = ::Object.method(:system)
37
39
  @status = Proc.new() { $?.exitstatus }
@@ -70,12 +72,22 @@ class Balmora::Shell
70
72
  }.
71
73
  join(' ')
72
74
 
75
+ dry = false
76
+ if options[:change] == true && @dry
77
+ dry = true
78
+ shell_command = "dry: #{shell_command}"
79
+ end
80
+
73
81
  if options[:verbose] != false
74
82
  @logger.info(shell_command)
75
83
  else
76
84
  @logger.debug(shell_command)
77
85
  end
78
86
 
87
+ if dry
88
+ return 0, ''
89
+ end
90
+
79
91
  method = @run
80
92
  if options[:system] == true
81
93
  method = @system
@@ -0,0 +1,7 @@
1
+ class Balmora::Variables::Options
2
+
3
+ def self.factory(state)
4
+ return state.options
5
+ end
6
+
7
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: balmora
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonid Shagabutdinov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-20 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2015-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: term-ansicolor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
13
27
  description: Balmora - linux task runner
14
28
  email: leonid@shagabutdinov.com
15
29
  executables:
@@ -26,6 +40,8 @@ files:
26
40
  - lib/balmora/command/exec.rb
27
41
  - lib/balmora/command/file.rb
28
42
  - lib/balmora/command/files.rb
43
+ - lib/balmora/command/link.rb
44
+ - lib/balmora/command/links.rb
29
45
  - lib/balmora/command/pacman.rb
30
46
  - lib/balmora/command/reload_config.rb
31
47
  - lib/balmora/command/restart.rb
@@ -37,6 +53,7 @@ files:
37
53
  - lib/balmora/context/config_changed.rb
38
54
  - lib/balmora/context/exec.rb
39
55
  - lib/balmora/context/exec_result.rb
56
+ - lib/balmora/context/value.rb
40
57
  - lib/balmora/contexts.rb
41
58
  - lib/balmora/extension.rb
42
59
  - lib/balmora/extension/file_secret.rb
@@ -46,6 +63,7 @@ files:
46
63
  - lib/balmora/state.rb
47
64
  - lib/balmora/variables.rb
48
65
  - lib/balmora/variables/config.rb
66
+ - lib/balmora/variables/options.rb
49
67
  - lib/balmora/variables/variables.rb
50
68
  homepage: http://github.com/shagabutdinov/balmora
51
69
  licenses: