#!/usr/bin/env ruby # frozen_string_literal: true require 'rubygems' require 'optparse' require 'yaml' require 'net/http' require 'json' require 'net_http_unix' require 'securerandom' require 'erb' require 'uri' require 'fileutils' require 'mkit/client/command_parser' require 'mkit/client/mkitd_client' class InvalidConfigurationException < Exception attr_reader :command def initialize(cause, command = nil) super(cause) @command = command end end class MKItClient def initialize @root = File.expand_path('..', __dir__) @config_dir = "#{ENV['HOME']}/.mkit" @profile_file = "#{@config_dir}/current" @config_file = "#{@config_dir}/mkitc_config.yml" @parser = CommandParser.new create_default_config end # # client config # def create_default_config unless File.exist?(@config_dir) puts "Creating config directory on '#{@config_dir}'..." FileUtils.mkdir_p(@config_dir) end FileUtils.cp("#{@root}/config/mkitc_config.yml", @config_dir) unless File.exist?(@config_file) profile({ verb: 'set' }, { profile: 'local' }) unless File.exist?(@profile_file) end def read_configuration(init_call = false) current_profile = File.read(@profile_file) if current_profile.nil? || current_profile.empty? # force set default profile({ verb: 'set' }, { profile: 'local' }) current_profile = 'local' end cfg = YAML.load_file(@config_file) if cfg['mkit'].nil? || cfg['mkit'][current_profile.lstrip].nil? raise InvalidParametersException, "invalid configuration found on '~/.mkit' or profile not found" end @configuration = cfg['mkit'][current_profile.lstrip] if !init_call && cfg['my_id'].nil? raise InvalidParametersException.new("Please run ' mkit init' to initialize mkit client.", @parser.find_command('init')) end @my_id = cfg['my_id'] cfg end # # doIt # def doIt(argv) read_configuration request = @parser.parse(argv) result = execute(request) puts result rescue InvalidParametersException => e @parser.help(cause: e) end def execute(request) if respond_to? request[:cmd] send(request[:cmd], request[:request], request[:data]) else request(request[:request], request[:data]) end end def request(request, request_data) client = MKIt::MKItdClient.new(request, @configuration['server.uri'], @my_id) client.request(request, request_data) end # # commands # def init(request, request_data = nil) cfg = read_configuration(true) if cfg['my_id'].nil? my_id = SecureRandom.uuid.gsub('-','') cfg['my_id'] = my_id File.write(@config_file, cfg.to_yaml) puts "Please check if your api-key is on mkitd server allowed keys" else my_id = cfg['my_id'] end puts "Your api-key is #{my_id}" end def create(request, request_data = nil) unless File.file?(request_data[:file]) raise InvalidParametersException.new('File not found.', @parser.find_command('create')) end yaml = YAML.load_file(request_data[:file]) if yaml['service'].nil? raise InvalidParametersException.new('Invalid configuration file', @parser.find_command('create')) else request(request, request_data) end end def update(request, request_data = nil) unless File.file?(request_data[:file]) raise InvalidParametersException.new('File not found.', @parser.find_command('update')) end yaml = YAML.load_file(request_data[:file]) if yaml['service'].nil? raise InvalidParametersException.new('Invalid configuration file', @parser.find_command('update')) else id = yaml['service']['name'] request_data[:id] = id request(request, request_data) end end def profile(request, request_data = {}) cfg = YAML.load_file("#{@config_dir}/mkitc_config.yml") cmd = @parser.find_command('profile') if cfg['mkit'].nil? raise InvalidParametersException.new( "Invalid configuration on '~/.mkit'\nPlease fix or clean up for defaults apply", cmd ) end case request[:verb] when 'set' profile = request_data[:profile] if cfg['mkit'][profile.lstrip].nil? raise InvalidParametersException.new("Profile not found on '~/.mkit' configuration", cmd) end puts "Setting current profile to #{profile}." File.write(@profile_file, request_data[:profile]) '' when 'show' active = File.read("#{@config_dir}/current") cfg['mkit'].map do |k, _v| if k == active "*#{k}" else k end end.join(' ') else raise InvalidParametersException.new("Invalid 'profile' operation", cmd) end end end # # go # client = MKItClient.new client.doIt(ARGV.dup) # # if ARGV.any? # parse args # host, socket, config file # end