squared 0.8.8 → 0.9.0.pre1

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.
@@ -128,6 +128,7 @@ module Squared
128
128
  'image' => %i[ls rm pull push tag save inspect history].freeze,
129
129
  'container' => %i[attach commit create diff exec inspect kill logs pause restart rm run start stats stop top
130
130
  unpause update].freeze,
131
+ 'run' => %i[rmi rmd mount volume entrypoint].freeze,
131
132
  'network' => %i[connect disconnect create rm].freeze,
132
133
  'ls' => nil
133
134
  })
@@ -181,7 +182,7 @@ module Squared
181
182
  if flags.nil?
182
183
  case action
183
184
  when 'ls'
184
- format_desc(action, nil, 'a/ll?,s/tandard?,range*', before: VAL_DOCKER[:ls])
185
+ format_desc(action, nil, VAL_DOCKER[:ls].keys, after: 'a/ll?,s/tandard?,range*', arg: nil)
185
186
  task action, [:command] do |_, args|
186
187
  command = param_guard(action, 'command', args: args, key: :command)
187
188
  args = args.extras
@@ -303,13 +304,22 @@ module Squared
303
304
  end
304
305
  end
305
306
  else
306
- format_desc action, flag, "opts*,id/name#{case flag
307
- when :stats then '*'
308
- when :logs, :diff, :attach, :top then nil
309
- else '+'
310
- end}"
307
+ single = %i[logs diff attach top].freeze
308
+ format_desc action, flag, "opts*,id/name#{if flag == :stats
309
+ '*'
310
+ elsif !single.include?(flag)
311
+ '+'
312
+ end}|:"
311
313
  task flag do |_, args|
312
- container flag, args.to_a
314
+ args = args.to_a
315
+ if choice!(args, first: false)
316
+ args.concat(Array(choice_command(:container, multiple: !single.include?(flag))))
317
+ if flag == :rm
318
+ stop = filter_ps(:stop, column: 'ID').select { |id| args.include?(id) }
319
+ container :stop, stop if !stop.empty? && confirm_basic('Stop?', stop.join(', '))
320
+ end
321
+ end
322
+ container flag, args
313
323
  end
314
324
  end
315
325
  when 'image'
@@ -358,18 +368,47 @@ module Squared
358
368
  end
359
369
  end
360
370
  end
371
+ when 'run'
372
+ format_desc action, flag, 'id|:,opts*'
373
+ task flag, [:id] do |_, args|
374
+ id = if args.id == ':'
375
+ choice_command(:image, multiple: false)
376
+ else
377
+ param_guard(action, flag, args: args, key: :id)
378
+ end
379
+ args = case flag
380
+ when :mount
381
+ mount = []
382
+ loop do
383
+ s = readline("Enter mount ##{mount.size.succ}", force: false)
384
+ break if s.empty?
385
+
386
+ mount << (s.include?('type=') ? s : "type=bind,#{s}")
387
+ end
388
+ mount.map { |val| quote_option('mount', val, double: true) }
389
+ when :volume
390
+ choice_command(:volume, multiple: true, series: true).map do |val|
391
+ basic_option 'volume', val
392
+ end
393
+ when :entrypoint
394
+ ["entrypoint=#{shell_quote(readline('Entrypoint', force: true), double: true)}"]
395
+ when :rmi
396
+ %w[rm i t]
397
+ when :rmd
398
+ %w[rm d]
399
+ end + args.extras
400
+ container(:run, args.uniq, id: id)
401
+ end
361
402
  when 'network'
362
403
  format_desc action, flag, 'name,opts*'
363
404
  task flag, [:name] do |_, args|
364
- name = if flag == :create || flag == :rm
365
- param_guard(action, flag, args: args, key: :name)
366
- else
367
- args.name
368
- end
405
+ name = flag == :create ? param_guard(action, flag, args: args, key: :name) : args.name
369
406
  if name
370
407
  network(flag, args.extras, name: name)
408
+ elsif flag == :rm
409
+ choice_command :'network:rm'
371
410
  else
372
- choice_command flag
411
+ choice_command(flag, id: choice_command(:container, force: false), series: true)
373
412
  end
374
413
  end
375
414
  end
@@ -501,7 +540,7 @@ module Squared
501
540
  if flag == :publish
502
541
  id ||= option('tag', ignore: false) || op.shift || tagmain
503
542
  registry ||= option('registry') || op.shift || @oci
504
- emptyargs op, flag
543
+ op.clear(strict: true)
505
544
  op << shell_quote(tagjoin(registry, id))
506
545
  return unless confirm_command(op.to_s, title: from, target: id)
507
546
  else
@@ -516,10 +555,10 @@ module Squared
516
555
  if multiple
517
556
  op.concat(service) if service
518
557
  op.append(delim: true, escape: true, strip: ':')
519
- else
520
- raise ArgumentError, message('no service was selected', hint: flag) unless service
521
-
558
+ elsif service
522
559
  append_command(flag, service, op.extras, prompt: '::')
560
+ else
561
+ raise ArgumentError, message('no service was selected', hint: flag)
523
562
  end
524
563
  end
525
564
  end
@@ -668,7 +707,7 @@ module Squared
668
707
  args = if name
669
708
  index += 1
670
709
  opts.dup << "name=#{index == 0 ? name : "#{name}-#{index}"}"
671
- elsif choice!(opts, only: true)
710
+ elsif choice!(opts, empty: true, only: true)
672
711
  OptionPartition.strip(readline('Enter arguments', force: false))
673
712
  else
674
713
  opts
@@ -720,9 +759,9 @@ module Squared
720
759
  when :push
721
760
  id ||= option('tag', ignore: false) || op.shift || tagmain
722
761
  registry ||= option('registry') || op.shift || self.registry
723
- emptyargs op, flag
724
762
  raise ArgumentError, message('username/registry not specified', hint: flag) unless registry
725
763
 
764
+ op.clear(strict: true)
726
765
  uri = shell_quote tagjoin(registry, id)
727
766
  op << uri
728
767
  img = docker_output 'image tag', id, uri
@@ -745,7 +784,7 @@ module Squared
745
784
  success?(run(sync: sync, exception: exception, banner: banner, from: from), status)
746
785
  end
747
786
 
748
- def network(flag, opts = [], target: nil, name: target)
787
+ def network(flag, opts = [], name: nil)
749
788
  cmd, opts = docker_session('network', flag, opts: opts)
750
789
  op = OptionPartition.new(opts, OPT_DOCKER[:network].fetch(flag, []), cmd, **dockerinit)
751
790
  .clear
@@ -891,7 +930,7 @@ module Squared
891
930
  target
892
931
  end
893
932
 
894
- def filter_ps(flag, meth = nil, column: nil, from: meth || :'container:ps')
933
+ def filter_ps(flag, column: nil, from: :'container:ps')
895
934
  no = false
896
935
  status = case flag.to_sym
897
936
  when :start
@@ -927,7 +966,7 @@ module Squared
927
966
  pwd_set(from: from) do
928
967
  index = 1
929
968
  all = option('all', prefix: 'docker')
930
- y = from.to_s.end_with?(':rm') && option('y', prefix: 'docker')
969
+ y = from == :'image:rm' && option('y', prefix: 'docker')
931
970
  filter = env('DOCKER_FILTER', filter).to_s
932
971
  pat = if OptionPartition.pattern?(filter)
933
972
  Regexp.new(filter)
@@ -974,7 +1013,7 @@ module Squared
974
1013
  end
975
1014
  pad = cols.map(&:size).max
976
1015
  cols.each { |key| puts "#{g + f} #{key.rjust(pad)}: #{Array(data[key]).join(', ')}" }
977
- w = 9 + flag.to_s.size + ARG[:TAB] + ee.size
1016
+ w = 9 + flag.to_s.size + 4 + ee.size
978
1017
  puts g + sub_style(ARG[:BORDER][6] + (ARG[:BORDER][1] * w), theme[:inline])
979
1018
  index += 1
980
1019
  next unless confirm("#{h + b}?", no ? 'N' : 'Y')
@@ -1009,18 +1048,22 @@ module Squared
1009
1048
  confirm "#{s} #{sub_style(target, theme[:subject])}#{" as #{sub_style(as, theme[:inline])}" if as}?", 'N'
1010
1049
  end
1011
1050
 
1012
- def choice_command(flag, *action)
1051
+ def choice_command(flag, *action, id: nil, multiple: nil, series: false, force: true)
1013
1052
  msg, cmd = case flag
1014
- when :exec
1053
+ when :exec, :container
1015
1054
  ['Choose a container', 'ps -a']
1016
1055
  when :bake
1017
1056
  ['Choose a target', 'buildx bake --list=type=targets']
1018
- when :connect, :disconnect
1057
+ when :connect, :disconnect, :'network:rm'
1019
1058
  ['Choose a network', 'network ls']
1020
1059
  when :service
1021
1060
  ['Choose a service',
1022
1061
  'compose ps -a ' \
1023
1062
  '--format="table {{.Service}}\t{{.Name}}\t{{.Image}}\t{{.Command}}\t{{.Status}}\t{{.Ports}}"']
1063
+ when :volume
1064
+ ['Choose a volume',
1065
+ 'volume ls ' \
1066
+ '--format="table {{.Name}}\t{{.Driver}}\t{{.Mountpoint}}"']
1024
1067
  else
1025
1068
  ['Choose an image',
1026
1069
  'images -a ' \
@@ -1031,28 +1074,40 @@ module Squared
1031
1074
  puts log_message('none found', subject: name, hint: "docker #{cmd.split(' ', 3)[0, 2].join(' ')}")
1032
1075
  return
1033
1076
  end
1034
- puts " # #{lines.shift}"
1035
- multiple = false
1077
+ parse = lambda do |list|
1078
+ ret = Array(list).map { |val| val.split(/\s+/, 2).first }.reject(&:empty?)
1079
+ return ret.first if multiple == false
1080
+
1081
+ ret unless ret.empty?
1082
+ end
1083
+ puts "#{"\n" if id && !printfirst?} # #{lines.shift}"
1036
1084
  ctx = flag.to_s
1037
1085
  case flag
1038
1086
  when :run, :exec
1039
1087
  values = [['Options', flag == :run], ['Arguments', flag == :exec]]
1040
1088
  when :rm, :bake
1041
1089
  values = ['Options']
1042
- multiple = true
1090
+ multiple = true if multiple.nil?
1043
1091
  ctx = flag == :rm ? 'image rm' : "buildx bake -f #{shell_quote(dockerfile)}"
1044
1092
  when :save
1045
1093
  values = [['Output', true], 'Platform']
1046
- multiple = true
1094
+ multiple = true if multiple.nil?
1047
1095
  when :service
1048
- values = []
1049
- multiple = true
1096
+ values = ['Options']
1097
+ multiple = true if multiple.nil?
1050
1098
  ctx = 'compose'
1051
1099
  when :connect, :disconnect
1052
- values = ['Options', ['Container', true]]
1100
+ values = id ? ['Options'] : ['Options', ['Container', true]]
1053
1101
  ctx = "network #{flag}"
1102
+ when :'network:rm'
1103
+ values = ['Options']
1104
+ multiple = true if multiple.nil?
1105
+ ctx = 'network rm'
1106
+ when :container, :image, :volume
1107
+ return parse.call(choice_index(msg, lines, multiple: multiple, series: series, force: force,
1108
+ attempts: force ? 3 : 1))
1054
1109
  end
1055
- out, opts, args = choice_index(msg, lines, multiple: multiple, values: values)
1110
+ out, opts, args = choice_index(msg, lines, values: values, multiple: multiple, series: series)
1056
1111
  cmd = docker_output(ctx, *action)
1057
1112
  case flag
1058
1113
  when :tag
@@ -1067,8 +1122,10 @@ module Squared
1067
1122
  else
1068
1123
  cmd << opts << '--'
1069
1124
  end
1070
- cmd.merge(Array(out).map { |val| val.split(/\s+/, 2).first })
1071
- success?(run(cmd << args, { 'NO_COLOR' => 'true' }), ctx.start_with?('network', 'tag', 'save'))
1125
+ cmd.merge(parse.call(out))
1126
+ cmd << args
1127
+ cmd.merge(Array(id)) if id
1128
+ success?(run(cmd, { 'NO_COLOR' => 'true' }), ctx.start_with?('network', 'tag', 'save'))
1072
1129
  end
1073
1130
 
1074
1131
  def filetype(val = dockerfile)
@@ -1108,14 +1165,14 @@ module Squared
1108
1165
  s = val.join(':')
1109
1166
  return s unless s.size > max
1110
1167
  end
1111
- ret[0...max]
1168
+ ret[...max]
1112
1169
  end
1113
1170
 
1114
1171
  def dnsname(val, max: 63, char: nil)
1115
1172
  ret = val.sub(/^[^a-z]+/i, '')
1116
1173
  .sub(/[^a-z\d]+$/i, '')
1117
1174
  .gsub(/[^a-z\d#{char}]+/i, '-')
1118
- .gsub(/-{2,}/, '-')[0...max]
1175
+ .gsub(/-{2,}/, '-')[...max]
1119
1176
  char ? ret : ret.downcase
1120
1177
  end
1121
1178
 
@@ -1123,10 +1180,6 @@ module Squared
1123
1180
  dnsname(val, max: 128, char: '.').gsub(/\.{2,}/, '.')
1124
1181
  end
1125
1182
 
1126
- def charname(val)
1127
- val.gsub(/[^\w.-]+/, '_')
1128
- end
1129
-
1130
1183
  def tagmain
1131
1184
  tag.is_a?(Array) ? tag.first : tag
1132
1185
  end
@@ -1135,10 +1188,6 @@ module Squared
1135
1188
  { project: self, strict: strict?, explicit: '=', bool: %w[1 0 true false] }
1136
1189
  end
1137
1190
 
1138
- def emptyargs(list, hint = nil)
1139
- raise ArgumentError, message('unrecognized args', list.join(', '), hint: hint) unless list.empty?
1140
- end
1141
-
1142
1191
  def anypath?(*args)
1143
1192
  args.any? { |val| basepath!(val) }
1144
1193
  end