diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fbe7a5..f7858ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 the CLI. + ## [2.1.0] - 2022-06-03 ### Added diff --git a/src/cb/cacheable.cr b/src/cb/cacheable.cr index aac1d90..d189ba2 100644 --- a/src/cb/cacheable.cr +++ b/src/cb/cacheable.cr @@ -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 @@ -49,4 +55,8 @@ module CB::Cacheable end self end + + def delete + File.delete(self.class.file_path(key)) + end end diff --git a/src/cb/completion.cr b/src/cb/completion.cr index 8e851a3..31959c7 100644 --- a/src/cb/completion.cr +++ b/src/cb/completion.cr @@ -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", @@ -111,7 +112,7 @@ class CB::Completion if @client options else - options.first 3 + options.first 4 end end diff --git a/src/cb/creds.cr b/src/cb/creds.cr index bb96a5d..3288f59 100644 --- a/src/cb/creds.cr +++ b/src/cb/creds.cr @@ -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 diff --git a/src/cb/logout.cr b/src/cb/logout.cr new file mode 100644 index 0000000..6a46f02 --- /dev/null +++ b/src/cb/logout.cr @@ -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 diff --git a/src/cb/token.cr b/src/cb/token.cr index dc83fdc..0a99ccf 100644 --- a/src/cb/token.cr +++ b/src/cb/token.cr @@ -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 diff --git a/src/cli.cr b/src/cli.cr index a675040..02a463e 100755 --- a/src/cli.cr +++ b/src/cli.cr @@ -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