Sha256: f8834530160eb5f4ca4bd3acddad673e3e1bff8d5bba3a10c4d7c1ea1a9dcb5a

Contents?: true

Size: 1.96 KB

Versions: 38

Compression:

Stored size: 1.96 KB

Contents

class Mu
class Command
class API < Command
    attr_accessor :credentials
        
    def cmd_init argv
        FileUtils.rm credentials_file rescue nil
        API.client
        
        msg "You are now ready to use mu!"
        msg "Try mu help to learn more about the commands."
    end
    
    def client
        get_credentials
        Mu::Client.new(user, password, host)
    end
    
    def user
        get_credentials
        @credentials[0]
    end
    
    def password
        get_credentials
        @credentials[1]
    end
    
    def host
        ENV['MU_IP'] || ''
    end
    
    def credentials_file
    end
    
    def get_credentials
        return if @credentials
        unless @credentials = read_credentials
            @credentials = ask_for_credentials
            save_credentials
        end
        @credentials
    end
    
    def read_credentials
        File.exists?(credentials_file) and File.read(credentials_file).split("\n")        
    end
    
    def ask_for_credentials
        msg "Enter your credentials."
        print "User-ID: "
        user = ask
        print "API-Key: "
        apik = ask
        apik2 = Mu::Client.new(user, apik, host).login['api_key']
        if not apik2
            error "Authentication failed"
            exit 1
        end
        
        [ user, apik2 ]
    end
    
    def save_credentials
        write_credentials
    end
    
    def write_credentials
        FileUtils.mkdir_p(File.dirname(credentials_file))
        File.open(credentials_file, 'w') do |f|
          f.puts self.credentials
        end
        set_credentials_permissions
    end
    
    def set_credentials_permissions
        FileUtils.chmod 0700, File.dirname(credentials_file)
        FileUtils.chmod 0600, credentials_file        
    end
    
    def self.instance
        @instance ||= API.new
    end
    
    def self.client
        self.instance.client
    end
    
    private
    def initialize
    end
end
Api = API
end # Command
end # Mu

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
mu-5.7.41 lib/mu/command/api.rb
mu-5.7.40 lib/mu/command/api.rb
mu-5.7.39 lib/mu/command/api.rb
mu-5.7.38 lib/mu/command/api.rb
mu-5.7.37 lib/mu/command/api.rb
mu-5.7.36 lib/mu/command/api.rb
mu-5.7.35 lib/mu/command/api.rb
mu-5.7.34 lib/mu/command/api.rb
mu-5.7.33 lib/mu/command/api.rb
mu-5.7.32 lib/mu/command/api.rb
mu-5.7.31 lib/mu/command/api.rb
mu-5.7.30 lib/mu/command/api.rb
mu-5.7.29 lib/mu/command/api.rb
mu-5.7.28 lib/mu/command/api.rb
mu-5.7.27 lib/mu/command/api.rb
mu-5.7.26 lib/mu/command/api.rb
mu-5.7.25 lib/mu/command/api.rb
mu-5.7.24 lib/mu/command/api.rb
mu-5.7.23 lib/mu/command/api.rb
mu-5.7.22 lib/mu/command/api.rb