Skip to content

Commit

Permalink
Refactor login to an Action.
Browse files Browse the repository at this point in the history
This refactor required a bit more than just moving the function from
`Program` to an action. In order to do this, we first need to
distinguish between actions that 'require' a client and those that
don't.

For example, `TeamList` is an action that requires access to the API and
therefore needs an client configured. However, `Login` simply collects
and persists the login credentials used to obtain a token. This action
does not require any API access.
  • Loading branch information
abrightwell committed Jun 3, 2022
1 parent 0713915 commit 586d8b9
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 55 deletions.
12 changes: 10 additions & 2 deletions src/cb/action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ require "log"
require "./client"

module CB
# Action is the base class for all actions performed by `cb`.
abstract class Action
Log = ::Log.for("Action")
Error = Program::Error

property input : IO
property output : IO
getter client

def initialize(@client : Client, @input = STDIN, @output = STDOUT)
def initialize(@input = STDIN, @output = STDOUT)
end

def call
Log.info { "calling #{self.class}" }
run
end
end

# APIAction performs some action utilizing the API.
abstract class APIAction < Action
property client : Client

def initialize(@client, @input = STDIN, @output = STDOUT)
end

abstract def run

Expand Down
6 changes: 3 additions & 3 deletions src/cb/backup.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./action"

module CB
class BackupCapture < Action
class BackupCapture < APIAction
eid_setter cluster_id

def run
Expand Down Expand Up @@ -55,7 +55,7 @@ module CB
end
end

class BackupList < Action
class BackupList < APIAction
eid_setter cluster_id

def run
Expand Down Expand Up @@ -88,7 +88,7 @@ module CB
end
end

class BackupToken < Action
class BackupToken < APIAction
eid_setter cluster_id
eid_setter stanza
ident_setter format
Expand Down
2 changes: 1 addition & 1 deletion src/cb/cluster_create.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::ClusterCreate < CB::Action
class CB::ClusterCreate < CB::APIAction
bool_setter ha
property name : String?
ident_setter plan
Expand Down
2 changes: 1 addition & 1 deletion src/cb/cluster_destroy.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::ClusterDestroy < CB::Action
class CB::ClusterDestroy < CB::APIAction
eid_setter cluster_id

def run
Expand Down
2 changes: 1 addition & 1 deletion src/cb/cluster_info.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::ClusterInfo < CB::Action
class CB::ClusterInfo < CB::APIAction
eid_setter cluster_id

def run
Expand Down
2 changes: 1 addition & 1 deletion src/cb/cluster_list.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::List < CB::Action
class CB::List < CB::APIAction
def run
teams = client.get_teams
clusters = client.get_clusters(teams)
Expand Down
2 changes: 1 addition & 1 deletion src/cb/cluster_rename.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::ClusterRename < CB::Action
class CB::ClusterRename < CB::APIAction
eid_setter cluster_id
property new_name : String?

Expand Down
4 changes: 2 additions & 2 deletions src/cb/cluster_suspend_resume.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./action"

module CB
class ClusterSuspend < Action
class ClusterSuspend < APIAction
eid_setter cluster_id

def run
Expand All @@ -11,7 +11,7 @@ module CB
end
end

class ClusterResume < Action
class ClusterResume < APIAction
eid_setter cluster_id

def run
Expand Down
2 changes: 1 addition & 1 deletion src/cb/cluster_upgrade.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

abstract class CB::Upgrade < CB::Action
abstract class CB::Upgrade < CB::APIAction
eid_setter cluster_id
property confirmed : Bool = false

Expand Down
2 changes: 1 addition & 1 deletion src/cb/cluster_uri.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::ClusterURI < CB::Action
class CB::ClusterURI < CB::APIAction
eid_setter cluster_id
property role_name : String = "default"

Expand Down
2 changes: 1 addition & 1 deletion src/cb/completion.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CB::Completion
end
end
rescue NoClientError
[] of String
suggest_none
end

def top_level
Expand Down
2 changes: 1 addition & 1 deletion src/cb/detach.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::Detach < CB::Action
class CB::Detach < CB::APIAction
eid_setter cluster_id
property confirmed : Bool = false

Expand Down
2 changes: 1 addition & 1 deletion src/cb/logdest_add.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./action"

module CB
class LogDestinationAdd < Action
class LogDestinationAdd < APIAction
eid_setter cluster_id
i32_setter port
property host : String?
Expand Down
2 changes: 1 addition & 1 deletion src/cb/logdest_destroy.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./action"

module CB
class LogDestinationDestroy < Action
class LogDestinationDestroy < APIAction
eid_setter cluster_id
eid_setter logdest_id

Expand Down
2 changes: 1 addition & 1 deletion src/cb/logdest_list.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./action"

module CB
class LogDestinationList < Action
class LogDestinationList < APIAction
eid_setter cluster_id

def run
Expand Down
27 changes: 27 additions & 0 deletions src/cb/login.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "./action"

class CB::Login < CB::Action
def run
host = CB::HOST

raise CB::Program::Error.new "No valid credentials found. Please login." unless output.tty?
hint = "from https://www.crunchybridge.com/account" if host == "api.crunchybridge.com"
output.puts "add credentials for #{host.colorize.t_name} #{hint}>"
output.print " application ID: "
id = input.gets
if id.nil? || id.empty?
STDERR.puts "#{"error".colorize.red.bold}: application ID must be present"
exit 1
end

print " application secret: "
secret = input.noecho { input.gets }
output.print "\n"
if secret.nil? || secret.empty?
STDERR.puts "#{"error".colorize.red.bold}: application secret must be present"
exit 1
end

Creds.new(host, id, secret).store
end
end
2 changes: 1 addition & 1 deletion src/cb/logs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "./dirs"
require "ssh2"

module CB
class Logs < Action
class Logs < APIAction
eid_setter cluster_id

def run
Expand Down
2 changes: 1 addition & 1 deletion src/cb/manage_firewall.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CB::ManageFirewall < CB::Action
class CB::ManageFirewall < CB::APIAction
Error = Program::Error

eid_setter cluster_id
Expand Down
26 changes: 1 addition & 25 deletions src/cb/program.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,11 @@ class CB::Program
Colorize.enabled = false unless output == STDOUT && input == STDIN
end

def login
host = CB::HOST

raise Error.new "No valid credentials found. Please login." unless output.tty?
hint = "from https://www.crunchybridge.com/account" if host == "api.crunchybridge.com"
output.puts "add credentials for #{host.colorize.t_name} #{hint}>"
output.print " application ID: "
id = input.gets
if id.nil? || id.empty?
STDERR.puts "#{"error".colorize.red.bold}: application ID must be present"
exit 1
end

print " application secret: "
secret = input.noecho { input.gets }
output.print "\n"
if secret.nil? || secret.empty?
STDERR.puts "#{"error".colorize.red.bold}: application secret must be present"
exit 1
end

Creds.new(host, id, secret).store
end

def creds : CB::Creds
if c = @creds
return c
end
@cred = Creds.for_host(CB::HOST) || login
@cred = Creds.for_host(CB::HOST) || CB::Login.new.run
end

def token : CB::Token
Expand Down
2 changes: 1 addition & 1 deletion src/cb/psql.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::Psql < CB::Action
class CB::Psql < CB::APIAction
eid_setter cluster_id
property database : String?

Expand Down
2 changes: 1 addition & 1 deletion src/cb/restart.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::Restart < CB::Action
class CB::Restart < CB::APIAction
eid_setter cluster_id
bool_setter confirmed
bool_setter full
Expand Down
2 changes: 1 addition & 1 deletion src/cb/role.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module CB
VALID_CLUSTER_ROLES = Set{"application", "default", "postgres", "user"}
end

abstract class CB::RoleAction < CB::Action
abstract class CB::RoleAction < CB::APIAction
eid_setter cluster_id
property role_name : String?
end
Expand Down
2 changes: 1 addition & 1 deletion src/cb/scope.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./action"
require "./scope_checks/*"

class CB::Scope < CB::Action
class CB::Scope < CB::APIAction
property cluster_id : String?
property checks : Array(::Scope::Check.class) = [] of ::Scope::Check.class
property database : String?
Expand Down
2 changes: 1 addition & 1 deletion src/cb/team.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

abstract class CB::TeamAction < CB::Action
abstract class CB::TeamAction < CB::APIAction
eid_setter team_id

private def team_details(t : CB::Client::Team) : String
Expand Down
2 changes: 1 addition & 1 deletion src/cb/team_cert.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::TeamCert < CB::Action
class CB::TeamCert < CB::APIAction
eid_setter team_id

def run
Expand Down
2 changes: 1 addition & 1 deletion src/cb/team_member.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end
#
# Provides common properties and functionality specific to team member managment
# actions.
abstract class CB::TeamMemberAction < CB::Action
abstract class CB::TeamMemberAction < CB::APIAction
eid_setter team_id
eid_setter account_id
property email : String?
Expand Down
2 changes: 1 addition & 1 deletion src/cb/whoami.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./action"

class CB::WhoAmI < CB::Action
class CB::WhoAmI < CB::APIAction
def run
output << "user id: ".colorize.t_id << client.token.user_id << "\n"
output << " name: ".colorize.t_id << client.token.name << "\n"
Expand Down
2 changes: 1 addition & 1 deletion src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ op = OptionParser.new do |parser|

parser.on("login", "Store API key") do
parser.banner = "cb login"
action = ->{ PROG.login }
action = CB::Login.new
end

parser.on("list", "List clusters") do
Expand Down

0 comments on commit 586d8b9

Please sign in to comment.