root.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. func GetClient(ctx context.Context, cmd *cli.Command) (*client.Client, error) {
  27. cfg, err := config.Load()
  28. if err != nil {
  29. return nil, fmt.Errorf("failed to load config: %w", err)
  30. }
  31. // Command flag takes precedence, then config, then empty
  32. serverURL := cmd.String(urlFlag.Name)
  33. if serverURL == "" {
  34. serverURL = cfg.ServerURL
  35. }
  36. if serverURL == "" {
  37. return nil, fmt.Errorf("no server URL configured. Use --url flag or run 'arp_cli login' first")
  38. }
  39. c := client.New(serverURL)
  40. if cfg.Token != "" {
  41. c.SetToken(cfg.Token)
  42. }
  43. return c, nil
  44. }
  45. // RequireAuth ensures the user is authenticated
  46. func RequireAuth(cfg *config.Config) error {
  47. if cfg.Token == "" {
  48. return fmt.Errorf("not authenticated. Run 'arp_cli login' first")
  49. }
  50. return nil
  51. }
  52. // RootCommand returns the root CLI command
  53. func RootCommand() *cli.Command {
  54. return &cli.Command{
  55. Name: "arp_cli",
  56. Usage: "Command-line interface for ARP (Agent-native ERP) server",
  57. Description: `arp_cli is a command-line tool for interacting with the ARP GraphQL API.
  58. It provides CRUD operations for managing users, services, tasks, notes, channels,
  59. messages, roles, and permissions. The CLI also supports real-time subscriptions
  60. for task and message events.
  61. Start by running 'arp_cli login' to authenticate with your ARP server.`,
  62. Flags: []cli.Flag{
  63. urlFlag,
  64. outputFlag,
  65. },
  66. Commands: []*cli.Command{
  67. LoginCommand(),
  68. ConfigCommand(),
  69. ServiceCommand(),
  70. UserCommand(),
  71. NoteCommand(),
  72. TaskCommand(),
  73. ChannelCommand(),
  74. MessageCommand(),
  75. RoleCommand(),
  76. PermissionCommand(),
  77. },
  78. Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
  79. // Set default output format in context if needed
  80. return ctx, nil
  81. },
  82. Action: func(ctx context.Context, cmd *cli.Command) error {
  83. // Show help when no subcommand is provided
  84. return cli.ShowAppHelp(cmd)
  85. },
  86. }
  87. }
  88. // Run executes the CLI
  89. func Run() error {
  90. cmd := RootCommand()
  91. return cmd.Run(context.Background(), os.Args)
  92. }