cl-magic 0.3.0 → 0.3.2

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
  SHA256:
3
- metadata.gz: 504fca48eea3cb6eca83e8025a98e4f3187f19286b81134b42b53a0a639f2b9b
4
- data.tar.gz: 1269ba9642497c4e7541598d3ba180a09b2e5c033627ae9e60cbd545ba3f4e91
3
+ metadata.gz: a9f83478d5c0be3f555396d8274a2c4ed7e275130f8068236d8bc9a2775bc30e
4
+ data.tar.gz: d48e33ed2be7ccc8e83c36edf86ef622e0668cc911b9d50cc5e8c5fb6c039614
5
5
  SHA512:
6
- metadata.gz: b56850adb7d7182d6a72777236902b7b961493f3780c209675c268381c4ccfc78a339ea740f38ed9dd16c2a96f8c102efa24ea3527435699830219b7a5b9e8c9
7
- data.tar.gz: a83324b6a355286f9ad2cfaf7b034ca93b710f2706da1fb4e1b61722575a6477e42763d669b082bc72dc308fa5c78195cedaa64c755f9ef4d250f98b36e443b0
6
+ metadata.gz: 7849ce794132e7111a17091b51b27e1b991e74f5ab2666f831152199da79baa8c683d005ceb9018a8fb48d9d7c5530e8e4c603b38272f7df32316dfc744c19b7
7
+ data.tar.gz: faab04d8e3c91b302d35b5e89f558326eabf1aabd6648d22d08bc9c38d66bba3aab2b01cb7a50b13c7f20a8719cb48b894078147b6b0dd7c09eeab5ac5cb1378
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cl-magic (0.3.0)
4
+ cl-magic (0.3.2)
5
5
  optparse-subcommand
6
6
  pastel
7
7
  tty-command
data/lib/cl/magic/cl-auth CHANGED
@@ -62,14 +62,6 @@ github_cli_banner = <<DOC
62
62
 
63
63
  DOC
64
64
 
65
- aws_okta_banner = <<DOC
66
-
67
- Authenticate with https://github.com/segmentio/aws-okta
68
-
69
- Usage: #{@cl_cmd_name} aws-okta [options]
70
-
71
- DOC
72
-
73
65
  global = OptionParser.new do |g|
74
66
  g.banner = global_banner
75
67
  add_help_and_verbose(g)
@@ -84,11 +76,6 @@ global = OptionParser.new do |g|
84
76
  options[:action] = :github_cli
85
77
  end
86
78
 
87
- g.subcommand 'aws-okta' do |s|
88
- s.banner = aws_okta_banner
89
- options[:action] = :aws_okta
90
- end
91
-
92
79
  end
93
80
 
94
81
  #
data/lib/cl/magic/cl-dk CHANGED
@@ -2,10 +2,12 @@
2
2
  require 'yaml'
3
3
  require 'json'
4
4
  require 'tty-command'
5
+ require 'tty-prompt'
5
6
  require 'tempfile'
6
7
 
7
8
  require 'cl/magic/common/common_options.rb'
8
9
  require 'cl/magic/common/logging.rb'
10
+ require 'cl/magic/common/parse_and_pick.rb'
9
11
 
10
12
  @logger = get_logger()
11
13
 
@@ -18,23 +20,50 @@ def get_base_compose_hash()
18
20
  return YAML.load(`#{cmd}`)
19
21
  end
20
22
 
23
+ def get_save_parts_filepath()
24
+ return File.join(@working_dir, ".cl-dk-parts.yml")
25
+ end
26
+
27
+ # world
28
+
21
29
  def get_repo_basename()
22
30
  command = "cd #{@working_dir} && basename $(git remote get-url origin 2> /dev/null) .git"
23
31
  return TTY::Command.new(:printer => :null).run(command).out.gsub('.git', '').strip.chomp
24
32
  end
25
33
 
26
- def get_dk_world_project_path()
27
- repo_basename = get_repo_basename()
28
- return "dk-world/#{repo_basename}" if repo_basename
29
- return nil
34
+ def get_world_settings_filepath()
35
+ return File.join(".cl-dk-world.yml")
30
36
  end
31
37
 
32
- def get_save_parts_filepath()
33
- return File.join(@working_dir, ".cl-dk-parts.yml")
38
+ def get_world_settings_hash()
39
+ filepath = get_world_settings_filepath()
40
+ return File.exist?(filepath) ? YAML.load_file(filepath) : {}
41
+ end
42
+
43
+ def get_world_path_from_settings()
44
+ world_settings = get_world_settings_hash()
45
+ if world_settings.key?(:world_path) and world_settings.key?(:context)
46
+ return File.join(world_settings[:world_path], world_settings[:context])
47
+ end
48
+ return ""
49
+ end
50
+
51
+ def save_world_settings(world_settings_hash)
52
+ filepath = get_world_settings_filepath()
53
+ tempfile = File.new(filepath, 'w')
54
+ tempfile.write(world_settings_hash.to_yaml)
55
+ tempfile.close
56
+ end
57
+
58
+ def get_world_project_path()
59
+ repo_basename = get_repo_basename()
60
+ world_path = get_world_path_from_settings()
61
+ return File.join(world_path, repo_basename) if world_path and repo_basename
62
+ return nil
34
63
  end
35
64
 
36
65
  #
37
- # Print Help Methods
66
+ # Help Methods
38
67
  #
39
68
 
40
69
  def print_dk_help_line(key, help)
@@ -69,26 +98,27 @@ def print_dk_help(dk_parts_hash, dk_make_hash, args)
69
98
  puts "Run docker compose while munging yamls in sophisticated ways."
70
99
  puts ""
71
100
  if get_repo_basename
72
- puts "PROJ INFO:"
101
+ puts "PROJ INFO"
73
102
  puts " - Repo basename: #{get_repo_basename}"
74
- puts " - World filepath: #{File.join(@working_dir, get_dk_world_project_path())}"
103
+ puts " - World filepath: #{get_world_project_path()}"
75
104
  puts ""
76
105
  end
77
- puts "PROJ PARTS:"
106
+ puts "PROJ PARTS"
78
107
  dk_parts_hash.keys.each do |key|
79
108
  print_dk_help_line(key, dk_parts_hash[key].fetch('help'))
80
109
  end
81
110
  puts ""
82
111
  puts "YML TOKENS"
83
- puts " - <dk-remove>"
84
- puts " - <dk-replace>"
85
- puts " - <dk-project-path>"
86
- puts " - <dk-world-path>"
87
- puts " - <dk-working-path>"
112
+ puts " - <dk-remove> removes section of yaml"
113
+ puts " - <dk-replace> replaces values in a yaml array"
114
+ puts " - <dk-world-path> absolute filepath to world directory"
115
+ puts " - <dk-project-path> absolute filepath to world/project directory"
116
+ puts " - <dk-working-path> absolute filepath to location dk command was run from"
88
117
  puts ""
89
- puts "ADDITIONAL TURNKEY COMMANDS:"
90
- puts " - dk make"
91
- puts " - dk save-parts"
118
+ puts "ADDITIONAL TURNKEY COMMANDS"
119
+ puts " - dk set-world sets the location of the world directory"
120
+ puts " - dk make turnkey commands for a project"
121
+ puts " - dk save-parts save parts so they are automatically applied to commands"
92
122
  puts ""
93
123
  puts "-------------------------"
94
124
  end
@@ -113,16 +143,19 @@ def print_make_help(dk_parts_hash, dk_make_hash)
113
143
  puts ""
114
144
  end
115
145
 
116
- def print_save_parts_help(dk_parts_hash)
146
+ def print_set_world_help()
117
147
  puts ""
118
- puts "Usage: dk [DK_PARTS] save-parts"
148
+ puts "Usage: dk set-world PATH"
119
149
  puts ""
120
- puts "Save parts in project and apply every time"
150
+ puts "Set the folder that contains dk-world configuration"
121
151
  puts ""
122
- puts "Parts:"
123
- dk_parts_hash.keys.each do |key|
124
- print_dk_help_line(key, dk_parts_hash[key].fetch('help'))
125
- end
152
+ end
153
+
154
+ def print_set_parts_help()
155
+ puts ""
156
+ puts "Usage: dk [DK_PARTS] set-parts"
157
+ puts ""
158
+ puts "Set parts for a project and apply to every 'dk' command"
126
159
  puts ""
127
160
  end
128
161
 
@@ -169,7 +202,7 @@ def dk_merge_and_remove(compose_hash, yml_hash)
169
202
  end
170
203
 
171
204
  def merge_world_files(compose_hash, show_help=false)
172
- dk_proj_path = get_dk_world_project_path()
205
+ dk_proj_path = get_world_project_path()
173
206
  if dk_proj_path
174
207
  print_dk_help_line("dk-project-path", "#{dk_proj_path}") if show_help
175
208
 
@@ -178,8 +211,8 @@ def merge_world_files(compose_hash, show_help=false)
178
211
 
179
212
  # read file and replace
180
213
  contents = File.read(filepath)
181
- contents.gsub!('<dk-world-path>', File.join(Dir.pwd, "dk-world"))
182
- contents.gsub!('<dk-project-path>', File.join(Dir.pwd, dk_proj_path))
214
+ contents.gsub!('<dk-world-path>', get_world_path_from_settings())
215
+ contents.gsub!('<dk-project-path>', dk_proj_path)
183
216
  contents.gsub!('<dk-working-path>', @working_dir)
184
217
 
185
218
  # yml merge
@@ -277,22 +310,70 @@ def run_dk(compose_args)
277
310
  end
278
311
 
279
312
  #
280
- # Run: SAVE PARTS
313
+ # Run: SET WORLD
281
314
  #
282
315
 
283
- def run_dk_save_parts(compose_args, dk_parts_hash, selected_part_keys)
316
+ def run_dk_set_world()
317
+ world_settings = get_world_settings_hash()
318
+
319
+ # world path
320
+ world_path = ARGV[1]
321
+ if world_path.nil?
322
+ print_set_world_help
323
+ exit
324
+ else
325
+ world_path = File.expand_path(File.join(@working_dir, world_path))
326
+ end
327
+
328
+ if TTY::Prompt.new.yes?("Set world to: #{world_path}?")
329
+ world_settings[:world_path] = world_path
330
+ save_world_settings(world_settings)
331
+ run_dk_set_context()
332
+ else
333
+ exit
334
+ end
335
+
336
+ end
337
+
338
+ def run_dk_set_context()
339
+ world_settings = get_world_settings_hash()
340
+ unless world_settings[:world_path] and File.directory?(world_settings[:world_path])
341
+ @logger.error "no world path set."
342
+ exit 1
343
+ end
344
+
345
+ # read folder from world path (aka. context)
346
+ all_dir = Dir.chdir(world_settings[:world_path]) do
347
+ Dir.glob('*').select { |f| File.directory? f and f != "common" }
348
+ end
349
+ selected_dir = pick_single_result(all_dir.collect {|f| [f]}, "select context").first
350
+ world_settings[:context] = selected_dir
351
+ save_world_settings(world_settings)
352
+
353
+ @logger.puts "world set!"
354
+ exit
355
+ end
356
+
357
+ #
358
+ # Run: SET PARTS
359
+ #
360
+
361
+ def run_dk_set_parts()
362
+ parts = ARGV[1..]
284
363
  filepath = get_save_parts_filepath()
285
- if selected_part_keys.any?
364
+ if parts.any?
286
365
  tempfile = File.new(filepath, 'w')
287
- tempfile.write(selected_part_keys.to_yaml) # write it to the tempfile
366
+ tempfile.write(ARGV[1..].to_yaml) # write it to the tempfile
367
+ tempfile.close
288
368
  else
289
369
  if File.exist?(filepath)
290
370
  File.delete(filepath)
291
371
  @logger.info "parts cleared"
292
372
  else
293
- print_save_parts_help(dk_parts_hash)
373
+ print_set_parts_help()
294
374
  end
295
375
  end
376
+ exit
296
377
  end
297
378
 
298
379
  #
@@ -374,29 +455,38 @@ end
374
455
 
375
456
  @working_dir = ENV['CL_WORKING_DIR'] # passed through cl-magic to here
376
457
 
377
- # get compose settings
378
- compose_hash = get_base_compose_hash()
379
- if compose_hash
380
-
381
- # world
382
- compose_hash = merge_world_files(compose_hash, show_help=ARGV.include?("--help"))
383
-
384
- # x-dk fragments
385
- dk_parts_hash = compose_hash['x-dk-parts'] ? compose_hash.delete('x-dk-parts') : {}
386
- dk_make_hash = compose_hash['x-dk-make'] ? compose_hash.delete('x-dk-make') : {}
387
-
388
- # parts
389
- saved_part_keys = get_saved_parts(dk_parts_hash)
390
- compose_args, selected_part_keys = merge_parts_save_and_prep_args(compose_hash, dk_parts_hash, dk_make_hash, saved_part_keys)
391
-
392
- # sub-command
393
- case compose_args[2]
394
- when "make"
395
- all_part_keys = selected_part_keys + saved_part_keys
396
- run_dk_make(compose_args, dk_make_hash, dk_parts_hash, all_part_keys)
397
- when "save-parts"
398
- run_dk_save_parts(compose_args, dk_parts_hash, selected_part_keys)
399
- else
400
- run_dk(compose_args)
458
+ # SET commands first
459
+ case ARGV.first
460
+ when "set-world"
461
+ run_dk_set_world()
462
+ when "set-context"
463
+ run_dk_set_context()
464
+ when "set-parts"
465
+ run_dk_set_parts()
466
+ else
467
+
468
+ # get compose settings
469
+ compose_hash = get_base_compose_hash()
470
+ if compose_hash
471
+
472
+ # world
473
+ compose_hash = merge_world_files(compose_hash, show_help=ARGV.include?("--help"))
474
+
475
+ # x-dk fragments
476
+ dk_parts_hash = compose_hash['x-dk-parts'] ? compose_hash.delete('x-dk-parts') : {}
477
+ dk_make_hash = compose_hash['x-dk-make'] ? compose_hash.delete('x-dk-make') : {}
478
+
479
+ # parts
480
+ saved_part_keys = get_saved_parts(dk_parts_hash)
481
+ compose_args, selected_part_keys = merge_parts_save_and_prep_args(compose_hash, dk_parts_hash, dk_make_hash, saved_part_keys)
482
+
483
+ # sub-command
484
+ case compose_args[2]
485
+ when "make"
486
+ all_part_keys = selected_part_keys + saved_part_keys
487
+ run_dk_make(compose_args, dk_make_hash, dk_parts_hash, all_part_keys)
488
+ else
489
+ run_dk(compose_args)
490
+ end
401
491
  end
402
492
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Cl
4
4
  module Magic
5
- VERSION = "0.3.0"
5
+ VERSION = "0.3.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cl-magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Najd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-13 00:00:00.000000000 Z
11
+ date: 2023-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake