mirror of
https://gitea.com/gitea/tea.git
synced 2026-09-15 11:28:11 +02:00
Implements #1087. Adds `tea login status [<login name>] [-o <format>]`, which verifies the stored token for one or all configured logins and reports: - login name/URL and default status - whether the token is valid (via `GET /api/v1/user`) - auth method and token expiry - whether the git credential helper is configured Machine-readable output is available via the usual `-o` formats with fields `name`, `url`, `user`, `valid`, `auth_method`, `token_expiry`, `helper`, and `default`. Reviewed-on: https://gitea.com/gitea/tea/pulls/1105 Reviewed-by: bircni <bircni@icloud.com>
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package login
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.dev/tea/cmd/flags"
|
|
"gitea.dev/tea/modules/config"
|
|
"gitea.dev/tea/modules/print"
|
|
"gitea.dev/tea/modules/task"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// CmdLoginStatus represents a command to show authentication status for logins.
|
|
var CmdLoginStatus = cli.Command{
|
|
Name: "status",
|
|
Usage: "Show authentication status for Gitea logins",
|
|
Description: `Verify the stored token for one or all Gitea logins and report its validity.`,
|
|
ArgsUsage: "[<login name>]",
|
|
Action: RunLoginStatus,
|
|
Flags: []cli.Flag{&flags.OutputFlag},
|
|
}
|
|
|
|
// RunLoginStatus verifies one login, or every configured login when no name is
|
|
// provided, and prints a short authentication report.
|
|
func RunLoginStatus(requestCtx context.Context, cmd *cli.Command) error {
|
|
var logins []config.Login
|
|
|
|
switch cmd.Args().Len() {
|
|
case 0:
|
|
var err error
|
|
logins, err = config.GetLogins()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case 1:
|
|
login, err := config.GetLoginByName(cmd.Args().First())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if login == nil {
|
|
return fmt.Errorf("login '%s' not found", cmd.Args().First())
|
|
}
|
|
logins = []config.Login{*login}
|
|
default:
|
|
return fmt.Errorf("too many arguments")
|
|
}
|
|
|
|
statuses := make([]print.LoginStatus, 0, len(logins))
|
|
for i := range logins {
|
|
statuses = append(statuses, task.CheckLoginStatus(requestCtx, &logins[i]))
|
|
}
|
|
|
|
return print.LoginStatuses(statuses, cmd.String("output"))
|
|
}
|