Skip to content

Commit fb5158a

Browse files
committed
Merge pull request #44 from DeployGate/work/user_config_command
Add login and config command
2 parents e604dec + 5ad1aa8 commit fb5158a

File tree

9 files changed

+188
-28
lines changed

9 files changed

+188
-28
lines changed

lib/deploygate.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ module DeployGate
2828
require "deploygate/api/v1/push"
2929
require "deploygate/api/v1/user"
3030
require "deploygate/command_builder"
31-
require "deploygate/commands/init"
31+
require "deploygate/commands/login"
3232
require "deploygate/commands/logout"
33+
require "deploygate/commands/config"
3334
require "deploygate/commands/deploy"
3435
require "deploygate/commands/deploy/push"
3536
require "deploygate/commands/deploy/build"

lib/deploygate/api/v1/session.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ class Session
66

77
class << self
88

9+
# @param [String] token
10+
# @return [Hash]
11+
def show(token)
12+
res = Base.new(token).get(ENDPOINT + '/user', {})
13+
return nil if res['error']
14+
15+
res['results']
16+
end
17+
918
# @param [String] name
1019
# @param [String] token
1120
# @return [Boolean]
1221
def check(name, token)
13-
res = Base.new(token).get(ENDPOINT + '/user', {})
14-
return false if res['error']
22+
results = show(token)
23+
return false if results.nil?
1524

16-
name == res['results']['name']
25+
name == results['name']
1726
end
1827

1928
# @param [String] email

lib/deploygate/command_builder.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ def run
1111
program :version, VERSION
1212
program :description, 'You can control to DeployGate in your terminal.'
1313

14-
command :init do |c|
15-
c.syntax = 'dg init'
16-
c.description = 'project initial command'
14+
command :login do |c|
15+
c.syntax = 'dg login'
16+
c.description = 'DeployGate login command'
1717
c.action do |args, options|
1818
begin
19-
Commands::Init.run
19+
Commands::Login.run
2020
rescue => e
21-
error_handling("Commands::Init Error: #{e.class}", create_error_issue_body(e))
21+
error_handling("Commands::Login Error: #{e.class}", create_error_issue_body(e))
2222
raise e
2323
end
2424
end
@@ -56,6 +56,22 @@ def run
5656
end
5757
end
5858

59+
command :config do |c|
60+
c.syntax = 'dg config'
61+
c.description = 'dg user login config'
62+
c.option '--json', 'output json format'
63+
c.option '--name STRING', String, 'your DeployGate user name'
64+
c.option '--token STRING', String, 'your DeployGate api token'
65+
c.action do |args, options|
66+
begin
67+
Commands::Config.run(args, options)
68+
rescue => e
69+
error_handling("Commands::Config Error: #{e.class}", create_error_issue_body(e))
70+
raise e
71+
end
72+
end
73+
end
74+
5975
run!
6076
end
6177

lib/deploygate/commands/config.rb

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

lib/deploygate/commands/deploy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class << self
66
# @param [Array] args
77
# @param [Commander::Command::Options] options
88
def run(args, options)
9-
Init.start_login_or_create_account() unless DeployGate::Session.new.login?
9+
Login.start_login_or_create_account() unless DeployGate::Session.new.login?
1010

1111
# push or build(android/ios)
1212
args.push(Dir.pwd) if args.empty?

lib/deploygate/commands/deploy/push.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class << self
1212
def upload(args, options)
1313
session = DeployGate::Session.new()
1414
unless session.login?
15-
Init.login
15+
Login.start_login_or_create_account()
1616
session = DeployGate::Session.new()
1717
end
1818

lib/deploygate/commands/init.rb renamed to lib/deploygate/commands/login.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
module DeployGate
22
module Commands
3-
class Init
3+
class Login
44
class << self
55

66
# @return [void]
77
def run
8-
start_login_or_create_account() unless Session.new().login?
9-
10-
finish
8+
start_login_or_create_account()
119
end
1210

1311
# @return [void]
@@ -23,7 +21,7 @@ def start_login_or_create_account
2321
puts ''
2422
password = input_password('Password: ')
2523
puts ''
26-
login(email, password)
24+
start(email, password)
2725
else
2826
create_account(email)
2927
end
@@ -32,7 +30,7 @@ def start_login_or_create_account
3230
# @param [String] email
3331
# @param [String] password
3432
# @return [void]
35-
def login(email, password)
33+
def start(email, password)
3634
begin
3735
Session.login(email, password)
3836
rescue Session::LoginError => e
@@ -66,7 +64,7 @@ def create_account(email)
6664
raise 'User create error'
6765
else
6866
Message::Success.print('done! Your account has been set up successfully.')
69-
login(email, password)
67+
start(email, password)
7068
end
7169
end
7270

@@ -102,11 +100,6 @@ def input_password(message)
102100
ask(message) { |q| q.echo = "*" }
103101
end
104102

105-
# @return [void]
106-
def finish
107-
Message::Success.print('Enjoy development!')
108-
end
109-
110103
def print_deploygate_aa
111104
puts <<'EOF'
112105
_ _ _

lib/deploygate/session.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def login?
1717
@@login = @@login.nil? ? API::V1::Session.check(@name, @token) : @@login
1818
end
1919

20+
def show_login_user
21+
API::V1::Session.show(@token)
22+
end
23+
2024
# @param [String] email
2125
# @param [String] password
2226
# @return [void]

spec/deploygate/api/v1/session_spec.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
describe DeployGate::API::V1::Session do
2-
describe "#check" do
2+
describe "#show" do
33
it "logined" do
44
token = 'token'
55
name = 'test'
66
response = {
77
:error => false,
88
:because => '',
9-
:results => {:name => name}
9+
:results => {'name' => name}
1010
}
1111
stub_request(:get, "#{API_ENDPOINT}/sessions/user").
1212
with(:headers => { 'AUTHORIZATION' => token }).
1313
to_return(:body => response.to_json)
1414

1515

16-
result = DeployGate::API::V1::Session.check(name, token)
17-
expect(result).to be_truthy
16+
results = DeployGate::API::V1::Session.show(token)
17+
expect(results).to eql response[:results]
1818
end
1919

2020
it "not login" do
2121
token = 'token'
22-
name = 'test'
2322
response = {
2423
:error => true,
2524
:because => 'error message'
@@ -28,6 +27,26 @@
2827
with(:headers => { 'AUTHORIZATION' => token }).
2928
to_return(:body => response.to_json)
3029

30+
results = DeployGate::API::V1::Session.show(token)
31+
expect(results).to eql response[:results]
32+
end
33+
end
34+
describe "#check" do
35+
it "logined" do
36+
token = 'token'
37+
name = 'test'
38+
results = {'name' => name}
39+
allow(DeployGate::API::V1::Session).to receive(:show).and_return(results)
40+
41+
result = DeployGate::API::V1::Session.check(name, token)
42+
expect(result).to be_truthy
43+
end
44+
45+
it "not login" do
46+
token = 'token'
47+
name = 'test'
48+
allow(DeployGate::API::V1::Session).to receive(:show).and_return(nil)
49+
3150
result = DeployGate::API::V1::Session.check(name, token)
3251
expect(result).to be_falsey
3352
end

0 commit comments

Comments
 (0)