root.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "gogs.dmsc.dev/arp/arp_cli/client"
  7. "gogs.dmsc.dev/arp/arp_cli/config"
  8. "github.com/urfave/cli/v3"
  9. )
  10. var (
  11. // Global flags
  12. urlFlag = &cli.StringFlag{
  13. Name: "url",
  14. Aliases: []string{"u"},
  15. Usage: "ARP server URL",
  16. Sources: cli.EnvVars("ARP_URL"),
  17. }
  18. outputFlag = &cli.StringFlag{
  19. Name: "output",
  20. Aliases: []string{"o"},
  21. Usage: "Output format (table, json, quiet)",
  22. Value: "table",
  23. }
  24. )
  25. // GetClient creates a GraphQL client with the configured URL and token
  26. // Returns both the client and config for auth checking
  27. func GetClient(ctx context.Context, cmd *cli.Command) (*client.Client, *config.Config, error) {
  28. cfg, err := config.Load()
  29. if err != nil {
  30. return nil, nil, fmt.Errorf("failed to load config: %w", err)
  31. }
  32. // Command flag takes precedence, then config, then empty
  33. serverURL := cmd.String(urlFlag.Name)
  34. if serverURL == "" {
  35. serverURL = cfg.ServerURL
  36. }
  37. if serverURL == "" {
  38. return nil, cfg, fmt.Errorf("no server URL configured. Use --url flag or run 'arp_cli login' first")
  39. }
  40. c := client.New(serverURL)
  41. if cfg.Token != "" {
  42. c.SetToken(cfg.Token)
  43. }
  44. return c, cfg, nil
  45. }
  46. // RequireAuth ensures the user is authenticated
  47. func RequireAuth(cfg *config.Config) error {
  48. if cfg.Token == "" {
  49. return fmt.Errorf("not authenticated. Run 'arp_cli login' first")
  50. }
  51. return nil
  52. }
  53. // RootCommand returns the root CLI command
  54. func RootCommand() *cli.Command {
  55. return &cli.Command{
  56. Name: "arp_cli",
  57. Usage: "Command-line interface for ARP (Agent-native ERP) server",
  58. Description: `arp_cli is a command-line tool for interacting with the ARP GraphQL API.
  59. It provides CRUD operations for managing users, services, tasks, notes,
  60. messages, roles, and permissions. The CLI also supports real-time subscriptions
  61. for task and message events.
  62. Start by running 'arp_cli login' to authenticate with your ARP server.`,
  63. Flags: []cli.Flag{
  64. urlFlag,
  65. outputFlag,
  66. },
  67. Commands: []*cli.Command{
  68. LoginCommand(),
  69. ConfigCommand(),
  70. ServiceCommand(),
  71. UserCommand(),
  72. NoteCommand(),
  73. TaskCommand(),
  74. MessageCommand(),
  75. RoleCommand(),
  76. PermissionCommand(),
  77. WorkflowCommand(),
  78. },
  79. Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
  80. // Set default output format in context if needed
  81. return ctx, nil
  82. },
  83. Action: func(ctx context.Context, cmd *cli.Command) error {
  84. // Show help when no subcommand is provided
  85. return cli.ShowAppHelp(cmd)
  86. },
  87. }
  88. }
  89. // Run executes the CLI
  90. func Run() error {
  91. cmd := RootCommand()
  92. return cmd.Run(context.Background(), os.Args)
  93. }