Sha256: 81c98b03fb103b31d5afeb2f574dda981119bc9d82a168310e04db04403217f9

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

require 'yaml'
require_relative 'common'

module Kontena::Cli::Apps
  class RemoveCommand < Clamp::Command
    include Kontena::Cli::Common
    include Common

    option ['-f', '--file'], 'FILE', 'Specify an alternate Kontena compose file', attribute_name: :filename, default: 'kontena.yml'
    option ['-p', '--project-name'], 'NAME', 'Specify an alternate project name (default: directory name)'

    parameter "[SERVICE] ...", "Remove services"

    attr_reader :services, :service_prefix

    def execute
      require_api_url
      require_token
      require_config_file(filename)

      @service_prefix = project_name || current_dir
      #Dir.chdir(File.dirname(filename))
      @services = load_services(filename, service_list, service_prefix)
      if services.size > 0
        remove_services(services)
      elsif !service_list.empty?
        puts "No such service: #{service_list.join(', ')}".colorize(:red)
      end

    end

    def remove_services(services)
      services.find_all {|service_name, options| options['links'] && options['links'].size > 0 }.each do |service_name, options|
        delete(service_name, options)
        services.delete(service_name)
      end
      services.each do |service_name, options|
        delete(service_name, options)
      end
    end

    def delete(name, options)
      unless deleted_services.include?(name)
        print "deleting #{name}"
        service = get_service(token, prefixed_name(name)) rescue nil

        if(!service.nil?)
          print "."
          delete_service(token, prefixed_name(name))
          print ". done"
          deleted_services << name
          puts ''
        else
          puts "No such service: #{name}".colorize(:red)
        end
      end
    end

    ##
    #
    # @return [Array]
    def deleted_services
      @deleted_services ||= []
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kontena-cli-0.9.3 lib/kontena/cli/apps/remove_command.rb
kontena-cli-0.9.2 lib/kontena/cli/apps/remove_command.rb
kontena-cli-0.9.1 lib/kontena/cli/apps/remove_command.rb
kontena-cli-0.9.0 lib/kontena/cli/apps/remove_command.rb