Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage.out
examples/ini_file_parsing/ini_file_parsing
examples/inherited_ini_parsing/inherited_ini_parsing
examples/memory_ini_data_parsing/memory_ini_data_parsing
.idea
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## goini

[![Build Status](https://secure.travis-ci.org/zieckey/goini.png)](http://travis-ci.org/zieckey/goini) [![Coverage Status](https://img.shields.io/coveralls/zieckey/goini.svg)](https://coveralls.io/r/zieckey/goini?branch=master)
This repository has been forked from github.com/zieckey/goini

It fixes a bug (https://github.com/zieckey/goini/issues/4) and offers a method to retrieve the ini configuration sections as a list.

This is a Go package to interact with arbitrary INI.

Expand All @@ -22,15 +24,15 @@ I have not found any Golang open source code suitable for it. So I write it myse

## Importing

import github.com/zieckey/goini
import github.com/flevanti/goini

## Usage

### Example 1 : Parses an INI file

The simplest example code is :
```go
import github.com/zieckey/goini
import github.com/flevanti/goini

ini := goini.New()
err := ini.ParseFile(filename)
Expand Down
7 changes: 3 additions & 4 deletions examples/ini_file_parsing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"path/filepath"

"github.com/zieckey/goini"
"github.com/flevanti/goini"
)

func main() {
Expand All @@ -33,17 +33,16 @@ func main() {
fmt.Printf("sss size is invalid\n")
return
}

v, ok = ini.SectionGet("sss", "aa")
if !ok || v != "bb" {
fmt.Printf("sss/aa value is invalid\n")
return
}

v, ok = ini.SectionGet("sss", "appext")
if !ok || v != "ab=cd" {
fmt.Printf("sss/appext value is invalid\n")
return
}
}

3 changes: 2 additions & 1 deletion examples/memory_ini_data_parsing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ package main

import (
"fmt"
"github.com/zieckey/goini"

"github.com/flevanti/goini"
)

func f1() {
Expand Down
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/flevanti/goini

go 1.17

require github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869

require (
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
14 changes: 13 additions & 1 deletion ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ func (ini *INI) GetBool(key string) (bool, bool) {
return ini.SectionGetBool(DefaultSection, key)
}

// GetSectionsList returns the list of sections found in the parsed ini files
func (ini *INI) GetSectionsList() []string {
var l []string
for k := range ini.sections {
if k == "" {
continue
}
l = append(l, k)
}
return l
}

// SectionGet looks up a value for a key in a section
// and returns that value, along with a boolean result similar to a map lookup.
func (ini *INI) SectionGet(section, key string) (value string, ok bool) {
Expand Down Expand Up @@ -294,7 +306,7 @@ func (ini *INI) parseINI(data []byte, lineSep, kvSep string) error {
// Skip blank lines
continue
}
if ini.skipCommits && line[0] == ';' || line[0] == '#' {
if ini.skipCommits && (line[0] == ';' || line[0] == '#') {
// Skip comments
continue
}
Expand Down
4 changes: 3 additions & 1 deletion ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func Test1(t *testing.T) {
b, ok = ini.GetBool("debug")
assert.Equal(t, b, false)
assert.Equal(t, ok, true)


l := ini.GetSectionsList()
assert.Equal(t, len(l), 2)
}

func Test2(t *testing.T) {
Expand Down