~ashkeel/kilovolt-client-go

05d718bc5c6bed1f85b22a25d6df647e4f54a7f6 — Ash Keel 3 years ago c11f467
Add sample cli tool
1 files changed, 54 insertions(+), 0 deletions(-)

A cmd/kvcli/main.go
A cmd/kvcli/main.go => cmd/kvcli/main.go +54 -0
@@ 0,0 1,54 @@
package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"strings"

	kvclient "github.com/strimertul/kilovolt-client-go"
)

func check(err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "Fatal error: %s\n", err.Error())
		os.Exit(1)
	}
}

func main() {
	endpoint := flag.String("endpoint", "http://localhost:4338", "Address:port to connect to")
	auth := flag.String("auth", "", "Optional Authorization string (for stulbe)")
	command := flag.String("command", "", "Command to run (supported: kget/kset)")
	key := flag.String("key", "", "Key to run command on")
	data := flag.String("data", "", "Optional data argument for commands that require it")
	flag.Parse()

	if *command == "" {
		check(fmt.Errorf("must specify a valid -command"))
	}
	if *key == "" {
		check(fmt.Errorf("must specify a valid -key"))
	}

	headers := http.Header{}
	if *auth != "" {
		headers.Add("Authorization", "Bearer "+*auth)
	}

	client, err := kvclient.NewClient(*endpoint, kvclient.ClientOptions{Headers: headers})
	check(err)

	switch strings.ToLower(*command) {
	case "kget":
		str, err := client.GetKey(*key)
		check(err)
		fmt.Println(str)
	case "kset":
		check(client.SetKey(*key, *data))
	default:
		check(fmt.Errorf("unknown command \"%s\"", *command))
	}

}