init.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package common
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. )
  10. var (
  11. PrintVersion = flag.Bool("version", false, "Print the version of the program and exits.")
  12. PrintHelp = flag.Bool("help", false, "Print the help message and exits.")
  13. Port = flag.Int("port", 3000, "Specify the listening port. Default is 3000.")
  14. LogDir = flag.String("log-dir", "", "Specify the directory for log files.")
  15. )
  16. func printHelp() {
  17. fmt.Println(fmt.Sprintf("Message Pusher %s - Your all in one message push system.", Version))
  18. fmt.Println("Copyright (C) 2023 JustSong. All rights reserved.")
  19. fmt.Println("GitHub: https://github.com/songquanpeng/message-pusher")
  20. fmt.Println("Usage: message-pusher [options]")
  21. fmt.Println("Options:")
  22. flag.CommandLine.VisitAll(func(f *flag.Flag) {
  23. name := fmt.Sprintf("-%s", f.Name)
  24. usage := strings.Replace(f.Usage, "\n", "\n ", -1)
  25. fmt.Printf(" -%-14s%s\n", name, usage)
  26. })
  27. os.Exit(0)
  28. }
  29. func init() {
  30. flag.Parse()
  31. if *PrintVersion {
  32. fmt.Println(Version)
  33. os.Exit(0)
  34. }
  35. if *PrintHelp {
  36. printHelp()
  37. }
  38. if os.Getenv("SESSION_SECRET") != "" {
  39. SessionSecret = os.Getenv("SESSION_SECRET")
  40. }
  41. if os.Getenv("SQLITE_PATH") != "" {
  42. SQLitePath = os.Getenv("SQLITE_PATH")
  43. }
  44. if *LogDir != "" {
  45. var err error
  46. *LogDir, err = filepath.Abs(*LogDir)
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. if _, err := os.Stat(*LogDir); os.IsNotExist(err) {
  51. err = os.Mkdir(*LogDir, 0777)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. }
  56. }
  57. }