feat: switch gitea login from tea to gt
This commit is contained in:
1 parent
9ebb35860c
commit
a1cfcc1de7
4 files changed
+186
-27
No files matched your search
+50
-20
@@ -18,7 +18,6 @@ const (
|
||||
DefaultGiteaURL = "https://git.site.latha.org"
|
||||
DefaultRelayURL = "https://deploy.site.latha.org"
|
||||
DefaultClientID = "98c0bb53-4019-4295-bed8-122ecbf9c31f"
|
||||
DefaultTeaName = "latha"
|
||||
)
|
||||
|
||||
type TokenResponse struct {
|
||||
@@ -166,42 +165,73 @@ func ExchangeCode(ctx context.Context, client *http.Client, giteaURL, clientID,
|
||||
return &tok, nil
|
||||
}
|
||||
|
||||
func FindTeaBinary(override string) (string, error) {
|
||||
// FindGTBinary locates the Gitea CLI (`gt`) executable from an explicit override, PATH, or ~/.local/bin/gt.
|
||||
func FindGTBinary(override string) (string, error) {
|
||||
if override != "" {
|
||||
return override, nil
|
||||
}
|
||||
if p, err := exec.LookPath("tea"); err == nil {
|
||||
if p, err := exec.LookPath("gt"); err == nil {
|
||||
return p, nil
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
candidate := filepath.Join(home, ".local", "bin", "tea")
|
||||
candidate := filepath.Join(home, ".local", "bin", "gt")
|
||||
if info, statErr := os.Stat(candidate); statErr == nil && !info.IsDir() {
|
||||
return candidate, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("tea binary not found in PATH or ~/.local/bin/tea")
|
||||
return "", fmt.Errorf("gt binary not found in PATH or ~/.local/bin/gt (install from https://gitea.com/gitea/cli)")
|
||||
}
|
||||
|
||||
func ConfigureTeaAndGit(ctx context.Context, teaBin, name, giteaURL, token string) error {
|
||||
bin, err := FindTeaBinary(teaBin)
|
||||
// ConfigureGTAndGit logs gt into giteaURL using token and configures Git's credential helper via gt.
|
||||
// credentialStore may be "", "auto", "keyring", or "file".
|
||||
func ConfigureGTAndGit(ctx context.Context, gtBin, giteaURL, token, credentialStore string) error {
|
||||
switch credentialStore {
|
||||
case "", "auto", "keyring", "file":
|
||||
default:
|
||||
return fmt.Errorf("invalid credential store %q (expected auto, keyring, or file)", credentialStore)
|
||||
}
|
||||
|
||||
bin, err := FindGTBinary(gtBin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Remove existing login with the same name if present so we can cleanly update it.
|
||||
delCmd := exec.CommandContext(ctx, bin, "login", "delete", name)
|
||||
_ = delCmd.Run()
|
||||
|
||||
addCmd := exec.CommandContext(ctx, bin,
|
||||
"login", "add",
|
||||
"--name", name,
|
||||
"--url", strings.TrimRight(giteaURL, "/"),
|
||||
"--token", token,
|
||||
"--helper",
|
||||
)
|
||||
out, err := addCmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("tea login add failed: %w: %s", err, strings.TrimSpace(string(out)))
|
||||
baseURL := strings.TrimRight(giteaURL, "/")
|
||||
login := func(store string) ([]byte, error) {
|
||||
args := []string{"auth", "login", "--host", baseURL, "--with-token", "--yes", "--no-input"}
|
||||
if store != "" {
|
||||
args = append(args, "--credential-store", store)
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, bin, args...)
|
||||
cmd.Stdin = strings.NewReader(token + "\n")
|
||||
return cmd.CombinedOutput()
|
||||
}
|
||||
|
||||
if credentialStore == "" || credentialStore == "auto" {
|
||||
out, err := login("")
|
||||
if err != nil {
|
||||
msg := strings.TrimSpace(string(out))
|
||||
if isKeyringUnavailable(msg) {
|
||||
out, err = login("file")
|
||||
msg = strings.TrimSpace(string(out))
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("gt auth login failed: %w: %s", err, msg)
|
||||
}
|
||||
}
|
||||
} else if out, err := login(credentialStore); err != nil {
|
||||
return fmt.Errorf("gt auth login failed: %w: %s", err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
|
||||
setupCmd := exec.CommandContext(ctx, bin, "auth", "setup-git", "--host", baseURL, "--no-input")
|
||||
if out, err := setupCmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("gt auth setup-git failed: %w: %s", err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isKeyringUnavailable(msg string) bool {
|
||||
return strings.Contains(msg, "secure credential storage unavailable") ||
|
||||
strings.Contains(msg, "keyring storage unavailable")
|
||||
}
|
||||
Reference in new issue
Block a user