#!/usr/bin/env ruby # encoding: UTF-8 # resolve bin path, ignoring symlinks require 'pathname' require 'yaml' bin_file = Pathname.new(__FILE__).realpath # add self to libpath $:.unshift File.expand_path('../../lib', bin_file) require_relative '../client' require_relative '../cli/common' class Helper include Kontena::Cli::Common def client token = require_token super(token) end def grids client.get("grids")['grids'].map{|grid| grid['id']} rescue [] end def nodes client.get("grids/#{current_grid}/nodes")['nodes'].map{|node| node['name']} rescue [] end def services services = client.get("grids/#{current_grid}/services")['services'] results = [] results.push services.map{|s| s['name']} results rescue [] end def containers results = [] client.get("grids/#{current_grid}/services")['services'].each do |service| containers = client.get("services/#{service['id']}/containers")['containers'] results.push(containers.map{|c| c['name'] }) results.push(containers.map{|c| c['id'] }) end results rescue [] end def yml_services if File.exist?('kontena.yml') services = YAML.load(File.read('kontena.yml')) services.keys end rescue [] end def master_names config_file = File.expand_path('~/.kontena_client.json') if(File.exist?(config_file)) config = JSON.parse(File.read(config_file)) return config['servers'].map{|s| s['name']} end rescue [] end end helper = Helper.new words = ARGV words.delete_at(0) completion = [] completion.push %w(app deploy forgot-password master node grid invite service container vpn external-registry registry login logout whoami) if words.size < 2 if words.size > 0 case words[0] when 'grid' completion.clear sub_commands = %w(add-user audit-log create current list list-users remove remove-user 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' completion.clear sub_commands = %w(list show remove vagrant digitalocean azure aws) 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' completion.clear sub_commands = %w(list use vagrant digitalocean azure aws) if words[1] && words[1] == 'use' completion.push helper.master_names elsif words[1] completion.push(sub_commands) unless sub_commands.include?(words[1]) completion.push %w(create) else completion.push sub_commands end when 'service' completion.clear sub_commands = %w(containers create delete deploy list logs restart scale show start stats stop update monitor) 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' completion.clear sub_commands = %w(exec) 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.clear completion.push %w(config create delete) when 'external-registry' completion.clear completion.push %w(add list delete) when 'app' completion.clear sub_commands = %w(init build 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 end end puts completion