module Kontena::Plugin::Shell::Completer

Public Class Methods

complete(words) click to toggle source
# File lib/kontena/plugin/shell/completer.rb, line 87
def self.complete(words)
  while words.first == 'kontena' || words.first == 'complete'
    words.shift
  end
  helper = Helper.new
  completion = []

  case words[0]
  when NilClass, ''
    completion.concat %w(cloud logout grid app service stack vault certificate node master vpn registry container etcd external-registry whoami plugin version)
  when 'plugin'
    sub_commands = %w(list ls search install uninstall)
    if words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
    else
      completion.push sub_commands
    end
  when 'etcd'
    sub_commands = %w(get set mkdir mk list ls rm)
    if words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
    else
      completion.push sub_commands
    end
  when 'registry'
    sub_commands = %w(create remove rm)
    if words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
    else
      completion.push sub_commands
    end
  when 'grid'
    sub_commands = %w(add-user audit-log create current list user remove show use)
    if words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
      completion.push helper.grids
    else
      completion.push sub_commands
    end
  when 'node'
    sub_commands = %w(list show remove)
    if words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
      completion.push helper.nodes
    else
      completion.push sub_commands
    end
  when 'master'
    sub_commands = %w(list use user current remove rm config cfg login logout token join audit-log init-cloud)
    if words[1] && words[1] == 'use'
      completion.push helper.master_names
    elsif words[1] && words[1] == 'user'
      users_sub_commands = %(invite list role)
      completion.push users_sub_commands
    elsif words[1] && ['config', 'cfg'].include?(words[1])
      config_sub_commands = %(set get dump load import export unset)
      completion.push config_sub_commands
    elsif words[1] && words[1] == 'token'
      token_sub_commands = %(list ls rm remove show current create)
      completion.push token_sub_commands
    elsif words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
    else
      completion.push sub_commands
    end
  when 'cloud'
    sub_commands = %w(login logout master)
    if words[1] && words[1] == 'master'
      cloud_master_sub_commands = %(list ls remove rm add show update)
      completion.push cloud_master_sub_commands
    elsif words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
    else
      completion.push sub_commands
    end
  when 'service'
    sub_commands = %w(containers create delete deploy list logs restart
                    scale show start stats stop update monitor env
                    secret link unlink)
    if words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
      completion.push helper.services
    else
      completion.push sub_commands
    end
  when 'container'
    sub_commands = %w(exec inspect logs)
    if words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
      completion.push helper.containers
    else
      completion.push sub_commands
    end
  when 'vpn'
    completion.push %w(config create delete)
  when 'external-registry'
    completion.push %w(add list delete)
  when 'app'
    sub_commands = %w(init build config deploy start stop remove rm ps list
                      logs monitor show)
    if words[1]
      completion.push(sub_commands) unless sub_commands.include?(words[1])
      completion.push helper.yml_services
    else
      completion.push sub_commands
    end
  when 'stack'
    sub_commands = %w(build install upgrade deploy start stop remove rm ls list
                      logs monitor show registry)
    if words[1]
      if words[1] == 'registry'
        registry_sub_commands = %(push pull search show rm)
        completion.push registry_sub_commands
      elsif %w(install).include?(words[1])
          completion.push helper.yml_files
      elsif words[1] == 'upgrade' && words[3]
        completion.push helper.yml_files
      else
        completion.push(sub_commands) unless sub_commands.include?(words[1])
        completion.push helper.stacks
      end
    else
      completion.push sub_commands
    end
  else
  end
  completion.flatten.flat_map { |item| item.split(/\s+/) }
end