loader.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2016 José Santos <henrique_1609@me.com>
  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 jet
  15. import (
  16. "errors"
  17. "io"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. )
  22. // Loader is a minimal interface required for loading templates.
  23. type Loader interface {
  24. // Open opens the underlying reader with template content.
  25. Open(name string) (io.ReadCloser, error)
  26. // Exists checks for template existence and returns full path.
  27. Exists(name string) (string, bool)
  28. }
  29. // hasAddPath is an optional Loader interface. Most probably useful for OS file system only, thus unexported.
  30. type hasAddPath interface {
  31. AddPath(path string)
  32. }
  33. // hasAddGopathPath is an optional Loader interface. Most probably useful for OS file system only, thus unexported.
  34. type hasAddGopathPath interface {
  35. AddGopathPath(path string)
  36. }
  37. // OSFileSystemLoader implements Loader interface using OS file system (os.File).
  38. type OSFileSystemLoader struct {
  39. dirs []string
  40. }
  41. // NewOSFileSystemLoader returns an initialized OSFileSystemLoader.
  42. func NewOSFileSystemLoader(paths ...string) *OSFileSystemLoader {
  43. return &OSFileSystemLoader{dirs: paths}
  44. }
  45. // Open opens a file from OS file system.
  46. func (l *OSFileSystemLoader) Open(name string) (io.ReadCloser, error) {
  47. return os.Open(name)
  48. }
  49. // Exists checks if the template name exists by walking the list of template paths
  50. // returns string with the full path of the template and bool true if the template file was found
  51. func (l *OSFileSystemLoader) Exists(name string) (string, bool) {
  52. for i := 0; i < len(l.dirs); i++ {
  53. fileName := path.Join(l.dirs[i], name)
  54. if _, err := os.Stat(fileName); err == nil {
  55. return fileName, true
  56. }
  57. }
  58. return "", false
  59. }
  60. // AddPath adds the path to the internal list of paths searched when loading templates.
  61. func (l *OSFileSystemLoader) AddPath(path string) {
  62. l.dirs = append(l.dirs, path)
  63. }
  64. // AddGopathPath adds a path located in the GOPATH.
  65. // Example: l.AddGopathPath("github.com/CloudyKit/jet/example/views")
  66. func (l *OSFileSystemLoader) AddGopathPath(path string) {
  67. paths := filepath.SplitList(os.Getenv("GOPATH"))
  68. for i := 0; i < len(paths); i++ {
  69. var err error
  70. path, err = filepath.Abs(filepath.Join(paths[i], "src", path))
  71. if err != nil {
  72. panic(errors.New("Can't add this path err: " + err.Error()))
  73. }
  74. if fstats, err := os.Stat(path); os.IsNotExist(err) == false && fstats.IsDir() {
  75. l.AddPath(path)
  76. return
  77. }
  78. }
  79. if fstats, err := os.Stat(path); os.IsNotExist(err) == false && fstats.IsDir() {
  80. l.AddPath(path)
  81. }
  82. }