github.com/aavshr/aws-sdk-go@v1.41.3/internal/ini/ini.go (about)

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