-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
3,061 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/iq |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[[constraint]] | ||
name = "github.com/go-ini/ini" | ||
version = "^1.38" | ||
|
||
[prune] | ||
go-tests = true | ||
unused-packages = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.PHONY: build | ||
build: | ||
go build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
# iq | ||
|
||
INI file query tool. | ||
|
||
|
||
## Installation | ||
|
||
$ go get github.com/djui/iq | ||
|
||
|
||
## Usage | ||
|
||
$ echo "[section]\nkey = value\n" | jq section.key - | ||
value | ||
|
||
$ jq section.key file.ini | ||
value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-ini/ini" | ||
) | ||
|
||
type query struct { | ||
Section string | ||
Key string | ||
} | ||
|
||
func main() { | ||
log.SetFlags(0) | ||
log.SetPrefix("") | ||
|
||
flag.Usage = func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s SECTION.KEY FILE\n", os.Args[0]) | ||
flag.PrintDefaults() | ||
} | ||
flag.Parse() | ||
|
||
if flag.NArg() == 0 { | ||
haltOnError(errors.New("missing query argument")) | ||
} else if flag.NArg() == 1 { | ||
haltOnError(errors.New("missing file argument")) | ||
} | ||
|
||
queryArg := flag.Arg(0) | ||
q, err := parseQuery(queryArg) | ||
haltOnError(err) | ||
|
||
fileArg := flag.Arg(1) | ||
b, err := readFile(fileArg) | ||
haltOnError(err) | ||
|
||
i, err := ini.Load(b) | ||
haltOnError(err) | ||
|
||
fmt.Println(i.Section(q.Section).Key(q.Key).String()) | ||
} | ||
|
||
func parseQuery(q string) (*query, error) { | ||
parts := strings.SplitN(q, ".", 2) | ||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||
return nil, errors.New("invalid query argument") | ||
} | ||
|
||
return &query{ | ||
Section: parts[0], | ||
Key: parts[1], | ||
}, nil | ||
} | ||
|
||
func readFile(f string) ([]byte, error) { | ||
if f == "-" { | ||
return ioutil.ReadAll(os.Stdin) | ||
} | ||
return ioutil.ReadFile(f) | ||
} | ||
|
||
func haltOnError(err error) { | ||
if err != nil { | ||
log.Fatalln("Error:", err) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.