fj/cmd/whoami.go
sid 17ca49d0c5 feat: add branch, notification, org, open, whoami commands
Ports five commands from tea that fgj-sid was missing:

- fgj branch {list,rename,delete} — list branches with protection
  status, rename, and delete with confirmation.
- fgj notification {list,read} — list user notifications (unread by
  default, --all for everything), mark individual threads read.
- fgj org {list,create,delete} — manage organizations on the host.
  Create accepts --description/--full-name/--website/--location and
  --visibility (public/limited/private).
- fgj open [number] — open the repo, issue, or PR in a browser.
  Auto-detects issue-vs-PR via GetIssue. Falls back to printing the
  URL when stdout is not a TTY or --url is passed.
- fgj whoami — display authenticated user + host.

All commands follow the established pattern (parseRepo + config.Load +
api.NewClientFromConfig + ios), support --json where list semantics
apply, and share a new loadClient helper for host-scoped (non-repo)
commands. Tested live against forgejo.zerova.net.

Refs audit recommendation.md §'v0.5.0 — Missing resources'.
2026-04-19 21:27:55 -06:00

53 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var whoamiCmd = &cobra.Command{
Use: "whoami",
Short: "Show the authenticated user on the current host",
Long: "Display login, full name, and email for the authenticated user on the active host.",
Example: ` # Show who you are on the active host
fgj whoami
# On a specific host
fgj whoami --hostname forgejo.example.com
# As JSON
fgj whoami --json`,
RunE: runWhoami,
}
func init() {
rootCmd.AddCommand(whoamiCmd)
addJSONFlags(whoamiCmd, "Output as JSON")
}
func runWhoami(cmd *cobra.Command, args []string) error {
client, err := loadClient()
if err != nil {
return err
}
user, _, err := client.GetMyUserInfo()
if err != nil {
return fmt.Errorf("failed to fetch current user: %w", err)
}
if wantJSON(cmd) {
return outputJSON(cmd, user)
}
fmt.Fprintf(ios.Out, "%s\n", user.UserName)
if user.FullName != "" && user.FullName != user.UserName {
fmt.Fprintf(ios.Out, " name: %s\n", user.FullName)
}
if user.Email != "" {
fmt.Fprintf(ios.Out, " email: %s\n", user.Email)
}
fmt.Fprintf(ios.Out, " host: %s\n", client.Hostname())
return nil
}