✨ feat(spx): 按 git origin 支持 GitHub 仓库,state 评论改成隐藏注释格式,新增 issue view / pr view / pr create
This commit is contained in:
1 parent
64d5961b62
commit
c90d84d7f2
17 files changed
+2683
-400
No files matched your search
@@ -1,16 +1,18 @@
|
||||
// Package tea reads the tea CLI config to extract Gitea host + token.
|
||||
// Package tea reads the tea CLI config to find the Gitea login, and its token, for a given host.
|
||||
//
|
||||
// tea config typically lives at $HOME/.config/tea/config.yml. Older versions
|
||||
// of tea (<v0.10) stored the token inline in the YAML; newer versions move
|
||||
// tokens into a system keyring and only keep metadata in the file.
|
||||
//
|
||||
// We support the inline form here (matches the spx task contract). If the
|
||||
// token field is missing, callers should consider falling back to a
|
||||
// GITEA_TOKEN environment variable and emit a clear error otherwise.
|
||||
// We support the inline form here. If the token field is missing, callers
|
||||
// fall back to the GITEA_TOKEN environment variable.
|
||||
package tea
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -53,52 +55,25 @@ func load(path string) (*TeaConfig, error) {
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// LoadDefault returns host and token from the default login entry.
|
||||
// LoginForHost returns the login in the tea config at `path` whose URL points at `host`, or `nil` when none does.
|
||||
//
|
||||
// If multiple logins have default=true, the first wins. If no entry is marked
|
||||
// default, the first entry is used.
|
||||
func LoadDefault() (host, token string, err error) {
|
||||
path, err := DefaultConfigPath()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
// Hosts are compared by hostname only, ignoring case, scheme, port and trailing slashes, so the host read from a git
|
||||
// SSH remote still finds the login saved with the forge's web URL. A missing config file is not an error either,
|
||||
// because the caller can still fall back to `GITEA_TOKEN`.
|
||||
func LoginForHost(path, host string) (*TeaLogin, error) {
|
||||
cfg, err := load(path)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
return nil, err
|
||||
}
|
||||
if len(cfg.Logins) == 0 {
|
||||
return "", "", fmt.Errorf("tea config %s 里没有 logins 条目,请先运行 `tea login add`", path)
|
||||
}
|
||||
var chosen *TeaLogin
|
||||
want := strings.ToLower(host)
|
||||
for i := range cfg.Logins {
|
||||
if cfg.Logins[i].Default {
|
||||
chosen = &cfg.Logins[i]
|
||||
break
|
||||
u, perr := url.Parse(strings.TrimSpace(cfg.Logins[i].URL))
|
||||
if perr == nil && strings.ToLower(u.Hostname()) == want {
|
||||
return &cfg.Logins[i], nil
|
||||
}
|
||||
}
|
||||
if chosen == nil {
|
||||
chosen = &cfg.Logins[0]
|
||||
}
|
||||
return chosen.URL, chosen.Token, nil
|
||||
}
|
||||
|
||||
// LoadByHost returns the token for the login whose URL matches host.
|
||||
//
|
||||
// Host matching is case-insensitive and ignores trailing slashes.
|
||||
func LoadByHost(host string) (token string, err error) {
|
||||
path, err := DefaultConfigPath()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cfg, err := load(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
want := strings.TrimRight(strings.ToLower(host), "/")
|
||||
for _, l := range cfg.Logins {
|
||||
if strings.TrimRight(strings.ToLower(l.URL), "/") == want {
|
||||
return l.Token, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("tea config 里没找到 host=%s 的 login", host)
|
||||
return nil, nil
|
||||
}
|
||||
Reference in new issue
Block a user