cliTricks

package module
v0.0.0-...-3738d89 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2019 License: MIT Imports: 4 Imported by: 0

README

cliTricks

Custom written CLI tools for interfacing with a paginated API endpoint.

It consists of four binaries:

Installing

go get github.com/JackKnifed/cliTricks
  • Compile each binary
go install github.com/JackKnifed/cliTricks/templcat
go install github.com/JackKnifed/cliTricks/lasercat
go install github.com/JackKnifed/cliTricks/prettycat
go install github.com/JackKnifed/cliTricks/apicat
  • You should now be able to find all of the binaries at $GOPATH/bin

Testing

There are no unit tests or similar written for this - at least for the most part. THe binaries are provided as is, without proper testing.

This project is a hack, but it works.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BreakupArray

func BreakupArray(input string) []interface{}

func GetInt

func GetInt(data interface{}, target []interface{}) (int, error)
Example
testBytes := []byte(`{"Everything":"Awesome","Team":{"Everything":"Cool", "Solution": 63}}`)
var testData interface{}
err := json.Unmarshal(testBytes, &testData)
if err != nil {
	fmt.Printf("hit a snag unmarshalling the data - %v", err)
}

item, err := GetInt(testData, []interface{}{"Team", "Solution"})
if err != nil {
	fmt.Printf("hit a snag retrieving the item - %v", err)
	return
}
fmt.Println(item)
fmt.Println(reflect.TypeOf(item))
Output:

63
int

func GetItem

func GetItem(data interface{}, target []interface{}) (interface{}, error)
Example
testBytes := []byte(`{"Everything":"Awesome","Team":{"Everything":"Cool"}}`)
var testData interface{}
err := json.Unmarshal(testBytes, &testData)
if err != nil {
	fmt.Printf("hit a snag unmarshalling the data - %v", err)
}

item, err := GetItem(testData, []interface{}{"Team", "Everything"})
if err != nil {
	fmt.Printf("hit a snag retrieving the item - %v", err)
}
fmt.Println(item)
Output:

Cool

func SetItem

func SetItem(data interface{}, t []interface{}, val interface{}) (err error)
Example
var err error
testData := []byte(`
{
	"field1": {
		"field1.1": "novalue",
		"field1.2": {
			"field1.2.1": 1,
			"field1.2.2": "something"
		}
	},
	"field2": [[1,2],[3,4,5]]
}
`)
res := make(map[string]interface{})
if err := json.Unmarshal(testData, &res); err != nil {
	fmt.Fprintln(os.Stderr, "can't unmarshal json:", err)
	os.Exit(1)
}
testData, err = json.Marshal(res)
fmt.Println(string(testData))
if err := SetItem(res, []interface{}{"field1", "field1.1"}, "hello world"); err != nil {
	fmt.Fprintln(os.Stderr, "error:", err)
}
if err := SetItem(res, []interface{}{"field1", "field1.2", "field1.2.1"}, 2); err != nil {
	fmt.Fprintln(os.Stderr, "error:", err)
}
if err := SetItem(res, []interface{}{"field1", "field1.2", "field1.2.2"}, 3.14); err != nil {
	fmt.Fprintln(os.Stderr, "error:", err)
}
if err := SetItem(res, []interface{}{"field2", 0, 1}, 0xbeef); err != nil {
	fmt.Fprintln(os.Stderr, "error:", err)
}
if err := SetItem(res, []interface{}{"field2", 1}, 12345); err != nil {
	fmt.Fprintln(os.Stderr, "error:", err)
}
if testData, err = json.Marshal(res); err != nil {
	fmt.Fprintln(os.Stderr, "error:", err)
}
fmt.Println(string(testData))
Output:

{"field1":{"field1.1":"novalue","field1.2":{"field1.2.1":1,"field1.2.2":"something"}},"field2":[[1,2],[3,4,5]]}
{"field1":{"field1.1":"hello world","field1.2":{"field1.2.1":2,"field1.2.2":3.14}},"field2":[[1,48879],12345]}

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL