VictoriaMetrics/vendor/github.com/aws/aws-sdk-go-v2/internal/ini/ini.go

57 lines
1.2 KiB
Go
Raw Normal View History

2023-11-14 22:45:07 +01:00
// Package ini implements parsing of the AWS shared config file.
//
// Example:
// sections, err := ini.OpenFile("/path/to/file")
// if err != nil {
// panic(err)
// }
//
// profile := "foo"
// section, ok := sections.GetSection(profile)
// if !ok {
// fmt.Printf("section %q could not be found", profile)
// }
package ini
import (
"fmt"
"io"
"os"
2023-11-14 22:45:07 +01:00
"strings"
)
2023-11-14 22:45:07 +01:00
// OpenFile parses shared config from the given file path.
func OpenFile(path string) (sections Sections, err error) {
f, oerr := os.Open(path)
if oerr != nil {
return Sections{}, &UnableToReadFile{Err: oerr}
}
defer func() {
closeErr := f.Close()
if err == nil {
err = closeErr
} else if closeErr != nil {
err = fmt.Errorf("close error: %v, original error: %w", closeErr, err)
}
}()
return Parse(f, path)
}
2023-11-14 22:45:07 +01:00
// Parse parses shared config from the given reader.
func Parse(r io.Reader, path string) (Sections, error) {
contents, err := io.ReadAll(r)
if err != nil {
2023-11-14 22:45:07 +01:00
return Sections{}, fmt.Errorf("read all: %v", err)
}
2023-11-14 22:45:07 +01:00
lines := strings.Split(string(contents), "\n")
tokens, err := tokenize(lines)
if err != nil {
2023-11-14 22:45:07 +01:00
return Sections{}, fmt.Errorf("tokenize: %v", err)
}
2023-11-14 22:45:07 +01:00
return parse(tokens, path), nil
}