Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions auth/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,24 @@ func UpdateCreds() error {
})
}

func PurgeCredentials() error {
var fileList = []string{
"credentials.toml",
"columnar.lic",
}

prefix := filepath.Dir(credPath)

for _, file := range fileList {
fullPath := filepath.Join(prefix, file)
if err := os.Remove(fullPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
}

return nil
}

func IsColumnarPrivateRegistry(u *url.URL) bool {
return u.Host == DefaultOauthURI
}
Expand Down
13 changes: 11 additions & 2 deletions cmd/dbc/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (m loginModel) View() string { return m.spinner.View() + " Waiting for conf

type LogoutCmd struct {
RegistryURL string `arg:"positional" help:"URL of the driver registry to log out from"`
Purge bool `arg:"--purge" help:"Remove all local auth credentials for dbc"`
}

func (l LogoutCmd) GetModelCustom(baseModel baseModel) tea.Model {
Expand All @@ -242,6 +243,7 @@ func (l LogoutCmd) GetModelCustom(baseModel baseModel) tea.Model {
return logoutModel{
inputURI: l.RegistryURL,
baseModel: baseModel,
purge: l.Purge,
}
}

Expand All @@ -258,6 +260,7 @@ type logoutModel struct {
baseModel

inputURI string
purge bool
}

func (m logoutModel) Init() tea.Cmd {
Expand All @@ -279,8 +282,14 @@ func (m logoutModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case *url.URL:
return m, func() tea.Msg {
if err := auth.RemoveCredential(auth.Uri(*msg)); err != nil {
return fmt.Errorf("failed to log out: %w", err)
if m.purge {
if err := auth.PurgeCredentials(); err != nil {
return fmt.Errorf("failed to purge credentials: %w", err)
}
} else {
if err := auth.RemoveCredential(auth.Uri(*msg)); err != nil {
return fmt.Errorf("failed to log out: %w", err)
}
}

return tea.QuitMsg{}
Expand Down
72 changes: 71 additions & 1 deletion cmd/dbc/completions/dbc.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _dbc() {
local cur prev words cword
_init_completion || return

local subcommands="install uninstall init add sync search info docs remove completion"
local subcommands="install uninstall init add sync search info docs remove completion auth"
local global_opts="--help -h --version --quiet -q"

# If we're completing the first argument (subcommand)
Expand Down Expand Up @@ -47,6 +47,9 @@ _dbc() {
completion)
_dbc_completion_completions
;;
auth)
_dbc_auth_completions
;;
*)
COMPREPLY=()
;;
Expand Down Expand Up @@ -257,6 +260,73 @@ _dbc_completion_completions() {
COMPREPLY=()
}

_dbc_auth_completions() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

# If we're at position 2 (right after "auth"), suggest subcommands
if [[ $COMP_CWORD -eq 2 ]]; then
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
else
COMPREPLY=($(compgen -W "login logout" -- "$cur"))
fi
return 0
fi

# Get the auth subcommand (second argument)
local auth_subcommand="${COMP_WORDS[2]}"

case "$auth_subcommand" in
login)
_dbc_auth_login_completions
;;
logout)
_dbc_auth_logout_completions
;;
*)
COMPREPLY=()
;;
esac
}

_dbc_auth_login_completions() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

case "$prev" in
--api-key)
# API key should be provided by user, no completion
COMPREPLY=()
return 0
;;
esac

if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "-h --help --api-key" -- "$cur"))
return 0
fi

# Registry URL completion (no specific completion available)
COMPREPLY=()
}

_dbc_auth_logout_completions() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "-h --help --purge" -- "$cur"))
return 0
fi

# Registry URL completion (no specific completion available)
COMPREPLY=()
}

# Register the completion function
complete -F _dbc dbc

Expand Down
39 changes: 39 additions & 0 deletions cmd/dbc/completions/dbc.fish
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ complete -f -c dbc -n '__fish_dbc_needs_command' -a 'remove' -d 'Remove a driver
complete -f -c dbc -n '__fish_dbc_needs_command' -a 'info' -d 'Get detailed information about a specific driver'
complete -f -c dbc -n '__fish_dbc_needs_command' -a 'docs' -d 'Open driver documentation in a web browser'
complete -f -c dbc -n '__fish_dbc_needs_command' -a 'completion' -d 'Generate shell completions'
complete -f -c dbc -n '__fish_dbc_needs_command' -a 'auth' -d 'Authenticate with a driver registry'

# install subcommand
complete -f -c dbc -n '__fish_dbc_using_subcommand install' -s h -d 'Show Help'
Expand Down Expand Up @@ -94,3 +95,41 @@ complete -f -c dbc -n '__fish_dbc_using_subcommand completion' -l help -d 'Help'
complete -f -c dbc -n '__fish_dbc_using_subcommand completion' -a 'bash' -d 'Generate autocompletion script for bash'
complete -f -c dbc -n '__fish_dbc_using_subcommand completion' -a 'zsh' -d 'Generate autocompletion script for zsh'
complete -f -c dbc -n '__fish_dbc_using_subcommand completion' -a 'fish' -d 'Generate autocompletion script for fish'

# Helper function to check if we're using auth subcommand and need a nested subcommand
function __fish_dbc_auth_needs_subcommand
set -l cmd (commandline -opc)
if test (count $cmd) -eq 2
if test $cmd[2] = "auth"
return 0
end
end
return 1
end

# Helper function to check if we're using a specific auth subcommand
function __fish_dbc_auth_using_subcommand
set -l cmd (commandline -opc)
if test (count $cmd) -gt 2
if test $cmd[2] = "auth" -a $argv[1] = $cmd[3]
return 0
end
end
return 1
end

# auth subcommand
complete -f -c dbc -n '__fish_dbc_using_subcommand auth' -s h -d 'Help'
complete -f -c dbc -n '__fish_dbc_using_subcommand auth' -l help -d 'Help'
complete -f -c dbc -n '__fish_dbc_auth_needs_subcommand' -a 'login' -d 'Authenticate with a driver registry'
complete -f -c dbc -n '__fish_dbc_auth_needs_subcommand' -a 'logout' -d 'Log out from a driver registry'

# auth login subcommand
complete -f -c dbc -n '__fish_dbc_auth_using_subcommand login' -s h -d 'Help'
complete -f -c dbc -n '__fish_dbc_auth_using_subcommand login' -l help -d 'Help'
complete -f -c dbc -n '__fish_dbc_auth_using_subcommand login' -l api-key -d 'Authenticate using an API key instead of OAuth'

# auth logout subcommand
complete -f -c dbc -n '__fish_dbc_auth_using_subcommand logout' -s h -d 'Help'
complete -f -c dbc -n '__fish_dbc_auth_using_subcommand logout' -l help -d 'Help'
complete -f -c dbc -n '__fish_dbc_auth_using_subcommand logout' -l purge -d 'Remove all local auth credentials for dbc'
48 changes: 48 additions & 0 deletions cmd/dbc/completions/dbc.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function _dbc {
'docs[Open driver documentation in a web browser]' \
'remove[Remove a driver from the driver list]' \
'completion[Generate shell completions]' \
'auth[Authenticate with a driver registry]' \
'--help[Show help]' \
'-h[Show help]' \
'--version[Show version]' \
Expand Down Expand Up @@ -61,6 +62,9 @@ function _dbc {
completion)
_dbc_completion_completions
;;
auth)
_dbc_auth_completions
;;
esac
;;
esac
Expand Down Expand Up @@ -153,6 +157,50 @@ function _dbc_completion_completions {
':shell type:(bash zsh fish)'
}

function _dbc_auth_completions {
local line state

_arguments -C \
'(--help)-h[Help]' \
'(-h)--help[Help]' \
"1: :->auth_subcommand" \
"*::arg:->auth_args"

case $state in
auth_subcommand)
_values "auth subcommand" \
'login[Authenticate with a driver registry]' \
'logout[Log out from a driver registry]'
;;
auth_args)
case $line[1] in
login)
_dbc_auth_login_completions
;;
logout)
_dbc_auth_logout_completions
;;
esac
;;
esac
}

function _dbc_auth_login_completions {
_arguments \
'(--help)-h[Help]' \
'(-h)--help[Help]' \
'--api-key[Authenticate using an API key instead of OAuth]: :' \
':registry URL: '
}

function _dbc_auth_logout_completions {
_arguments \
'(--help)-h[Help]' \
'(-h)--help[Help]' \
'--purge[Remove all local auth credentials for dbc]' \
':registry URL: '
}

# don't run the completion function when being source-d or eval-d
if [ "$funcstack[1]" = "_dbc" ]; then
_dbc
Expand Down
Loading