ini.go 996 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package ini
  2. import (
  3. "io"
  4. "os"
  5. "github.com/aws/aws-sdk-go/aws/awserr"
  6. )
  7. // OpenFile takes a path to a given file, and will open and parse
  8. // that file.
  9. func OpenFile(path string) (Sections, error) {
  10. f, err := os.Open(path)
  11. if err != nil {
  12. return Sections{}, awserr.New(ErrCodeUnableToReadFile, "unable to open file", err)
  13. }
  14. defer f.Close()
  15. return Parse(f)
  16. }
  17. // Parse will parse the given file using the shared config
  18. // visitor.
  19. func Parse(f io.Reader) (Sections, error) {
  20. tree, err := ParseAST(f)
  21. if err != nil {
  22. return Sections{}, err
  23. }
  24. v := NewDefaultVisitor()
  25. if err = Walk(tree, v); err != nil {
  26. return Sections{}, err
  27. }
  28. return v.Sections, nil
  29. }
  30. // ParseBytes will parse the given bytes and return the parsed sections.
  31. func ParseBytes(b []byte) (Sections, error) {
  32. tree, err := ParseASTBytes(b)
  33. if err != nil {
  34. return Sections{}, err
  35. }
  36. v := NewDefaultVisitor()
  37. if err = Walk(tree, v); err != nil {
  38. return Sections{}, err
  39. }
  40. return v.Sections, nil
  41. }