rbdash 0.2.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7bd45279cad296b0dc993904fa6bfc2ee3cf5089
4
- data.tar.gz: 2db662d897905f8bc1949191c3e759b937905706
3
+ metadata.gz: bb38bbb5168551aa34c9d8b19a7365257c5eafc0
4
+ data.tar.gz: 797eab469029bd4e9f82489b8f51513e24574104
5
5
  SHA512:
6
- metadata.gz: 2aa862983c38f4af37dda8abab348bcca917b3dd2ea4a7caf8991ee6242d196310a16b582bd5de296bd94f83e4f5cef19434f5cfff1ac84185ecdfd552af6649
7
- data.tar.gz: 9c140bb5f4c5cce93e7b57c82662c7b0106be23b5736ae16e9f7ad819993ddfa83bdbdea5684dfd6f9d27566efe8c25f67d5320fa04da7c44c86f02340110962
6
+ metadata.gz: c05f284cbae5b8cb5749b1aea82de9af83ba3267daa26bbe6d7a53b32f1c2fe0dd86827929a56eb66a3585cee2432ba0b4f6be62ca903471de884ed8f1e1e2ee
7
+ data.tar.gz: 0a5127285700d4401575ca46cc3ee4ccfd207c69f31db3abda15f7d0505b9fc635133a31eeec92af6fe74cc2a30a0059684a45693b17d420c23df7b73a280d0e
data/lib/rbdash.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'rbdash/version'
2
2
 
3
3
  module Rbdash
4
+ require 'rbdash/utils'
4
5
  require 'rbdash/cli'
5
6
  require 'rbdash/cli/pull'
6
7
  require 'rbdash/cli/push'
7
- require 'rbdash/cli/push_all'
8
8
  require 'rbdash/models/query'
9
9
  end
data/lib/rbdash/cli.rb CHANGED
@@ -3,6 +3,8 @@ require 'thor'
3
3
  module Rbdash
4
4
  class CLI < Thor
5
5
  include Thor::Actions
6
+ OPT_DRY_RUN = 'dry-run'.freeze
7
+ OPT_ALL = 'all'.freeze
6
8
 
7
9
  desc 'init', 'create a configuration file.'
8
10
  def init
@@ -13,26 +15,50 @@ module Rbdash
13
15
  end
14
16
  end
15
17
 
16
- desc 'pull', 'pulls existing configurations.'
17
- method_option 'dry-run'
18
- def pull
19
- CLI::Pull.new.run
18
+ desc 'pull <id1> <id2> ... [options]', 'pulls existing remote configurations.'
19
+ method_option OPT_DRY_RUN
20
+ method_option OPT_ALL
21
+ def pull(*ids)
22
+ if all? && !ids.empty?
23
+ puts "'CONFLICT: Cannot assign ids with --#{OPT_ALL} option.'"
24
+ return
25
+ end
26
+ if !all? && ids.empty?
27
+ puts "'You may pass either ids or --#{OPT_ALL} option.'"
28
+ return
29
+ end
30
+
31
+ CLI::Pull.new.run(*ids, command_options)
20
32
  end
21
33
 
22
- desc 'push <id1> <id2> ...', 'push configurations'
23
- method_option 'dry-run'
34
+ desc 'push <id1> <id2> ... [options]', 'push local configurations to remote.'
35
+ method_option OPT_DRY_RUN
36
+ method_option OPT_ALL
24
37
  def push(*ids)
25
- if options['dry-run']
26
- CLI::Push.new.dry_run(*ids)
38
+ if all? && !ids.empty?
39
+ puts "'CONFLICT: Cannot assign ids with --#{OPT_ALL} option.'"
40
+ return
41
+ end
42
+ if !all? && ids.empty?
43
+ puts "'You may pass either ids or --#{OPT_ALL} option.'"
27
44
  return
28
45
  end
29
- CLI::Push.new.run(*ids)
46
+
47
+ CLI::Push.new.run(*ids, command_options)
48
+ end
49
+
50
+ private
51
+
52
+ def command_options
53
+ { is_all: all?, is_dry: dry? }
54
+ end
55
+
56
+ def dry?
57
+ options[OPT_DRY_RUN]
30
58
  end
31
59
 
32
- desc 'push_all', 'push all configurations'
33
- method_option 'dry-run'
34
- def push_all
35
- CLI::PushAll.new.run
60
+ def all?
61
+ options[OPT_ALL]
36
62
  end
37
63
  end
38
64
  end
@@ -1,8 +1,39 @@
1
1
  module Rbdash
2
2
  class CLI::Pull
3
- def run
4
- queries = Rbdash::Models::Query.find_all
5
- queries.each(&:save)
3
+ def run(*ids, **options)
4
+ # find remote queries
5
+ queries = if options[:is_all]
6
+ Rbdash::Models::Query.find_all
7
+ else
8
+ ids.map do |id|
9
+ Rbdash::Models::Query.find(id)
10
+ end
11
+ end
12
+ # save
13
+ queries.each do |query|
14
+ show_diff(query)
15
+ next if options[:is_dry]
16
+
17
+ # update local states
18
+ query.save
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def show_diff(query)
25
+ id = query.id
26
+ local_state = Rbdash::Models::Query.load(id)
27
+ diff = Diffy::Diff.new(local_state.to_json, query.to_json, diff_opt).to_s(:color)
28
+
29
+ unless diff.chomp.empty?
30
+ puts "[changed] #{id}"
31
+ puts diff
32
+ end
33
+ end
34
+
35
+ def diff_opt
36
+ { include_diff_info: true, context: 2 }
6
37
  end
7
38
  end
8
39
  end
@@ -1,17 +1,14 @@
1
1
  require 'diffy'
2
2
  module Rbdash
3
3
  class CLI::Push
4
-
5
- def run(*ids)
4
+ def run(*ids, **options)
5
+ ids = Utils.find_local_ids if options[:is_all]
6
6
  ids.each do |id|
7
7
  show_diff(id)
8
- update(id)
9
- end
10
- end
8
+ next if options[:is_dry]
11
9
 
12
- def dry_run(*ids)
13
- ids.each do |id|
14
- show_diff(id)
10
+ # update remote resources
11
+ update(id)
15
12
  end
16
13
  end
17
14
 
@@ -24,7 +21,12 @@ module Rbdash
24
21
  def show_diff(id)
25
22
  remote_state = Rbdash::Models::Query.find(id)
26
23
  local_state = Rbdash::Models::Query.load(id)
27
- puts Diffy::Diff.new(remote_state.to_json, local_state.to_json, diff_opt).to_s(:color)
24
+ diff = Diffy::Diff.new(remote_state.to_json, local_state.to_json, diff_opt).to_s(:color)
25
+
26
+ unless diff.chomp.empty?
27
+ puts "[changed] #{id}"
28
+ puts diff
29
+ end
28
30
  end
29
31
 
30
32
  def diff_opt
@@ -3,10 +3,11 @@ require_relative '../request'
3
3
  module Rbdash
4
4
  module Models
5
5
  class BaseModel
6
- attr_accessor :body
6
+ attr_accessor :body, :id
7
7
 
8
- def initialize(body)
8
+ def initialize(body, id)
9
9
  @body = body
10
+ @id = id
10
11
  end
11
12
 
12
13
  def to_json
@@ -14,7 +15,6 @@ module Rbdash
14
15
  end
15
16
 
16
17
  def save
17
- id = @body['id']
18
18
  dirname = self.class.dirname
19
19
  Dir.mkdir(dirname) unless File.exist?(dirname)
20
20
  filename = "#{dirname}/query-#{id}.json"
@@ -37,7 +37,7 @@ module Rbdash
37
37
  end
38
38
  h = JSON.parse(response.body)
39
39
  body = h.select { |k, _| attributes.map(&:to_s).include?(k) }
40
- new(body)
40
+ new(body, id)
41
41
  end
42
42
 
43
43
  def find_all
@@ -52,7 +52,8 @@ module Rbdash
52
52
  results = h['results']
53
53
  all_results += results.map do |result|
54
54
  body = result.select { |k, _| attributes.map(&:to_s).include?(k) }
55
- new(body)
55
+ id = result['id']
56
+ new(body, id)
56
57
  end
57
58
 
58
59
  count = h['count']
@@ -76,10 +77,10 @@ module Rbdash
76
77
  end
77
78
 
78
79
  def load(id)
79
- file = Dir.glob("#{dirname}/query-#{id}.json").first
80
- raise StandardError, 'id not found' unless file
80
+ file = Utils.find_local_file(id)
81
+ return new(nil, id) unless file
81
82
  body = JSON.parse(File.read(file))
82
- new(body)
83
+ new(body, id)
83
84
  end
84
85
  end
85
86
  end
@@ -5,7 +5,6 @@ module Rbdash
5
5
  class Query < BaseModel
6
6
  def self.attributes
7
7
  [
8
- :id, # number
9
8
  :data_source_id, # number
10
9
  :query, # string
11
10
  :name, # string
@@ -0,0 +1,15 @@
1
+ module Rbdash
2
+ module Utils
3
+ class << self
4
+ def find_local_ids
5
+ Dir.glob('queries/*').map do |f|
6
+ f.match(/\d+/)[0]
7
+ end
8
+ end
9
+
10
+ def find_local_file(id)
11
+ Dir.glob("queries/query-#{id}.json").first
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Rbdash
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbdash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shotat
@@ -129,10 +129,10 @@ files:
129
129
  - lib/rbdash/cli.rb
130
130
  - lib/rbdash/cli/pull.rb
131
131
  - lib/rbdash/cli/push.rb
132
- - lib/rbdash/cli/push_all.rb
133
132
  - lib/rbdash/models/base_model.rb
134
133
  - lib/rbdash/models/query.rb
135
134
  - lib/rbdash/request.rb
135
+ - lib/rbdash/utils.rb
136
136
  - lib/rbdash/version.rb
137
137
  - local_install.sh
138
138
  - rbdash.gemspec
@@ -1,13 +0,0 @@
1
- module Rbdash
2
- class CLI::PushAll
3
- def run
4
- ids = Dir.glob('queries/*').map do |f|
5
- f.match(/\d+/)[0]
6
- end
7
- ids.each.map do |id|
8
- Rbdash::Models::Query.update(id)
9
- end
10
- end
11
- end
12
- end
13
-