Module: Kward::CLI::AuthCommands

Included in:
Kward::CLI
Defined in:
lib/kward/cli/auth_commands.rb

Overview

Login, logout, and credential-status commands mixed into the CLI frontend.

Instance Method Summary collapse

Instance Method Details

#auth_status_line(label, configured, location) ⇒ Object



34
35
36
37
38
# File 'lib/kward/cli/auth_commands.rb', line 34

def auth_status_line(label, configured, location)
  status = configured ? :ok : :warning
  message = configured ? "configured" : "not configured"
  "#{doctor_mark(status)} #{label}: #{message} (#{location})"
end

#handle_auth_command(arguments) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kward/cli/auth_commands.rb', line 7

def handle_auth_command(arguments)
  if help_option_arguments?(arguments)
    print_command_help("auth")
    return
  end

  case arguments
  when ["status"]
    print_auth_status
  when ["logout"]
    logout_auth
  else
    raise ArgumentError, command_usage("auth")
  end
end

#login(provider: nil, oauth: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kward/cli/auth_commands.rb', line 57

def (provider: nil, oauth: nil)
  provider = provider.to_s.downcase
  if provider == "openrouter"
    auth = oauth || OpenRouterAPIKey.new
    path = auth.(prompt: @prompt)
    @prompt.say("#{colored("Saved", :green, :bold)} OpenRouter API key to #{path}")
    return
  end

  oauth ||= case provider
            when "github" then GithubOAuth.new
            when "anthropic", "claude" then AnthropicOAuth.new
            else OpenAIOAuth.new
            end
  path = oauth.(prompt: @prompt)
  name = case provider
         when "github" then "GitHub"
         when "anthropic", "claude" then "Anthropic"
         else "OpenAI"
         end
  @prompt.say("#{colored("Saved", :green, :bold)} #{name} OAuth login to #{path}")
end

#logout_authObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kward/cli/auth_commands.rb', line 40

def logout_auth
  removed = []
  [OpenAIOAuth.default_auth_path, AnthropicOAuth.default_auth_path, GithubOAuth.default_auth_path].each do |path|
    next unless File.exist?(path)

    File.delete(path)
    removed << path
  end
  removed << "OpenRouter API key" if OpenRouterAPIKey.new.logout

  if removed.empty?
    @prompt.say "No saved credentials found."
  else
    @prompt.say "Removed #{removed.length} saved credential#{removed.length == 1 ? "" : "s"}."
  end
end

Writes the auth status output for the terminal CLI flow.



24
25
26
27
28
29
30
31
32
# File 'lib/kward/cli/auth_commands.rb', line 24

def print_auth_status
  config = safely_read_config.to_h
  lines = ["#{colored("Auth Status", :green, :bold)}", ""]
  lines << auth_status_line("OpenAI OAuth", File.exist?(OpenAIOAuth.default_auth_path), OpenAIOAuth.default_auth_path)
  lines << auth_status_line("Anthropic OAuth", File.exist?(AnthropicOAuth.default_auth_path), AnthropicOAuth.default_auth_path)
  lines << auth_status_line("GitHub OAuth", File.exist?(GithubOAuth.default_auth_path), GithubOAuth.default_auth_path)
  lines << auth_status_line("OpenRouter API key", !config["openrouter_api_key"].to_s.empty? || !ENV["OPENROUTER_API_KEY"].to_s.empty?, ConfigFiles.config_path)
  @prompt.say lines.join("\n")
end