Giter Site home page Giter Site logo

kasvith / teks Goto Github PK

View Code? Open in Web Editor NEW
41.0 3.0 3.0 22 KB

Easily get custom go template based outputs to your command-line tool. Like in docker/kubernetes

License: MIT License

Go 100.00%
output command cli table tabular formatter go-template golang go cli-app

teks's Introduction

teks : awesome outputs for your commands

Build Status Go Report Card GoDoc

teks brings painless output formating for your commands. Docker/Kubernetes provides custom formatting via go-templates. teks brings the power into any application providing a smooth intergration as a library.

teks is hevily inspired by Docker CLI

Install

teks is a go package. To use it execute

go get github.com/kasvith/teks

Available formatting options

Name Usage
json Output is formatted as JSON
jsonPretty Outputs a human-readable JSON with indented by 2 spaces
upper Convert string to uppercase
lower Convert string to lowercase
split Splits strings given by string and sep
join Joins strings by given separator
title Convert the first letter to uppercase of a string

Example

In this example we are going to printout details of few persons using teks.

package main

import (
	"flag"
	"fmt"
	"io"
	"os"
	"text/template"

	"github.com/kasvith/teks"
)

// Person represents a human being or whatever
type Person struct {
	Name    string
	Age     int
	Address string
}

func main() {
	var format string
	// default one will printout Name and Age in tabular format
	flag.StringVar(&format, "format", "table {{.Name}}\t{{.Age}}", "format of output")
	flag.Parse()

	// whatever data you have
	persons := []Person{
		{"Kasun", 24, "Earth"},
		{"John Doe", 34, "Somewhere on earth"},
		{"Spongebob", 30, "Under Sea"},
		{"Harry Potter", 30, "4 Privet Drive, Little Whinging, Surrey"},
	}

	// create new context
	ctx := teks.NewContext(os.Stdout, format)

	// create a renderer function to match signature defined in teks.Renderer
	renderer := func(w io.Writer, t *template.Template) error {
		for _, p := range persons {
			if err := t.Execute(w, p); err != nil {
				return err
			}
			_, _ = w.Write([]byte{'\n'})
		}
		return nil
	}

	// headers for table
	tableHeaders := map[string]string{
		"Age":     "Age",
		"Name":    "Name",
		"Address": "Address",
	}

	//override header functions if you want
	//teks.HeaderFuncs = template.FuncMap{
	//	"split": strings.Split,
	//}

	// execute context and write to our output
	if err := ctx.Write(renderer, tableHeaders); err != nil {
		fmt.Println("Error executing template:", err.Error())
	}
}

Now run program as follows

➜ go run simple.go 
Name                Age
Kasun               24
John Doe            34
Spongebob           30
Harry Potter        30

Let's pretty print Name and Address in tabular format

➜ go run simple.go --format "table {{.Name}}\t{{.Address}}"
Name                Address
Kasun               Earth
John Doe            Somewhere on earth
Spongebob           Under Sea
Harry Potter        4 Privet Drive, Little Whinging, Surrey

Let's make Name UPPERCASE

➜ go run simple.go --format "table {{upper .Name}}\t{{.Address}}"
NAME                Address
KASUN               Earth
JOHN DOE            Somewhere on earth
SPONGEBOB           Under Sea
HARRY POTTER        4 Privet Drive, Little Whinging, Surrey

You can change behavior of these headers by providing custom HeaderFuncs.

asciicast

Contribution

All contributions are welcome. Raise an Issue or a Pull Request

teks's People

Contributors

kasvith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

teks's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.