Skip to content

Commit

Permalink
Add cb logout command.
Browse files Browse the repository at this point in the history
These changes add a new command and action called `logout`. The purpose
of these are to allow a user to clear all credentials from their system
which has the effect of logging them out of the system.  To do this, we
remove both the stored credentials/API key. As well, we remove the
cached token regardless of whether or not it is expired.

In the case that none of the above items exist on the system, the effect
is the same as a no-op.
  • Loading branch information
abrightwell committed Jun 15, 2022
1 parent 4b8f5e8 commit 3f89c68
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `cb logout` to logout a user from the CLI.


## [2.1.0] - 2022-06-03
### Added
Expand Down
16 changes: 13 additions & 3 deletions src/cb/cacheable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ module CB::Cacheable
end

def fetch?(key)
token = fetch key

return nil unless token
return nil if Time.local > token.expires_at

token
end

def fetch(key)
begin
token = from_json File.read(file_path(key))
rescue File::Error | JSON::ParseException
return nil
end

return nil unless token
return nil if Time.local > token.expires_at

token
end

Expand All @@ -49,4 +55,8 @@ module CB::Cacheable
end
self
end

def delete
File.delete(self.class.file_path(key))
end
end
3 changes: 2 additions & 1 deletion src/cb/completion.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class CB::Completion
"--help\tShow help and usage",
"version\tShow version information",
"login\tStore API key",
"logout\tRemove stored API key and token",
"token\tGet current API token",
"list\tList clusters",
"team\tManage teams",
Expand All @@ -111,7 +112,7 @@ class CB::Completion
if @client
options
else
options.first 3
options.first 4
end
end

Expand Down
5 changes: 5 additions & 0 deletions src/cb/creds.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ struct CB::Creds
def delete
File.delete CONFIG/host
end

def self.delete(host)
creds = for_host host
creds.delete unless creds.nil?
end
end
11 changes: 11 additions & 0 deletions src/cb/logout.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "./action"

module CB
class Logout < Action
def run
host = CB::HOST
Creds.delete(host)
Token.delete(host)
end
end
end
5 changes: 5 additions & 0 deletions src/cb/token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ struct CB::Token
fetch? host
end

def self.delete(host)
token = fetch host
token.delete unless token.nil?
end

def expires_at : Time
Time.unix expires
end
Expand Down
5 changes: 5 additions & 0 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ op = OptionParser.new do |parser|
action = CB::Login.new
end

parser.on("logout", "Remove stored API key and tokens") do
parser.banner = "cb logout"
action = CB::Logout.new
end

parser.on("list", "List clusters") do
parser.banner = "cb list"
set_action List
Expand Down

0 comments on commit 3f89c68

Please sign in to comment.