module DkComposerTasks

Public Instance Methods

ask_and_select(h, *cols) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 192
def ask_and_select(h, *cols)
  cols = %i[name shortdesc] if cols.empty?
  print_table h.to_table(*cols, with_index: true)
  say('which app you want to up?'.blue)
  answer = ask('(q=QUIT;n=)', limited_to: ['q', *('1'..h.count.to_s).to_a])
  return nil if answer == 'q'
  h.to_a[answer.to_i - 1].last
end
auto_load(loadpaths = []) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 201
def auto_load(loadpaths = [])
  loadpaths ||= []
  loadpaths.unshift(*(ENV['DK_LOAD_PATH'] || '').split(':')).uniq!
  loadpaths.push '.'
  loadpaths.each do |path|
    $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
    dk_pattern = "#{path}/*.dk.rb"
    Dir[dk_pattern].each { |dkfile| require dkfile }
  end
end
beautify_file(file) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 187
def beautify_file(file)
  result = run('which rubocop', capture: true)
  run("rubocop -a #{file}", capture: true) unless result.empty?
end
create(name) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 160
def create(name)
  auto_load(options[:loadpaths])
  @opt = { name: name }
  answer = ask('Create stack from exist stack? yes=from exists, no=create new stack,q=quit', limited_to: %w[q y n])
  return if answer == 'q'
  target_file = "#{name}.dk.rb"
  if answer == 'y'
    ret = DkComposer::STACK
    print_table ret.to_table(:name, :shortdesc, :longdesc)
    answer = ask('input the stack name', limited_to: ret.keys.map(&:to_s))
    stack = ret[answer.to_sym]
    stack.build_path = '.'

    create_file(target_file, stack.to_s)
  end
  if answer == 'n'
    ret = DkComposer::SERVICE
    print_table ret.to_table(:name, :shortdesc, :longdesc)
    answers = multi_ask('Add a service into the app,input the service name', limited_to: ret.keys.map(&:to_s).unshift(''))
    @opt[:services] = answers.uniq
    template("#{self.class.source_root}/templates/stack.dk.rb", target_file)
  end
  beautify_file(target_file)
end
down(*params) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 136
def down(*params)
  name, params = params
  if name && name.start_with?('-')
    params ||= []
    params.unshift name
    name = nil
  end
  auto_load(options[:loadpaths])
  ret = DkComposer::STACK.select { |_k, v| /#{name}/ =~ v.name.to_s }
  return say('There is no app avaliable'.red) if ret.empty?
  app = if ret.count > 1
          ask_and_select(ret)
        else
          ret.to_a.first.last
        end
  return unless app
  app.stop *params
end
image(*params) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 81
def image(*params)
  name, action, = params
  auto_load(options[:loadpaths])
  ret = DkComposer::IMAGE.select { |k, v| /#{name}/ =~ k.to_s && v && /:latest$/ !~ k.to_s }
  return say('There is no image avaliable'.red) if ret.empty?
  img = if ret.count > 1
          ask_and_select(ret, :name, :tag, :from, :use, :repos, :shortdesc)
        else
          print_table ret.to_table(:name, :tag, :from, :use, :repos, :shortdesc)
          ret.to_a.first.last
        end
  return unless img
  actions = %w[pull build push]
  action = ask('pls input a correct action to the image', limited_to: actions.unshift('q')) if action.nil? || !actions.include?(action.to_s)
  return if action == 'q'
  say(img.send(action.to_sym))
end
lsa(keyword = nil) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 18
def lsa(keyword = nil)
  auto_load(options[:loadpaths])
  ret = DkComposer::STACK.select { |_k, v| /#{keyword}/ =~ v.name.to_s }

  if options[:source]
    ret.each do |k, v|
      puts ["#-------------stack:#{k}--------------",
            v.dump,
            ''].join("\n")
    end

    return ret
  end

  if options[:compose]
    ret.each do |k, v|
      puts ["#-------------stack:#{k}--------------",
            v.to_yaml,
            ''].join("\n")
    end

    return ret
  end
  print_table ret.to_table(:name, :shortdesc)
  ret
end
lsi(keyword = nil) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 56
def lsi(keyword = nil)
  auto_load(options[:loadpaths])
  ret = DkComposer::IMAGE.select { |k, v| /#{keyword}/ =~ k.to_s && v && /:latest$/ !~ k.to_s }
  if options[:dockerfile]

    ret.each do |k, v|
      puts ["#-------------image:#{k}--------------",
            v.to_docker_file,
            ''].join("\n")
    end
  else
    print_table ret.to_table(:name, :tag, :from, :use, :repos, :shortdesc)
  end
  ret
end
up(*params) click to toggle source
# File lib/concerns/dkcomposer_task.rb, line 108
def up(*params)
  name, params = params
  if name && name.start_with?('-')
    params ||= []
    params.unshift name
    name = nil
  end
  auto_load(options[:loadpaths])
  ret = DkComposer::STACK.select { |_k, v| /#{name}/ =~ v.name.to_s }
  return say('There is no app avaliable'.red) if ret.empty?
  app = if ret.count > 1
          ask_and_select(ret)
        else
          ret.to_a.first.last
        end
  return unless app
  app.run *params
end