gitlink-cli/shortcuts/sshkey/sshkey.go

97 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sshkey
import (
"fmt"
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
)
func Shortcuts() []*common.Shortcut {
return []*common.Shortcut{
// +list列出当前账号的 SSH 公钥
{
Name: "list",
Description: "列出当前账号的 SSH 公钥",
Long: `List your SSH public keys.
Shows all SSH public keys associated with your account, including
key ID, title, fingerprint, and creation date.
Supports pagination with --page and --limit flags.`,
Example: ` # List all SSH keys
gitlink sshkey +list
# List with pagination
gitlink sshkey +list -p 2 -l 10`,
Flags: []common.Flag{
{Name: "page", Short: "p", Usage: "Page number", Default: "1"},
{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
},
Run: func(ctx *common.RuntimeContext) error {
env, err := ctx.CallAPI("GET", "/public_keys", nil)
if err != nil {
return err
}
return ctx.Output(env)
},
},
// +create添加新的 SSH 公钥
{
Name: "create",
Description: "添加新的 SSH 公钥",
Long: `Add a new SSH public key.
Registers a new SSH public key to your account. The key content must
be a valid public key in ssh-rsa or ssh-ed25519 format.`,
Example: ` # Add an SSH key
gitlink sshkey +create -t "My Laptop" -k "ssh-rsa AAAAB3..."`,
Flags: []common.Flag{
{Name: "title", Short: "t", Usage: "Key title", Required: true},
{Name: "key", Short: "k", Usage: "Public key content (ssh-rsa/ed25519 ...)", Required: true},
},
Run: func(ctx *common.RuntimeContext) error {
title, err := ctx.RequireArg("title")
if err != nil {
return err
}
key, err := ctx.RequireArg("key")
if err != nil {
return err
}
payload := map[string]string{
"title": title,
"key": key,
}
env, err := ctx.CallAPI("POST", "/public_keys", payload)
if err != nil {
return err
}
return ctx.Output(env)
},
},
// +delete删除指定的 SSH 公钥
{
Name: "delete",
Description: "删除指定的 SSH 公钥",
Long: `Delete an SSH public key.
Removes an SSH public key from your account by its ID. This action cannot be undone.`,
Example: ` # Delete an SSH key by ID
gitlink sshkey +delete -i 123`,
Flags: []common.Flag{
{Name: "id", Short: "i", Usage: "Key ID to delete", Required: true},
},
Run: func(ctx *common.RuntimeContext) error {
id, err := ctx.RequireArg("id")
if err != nil {
return err
}
env, err := ctx.CallAPI("DELETE", fmt.Sprintf("/public_keys/%s", id), nil)
if err != nil {
return err
}
return ctx.Output(env)
},
},
}
}