odoo.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package odoo
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. S "bitbucket.org/robert2206/automation/modules/settings"
  11. "github.com/flosch/pongo2"
  12. )
  13. var (
  14. matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
  15. matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
  16. matchAllSpaces = regexp.MustCompile("(\\s*)")
  17. )
  18. // Create odoo instance
  19. func Create(odooName string) error {
  20. // 1. snakeize name
  21. odooName = snakeizeName(odooName)
  22. log.Printf("snakeize odoo name: %v", odooName)
  23. // 2. build odoo path
  24. path := filepath.Join(S.Ctx.OdooRootPath, odooName)
  25. log.Printf("odoo path builded: %v", path)
  26. // 3. check if odoo path exists
  27. pathExists := existsPath(path)
  28. if pathExists {
  29. log.Printf("odoo path existence: %v", pathExists)
  30. return errors.New("odoo path exists")
  31. }
  32. log.Printf("odoo path existence: %v", pathExists)
  33. // 4. make odoo path
  34. err := makePath(odooName)
  35. if err != nil {
  36. return err
  37. }
  38. // 5. make odoo default paths
  39. err = makeDefaultPaths(odooName)
  40. if err != nil {
  41. log.Fatalf("odoo default paths cannot be created: %v", err.Error())
  42. return err
  43. }
  44. log.Printf("odoo default paths is created")
  45. // 6. make openerp-server.conf
  46. err = makeConfiguration(odooName)
  47. if err != nil {
  48. log.Fatalf("odoo configuration cannot be created: %v", err.Error())
  49. return err
  50. }
  51. log.Printf("odoo configuration is created")
  52. return nil
  53. }
  54. func snakeizeName(name string) string {
  55. snakeCase := matchFirstCap.ReplaceAllString(name, "${1}_${2}")
  56. snakeCase = matchAllCap.ReplaceAllString(snakeCase, "${1}_${2}")
  57. snakeCase = matchAllSpaces.ReplaceAllString(snakeCase, "")
  58. return strings.ToLower(snakeCase)
  59. }
  60. func existsPath(path string) bool {
  61. _, err := os.Stat(path)
  62. return err == nil
  63. }
  64. func makePath(odooName string) error {
  65. path := filepath.Join(S.Ctx.OdooRootPath, odooName)
  66. return os.Mkdir(path, 0777)
  67. }
  68. func makeDefaultPaths(odooName string) error {
  69. paths := [3]string{"custom-addons", "config", "files"}
  70. for i := 0; i < len(paths); i++ {
  71. path := filepath.Join(S.Ctx.OdooRootPath, odooName, paths[i])
  72. err := os.Mkdir(path, 0777)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. return nil
  78. }
  79. func makeConfiguration(odooName string) error {
  80. tmpl, err := pongo2.FromFile("data/templates/openerp-server.tpl")
  81. if err != nil {
  82. return err
  83. }
  84. tmpl = pongo2.Must(tmpl, err)
  85. out, _ := tmpl.Execute(pongo2.Context{
  86. "admin_password": "admin",
  87. "db_host": "127.0.0.1",
  88. "db_port": "8069",
  89. "db_name": "odoo",
  90. "db_user": "odoo",
  91. })
  92. confPath := filepath.Join(S.Ctx.OdooRootPath, odooName, "config", "openerp-server.conf")
  93. return ioutil.WriteFile(confPath, []byte(out), 0777)
  94. }