beego bee tool mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1KB

  1. // Copyright 2017 bee authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package utils
  15. import (
  16. "fmt"
  17. "os/exec"
  18. "strconv"
  19. "strings"
  20. "runtime"
  21. "github.com/beego/bee/config"
  22. )
  23. const appName = "Beego"
  24. func Notify(text, title string) {
  25. if !config.Conf.EnableNotification {
  26. return
  27. }
  28. switch runtime.GOOS {
  29. case "darwin":
  30. osxNotify(text, title)
  31. case "linux":
  32. linuxNotify(text, title)
  33. case "windows":
  34. windowsNotify(text, title)
  35. }
  36. }
  37. func osxNotify(text, title string) {
  38. var cmd *exec.Cmd
  39. if existTerminalNotifier() {
  40. cmd = exec.Command("terminal-notifier", "-title", appName, "-message", text, "-subtitle", title)
  41. } else if MacOSVersionSupport() {
  42. notification := fmt.Sprintf("display notification \"%s\" with title \"%s\" subtitle \"%s\"", text, appName, title)
  43. cmd = exec.Command("osascript", "-e", notification)
  44. } else {
  45. cmd = exec.Command("growlnotify", "-n", appName, "-m", title)
  46. }
  47. cmd.Run()
  48. }
  49. func windowsNotify(text, title string) {
  50. exec.Command("growlnotify", "/i:", "", "/t:", title, text).Run()
  51. }
  52. func linuxNotify(text, title string) {
  53. exec.Command("notify-send", "-i", "", title, text).Run()
  54. }
  55. func existTerminalNotifier() bool {
  56. cmd := exec.Command("which", "terminal-notifier")
  57. err := cmd.Start()
  58. if err != nil {
  59. return false
  60. }
  61. err = cmd.Wait()
  62. return err != nil
  63. }
  64. func MacOSVersionSupport() bool {
  65. cmd := exec.Command("sw_vers", "-productVersion")
  66. check, _ := cmd.Output()
  67. version := strings.Split(string(check), ".")
  68. major, _ := strconv.Atoi(version[0])
  69. minor, _ := strconv.Atoi(version[1])
  70. if major < 10 || (major == 10 && minor < 9) {
  71. return false
  72. }
  73. return true
  74. }