mirror of
https://gitea.com/gitea/tea.git
synced 2026-06-06 03:08:44 +02:00
8e0666ab85
Reviewed-on: https://gitea.com/gitea/tea/pulls/1003 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.com> Co-committed-by: techknowlogick <techknowlogick@gitea.com>
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package sshkeys
|
|
|
|
import (
|
|
stdctx "context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"gitea.dev/tea/cmd/flags"
|
|
"gitea.dev/tea/modules/context"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// CmdSSHKeyAdd represents a sub command of ssh-keys to add an SSH public key
|
|
var CmdSSHKeyAdd = cli.Command{
|
|
Name: "add",
|
|
Usage: "Add an SSH public key",
|
|
Description: "Add an SSH public key to the current user's profile",
|
|
ArgsUsage: "<key-file>",
|
|
Action: RunSSHKeyAdd,
|
|
Flags: append([]cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "title",
|
|
Aliases: []string{"t"},
|
|
Usage: "Title for the key (defaults to the filename without extension)",
|
|
},
|
|
}, flags.LoginOutputFlags...),
|
|
}
|
|
|
|
// RunSSHKeyAdd reads a public key file and registers it with the Gitea instance
|
|
func RunSSHKeyAdd(_ stdctx.Context, cmd *cli.Command) error {
|
|
ctx, err := context.InitCommand(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if ctx.Args().Len() < 1 {
|
|
return fmt.Errorf("key file path is required")
|
|
}
|
|
|
|
keyFile := ctx.Args().First()
|
|
keyBytes, err := os.ReadFile(keyFile)
|
|
if err != nil {
|
|
return fmt.Errorf("could not read key file '%s': %w", keyFile, err)
|
|
}
|
|
|
|
keyContent := strings.TrimSpace(string(keyBytes))
|
|
if keyContent == "" {
|
|
return fmt.Errorf("key file '%s' is empty", keyFile)
|
|
}
|
|
|
|
title := ctx.String("title")
|
|
if title == "" {
|
|
base := filepath.Base(keyFile)
|
|
title = strings.TrimSuffix(base, filepath.Ext(base))
|
|
}
|
|
|
|
key, _, err := ctx.Login.Client().CreatePublicKey(gitea.CreateKeyOption{
|
|
Title: title,
|
|
Key: keyContent,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("Key '%s' (id: %d) added successfully.\n", key.Title, key.ID)
|
|
return nil
|
|
}
|