main.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "gogs.dmsc.dev/arp/arp_cli/cmd"
  9. "github.com/urfave/cli/v3"
  10. )
  11. func main() {
  12. // Create context with cancellation for graceful shutdown
  13. ctx, cancel := context.WithCancel(context.Background())
  14. defer cancel()
  15. // Handle interrupt signals
  16. sigChan := make(chan os.Signal, 1)
  17. signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
  18. go func() {
  19. <-sigChan
  20. cancel()
  21. }()
  22. // Build the CLI app
  23. app := buildApp()
  24. // If no arguments provided (only program name), start REPL mode
  25. if len(os.Args) <= 1 {
  26. repl := cmd.NewREPL(app)
  27. if err := repl.Run(ctx); err != nil {
  28. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  29. os.Exit(1)
  30. }
  31. return
  32. }
  33. // Otherwise, run single command mode
  34. if err := app.Run(ctx, os.Args); err != nil {
  35. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  36. os.Exit(1)
  37. }
  38. }
  39. func buildApp() *cli.Command {
  40. return &cli.Command{
  41. Name: "arp_cli",
  42. Usage: "Command-line interface for ARP (Agent-native ERP)",
  43. Description: `ARP CLI is a command-line tool for interacting with ARP GraphQL servers.
  44. It provides CRUD operations for Services, Users, Notes, Tasks, Messages,
  45. Roles, and Permissions. Real-time updates are available via WebSocket subscriptions
  46. for Tasks and Messages.
  47. When run without arguments, it starts an interactive REPL mode.
  48. Use 'exit' or 'quit' to leave the REPL, or Ctrl+D.
  49. First, login to an ARP server using 'login' to store your credentials.
  50. Then use the various commands to manage your ARP data.`,
  51. Version: "1.0.0",
  52. Flags: []cli.Flag{
  53. &cli.StringFlag{
  54. Name: "url",
  55. Aliases: []string{"u"},
  56. Usage: "ARP server URL",
  57. Sources: cli.EnvVars("ARP_URL"),
  58. },
  59. &cli.StringFlag{
  60. Name: "output",
  61. Aliases: []string{"o"},
  62. Usage: "Output format (table, json)",
  63. Value: "table",
  64. },
  65. },
  66. Commands: []*cli.Command{
  67. cmd.LoginCommand(),
  68. cmd.ConfigCommand(),
  69. cmd.ServiceCommand(),
  70. cmd.UserCommand(),
  71. cmd.NoteCommand(),
  72. cmd.TaskCommand(),
  73. cmd.MessageCommand(),
  74. cmd.RoleCommand(),
  75. cmd.PermissionCommand(),
  76. },
  77. Before: func(ctx context.Context, command *cli.Command) (context.Context, error) {
  78. // Store global flags in context for later use
  79. return ctx, nil
  80. },
  81. Action: func(ctx context.Context, cmd *cli.Command) error {
  82. // Show help when no subcommand is provided
  83. return cli.ShowAppHelp(cmd)
  84. },
  85. }
  86. }