main.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package cmd
  2. import (
  3. "context"
  4. "log"
  5. "os"
  6. "github.com/0xJacky/Nginx-UI/internal/user"
  7. "github.com/0xJacky/Nginx-UI/internal/version"
  8. "github.com/urfave/cli/v3"
  9. )
  10. func NewAppCmd() *cli.Command {
  11. serve := false
  12. cmd := &cli.Command{
  13. Name: "nginx-ui",
  14. Usage: "Yet another Nginx Web UI",
  15. Commands: []*cli.Command{
  16. {
  17. Name: "serve",
  18. Usage: "Start the Nginx-UI server",
  19. Action: func(ctx context.Context, command *cli.Command) error {
  20. serve = true
  21. return nil
  22. },
  23. Flags: []cli.Flag{
  24. &cli.StringFlag{
  25. Name: "pidfile",
  26. Usage: "`PATH` to the PID file",
  27. Action: func(ctx context.Context, command *cli.Command, s string) error {
  28. // remove `pidfile` parameter from os.Args
  29. for i, arg := range os.Args {
  30. if arg == "--pidfile" || arg == "-p" {
  31. os.Args = append(os.Args[:i], os.Args[i+2:]...)
  32. break
  33. }
  34. }
  35. return nil
  36. },
  37. },
  38. },
  39. },
  40. {
  41. Name: "reset-password",
  42. Usage: "Reset the initial user password",
  43. Action: user.ResetInitUserPassword,
  44. },
  45. UpgradeDockerStep2Command,
  46. },
  47. Flags: []cli.Flag{
  48. &cli.StringFlag{
  49. Name: "config",
  50. Value: "app.ini",
  51. Usage: "configuration file path",
  52. },
  53. },
  54. DefaultCommand: "serve",
  55. Version: version.Version,
  56. }
  57. // Set the version printer
  58. cli.VersionPrinter = VersionPrinter
  59. if err := cmd.Run(context.Background(), os.Args); err != nil {
  60. log.Fatal(err)
  61. } else if !serve {
  62. os.Exit(0)
  63. }
  64. return cmd
  65. }