|
| 1 | +module DeployGate |
| 2 | + module Commands |
| 3 | + class Config |
| 4 | + class << self |
| 5 | + |
| 6 | + # @param [Array] args |
| 7 | + # @param [Commander::Command::Options] options |
| 8 | + def run(args, options) |
| 9 | + json_format = options.json |
| 10 | + name = options.name |
| 11 | + token = options.token |
| 12 | + |
| 13 | + if name.nil? || token.nil? |
| 14 | + login_user = DeployGate::Session.new().show_login_user |
| 15 | + if login_user.nil? |
| 16 | + print_not_login(json_format) |
| 17 | + else |
| 18 | + print_login_user(login_user, json_format) |
| 19 | + end |
| 20 | + else |
| 21 | + login(name, token, json_format) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + # @param [String] name |
| 26 | + # @param [String] token |
| 27 | + # @param [Boolean] json_format |
| 28 | + # @return [void] |
| 29 | + def login(name, token, json_format) |
| 30 | + if API::V1::Session.check(name, token) |
| 31 | + DeployGate::Session.save(name, token) |
| 32 | + login_user = DeployGate::Session.new().show_login_user |
| 33 | + print_login_success(login_user, json_format) |
| 34 | + else |
| 35 | + print_login_failed(json_format) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + # @param [Hash] login_user |
| 40 | + # @param [Boolean] json_format |
| 41 | + # @return [void] |
| 42 | + def print_login_success(login_user, json_format) |
| 43 | + message = 'Login success' |
| 44 | + data = {:message => message, :error => false} |
| 45 | + |
| 46 | + unless json_format |
| 47 | + DeployGate::Message::Success.print(message) |
| 48 | + puts '' |
| 49 | + end |
| 50 | + |
| 51 | + print_login_user(login_user, json_format, data) |
| 52 | + end |
| 53 | + |
| 54 | + # @param [Boolean] json_format |
| 55 | + # @param [Hash] data |
| 56 | + # @return [void] |
| 57 | + def print_login_failed(json_format, data = {}) |
| 58 | + message = 'Login failed' |
| 59 | + data[:error] = true |
| 60 | + data[:message] = message |
| 61 | + |
| 62 | + if json_format |
| 63 | + print_json(data) |
| 64 | + else |
| 65 | + DeployGate::Message::Error.print(message) |
| 66 | + puts <<EOF |
| 67 | +
|
| 68 | +Please check your name and api token. |
| 69 | +EOF |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + # @param [Boolean] json_format |
| 74 | + # @param [Hash] data |
| 75 | + # @return [void] |
| 76 | + def print_not_login(json_format, data = {}) |
| 77 | + message = 'Not user login' |
| 78 | + data[:error] = true |
| 79 | + data[:message] = message |
| 80 | + |
| 81 | + if json_format |
| 82 | + print_json(data) |
| 83 | + else |
| 84 | + DeployGate::Message::Warning.print(message) |
| 85 | + puts <<EOF |
| 86 | +
|
| 87 | +Please login to dg command. |
| 88 | +$ dg login |
| 89 | +EOF |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + # @param [Hash] login_user |
| 94 | + # @param [Boolean] json_format |
| 95 | + # @param [Hash] data |
| 96 | + # @return [void] |
| 97 | + def print_login_user(login_user, json_format, data = {}) |
| 98 | + data[:error] = data[:error].nil? ? false : data[:error] |
| 99 | + data[:name] = login_user['name'] |
| 100 | + |
| 101 | + if json_format |
| 102 | + print_json(data) |
| 103 | + else |
| 104 | + puts <<EOF |
| 105 | +User name: #{data[:name]} |
| 106 | +EOF |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + # @param [Hash] data |
| 111 | + # @return [void] |
| 112 | + def print_json(data) |
| 113 | + puts data.to_json |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments