macutils.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain 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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package macutils
  15. import (
  16. "encoding/xml"
  17. "fmt"
  18. )
  19. type SPlist struct {
  20. XMLName xml.Name `xml:"plist"`
  21. Dict SDict `xml:"dict"`
  22. }
  23. type SDict struct {
  24. Key []string `xml:"key"`
  25. String []string `xml:"string"`
  26. }
  27. func ParsePlist(sinfo []byte) map[string]string {
  28. v := &SPlist{}
  29. ret := map[string]string{}
  30. if err := xml.Unmarshal(sinfo, v); err != nil {
  31. return ret
  32. }
  33. lenK := len(v.Dict.Key)
  34. lenS := len(v.Dict.String)
  35. if lenK > lenS {
  36. lenK = lenS
  37. }
  38. for i := 0; i < lenK; i++ {
  39. ret[v.Dict.Key[i]] = v.Dict.String[i]
  40. }
  41. return ret
  42. }
  43. func LaunchdRun(label, script string) string {
  44. content := `<?xml version="1.0" encoding="UTF-8"?>
  45. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  46. <plist version="1.0">
  47. <dict>
  48. <key>Label</key>
  49. <string>%s</string>
  50. <key>ProgramArguments</key>
  51. <array>
  52. <string>/bin/sh</string>
  53. <string>%s</string>
  54. </array>
  55. <key>RunAtLoad</key>
  56. <true/>
  57. <key>KeepAlive</key>
  58. <dict>
  59. <key>SuccessfulExit</key>
  60. <false/>
  61. <key>OtherJobEnabled</key>
  62. <string>com.apple.opendirectoryd</string>
  63. </dict>
  64. </dict>
  65. </plist>
  66. `
  67. return fmt.Sprintf(content, label, script)
  68. }