d715b053e7
Enables request-scoped cancellation, timeouts, and graceful shutdown for all database operations across API handlers, CLI commands, and TUI.
50 lines
923 B
Go
50 lines
923 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/atotto/clipboard"
|
|
"github.com/lerko/nib/internal/display"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var copyCmd = &cobra.Command{
|
|
Use: "copy <id>",
|
|
Short: "copy entity body to clipboard",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: runCopy,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(copyCmd)
|
|
}
|
|
|
|
func runCopy(cmd *cobra.Command, args []string) error {
|
|
store, err := openStore()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer store.Close()
|
|
|
|
id, err := store.Resolve(cmd.Context(), args[0])
|
|
if err != nil {
|
|
return fmt.Errorf("not_found — no entity with id %s", args[0])
|
|
}
|
|
|
|
e, err := store.Get(cmd.Context(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := clipboard.WriteAll(e.Body); err != nil {
|
|
return fmt.Errorf("clipboard: %w", err)
|
|
}
|
|
|
|
if err := store.IncrementUse(cmd.Context(), id); err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("(copied to clipboard) %s\n", display.FormatID(id))
|
|
return nil
|
|
}
|