Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
djui committed Sep 9, 2018
1 parent 198b31c commit dcec6b8
Show file tree
Hide file tree
Showing 18 changed files with 3,061 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/iq
17 changes: 17 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Gopkg.toml
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: build
build:
go build
16 changes: 16 additions & 0 deletions README.md
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
73 changes: 73 additions & 0 deletions main.go
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)
}
}
6 changes: 6 additions & 0 deletions vendor/github.com/go-ini/ini/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/go-ini/ini/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

191 changes: 191 additions & 0 deletions vendor/github.com/go-ini/ini/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/go-ini/ini/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dcec6b8

Please sign in to comment.