Skip to content

Commit

Permalink
Refactor cb token to an Action.
Browse files Browse the repository at this point in the history
Here were simply pulling the `cb token` command into an action.
  • Loading branch information
abrightwell committed Jun 13, 2022
1 parent 6d7c032 commit 4b8f5e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/cb/token.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
require "./cacheable"

# TODO (abrightwell): We had to explicitly qualify this class name as an
# `Action` due to conflicts with the below `Token` struct. Would be great to
# potentially namespace actions under `CB::Action` or something. Something
# perhaps worth considering.
class CB::TokenAction < CB::Action
enum Format
Default
Header
end

property token : Token
property format : Format = Format::Default

def initialize(@token, @input, @output)
end

def run
case @format
when "header"
output << "Authorization: Bearer #{token.token}"
when "default"
output << token.token
end
end
end

struct CB::Token
Cacheable.include key: host
getter host : String
Expand Down
7 changes: 3 additions & 4 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,9 @@ op = OptionParser.new do |parser|

parser.on("token", "Return a bearer token for use in the api") do
parser.banner = "cb token [-H]"
action = ->{ puts PROG.token.token }
parser.on("-H", "Authorization header format") do
action = ->{ puts "Authorization: Bearer #{PROG.token.token}" }
end
token = action = CB::TokenAction.new PROG.token, PROG.input, PROG.output

parser.on("-H", "Authorization header format") { token.format = CB::TokenAction::Format::Header }
end

parser.on("version", "Show the version") do
Expand Down

0 comments on commit 4b8f5e8

Please sign in to comment.