output

package
v0.0.0-...-577f95d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: MIT Imports: 15 Imported by: 14

Documentation

Index

Examples

Constants

View Source
const (
	FlagOutput = "output"
	FlagFields = "fields"
	FlagLimit  = "limit"
	FlagFollow = "follow"

	FieldsLong = "long"
)
View Source
const (
	BatchPrintSize = 100
)

Variables

View Source
var (
	UsageText = fmt.Sprintf("format output as: %v, %v, %v.", Table, JSON, Card)
)

Functions

func ParseToJSON

func ParseToJSON(o interface{}, indent bool) (string, error)

func PrintCards

func PrintCards(c *cli.Context, w io.Writer, items []interface{}, opts *PrintOptions) error
Example
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package main

import (
	"flag"
	"os"

	"github.com/temporalio/tctl-kit/pkg/output"
	"github.com/urfave/cli/v2"
)

func main() {
	ctx, teardown := setupCardTest()
	defer teardown()

	structItems := []*dataForCard{
		{
			Name: "foo1",
			Nested: struct {
				NName  string
				NValue string
			}{
				NName:  "baz1",
				NValue: "qux1",
			},
		},
	}

	var items []interface{}
	for _, item := range structItems {
		items = append(items, item)
	}

	po := output.PrintOptions{
		Fields:     []string{"Name"},
		FieldsLong: []string{"Nested.NName", "Nested.NValue"},
	}

	output.PrintCards(ctx, os.Stdout, items, &po)

}

func setupCardTest() (*cli.Context, func()) {
	app := cli.NewApp()
	flagSet := flag.FlagSet{}
	ctx := cli.NewContext(app, &flagSet, nil)

	return ctx, func() {}
}

type dataForCard struct {
	Name   string
	Nested struct {
		NName  string
		NValue string
	}
}
Output:

Name  foo1

func PrintItems

func PrintItems(c *cli.Context, items []interface{}, opts *PrintOptions) error

PrintItems prints items based on user flags or print options.

func PrintIterator

func PrintIterator(c *cli.Context, iter iterator.Iterator[interface{}], opts *PrintOptions) error

PrintIterator prints items from an iterator based on user flags or print options.

func PrintJSON

func PrintJSON(c *cli.Context, w io.Writer, o interface{}) error

func PrintTable

func PrintTable(c *cli.Context, w io.Writer, items []interface{}, opts *PrintOptions) error
Example
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package main

import (
	"flag"
	"os"

	"github.com/temporalio/tctl-kit/pkg/output"
	"github.com/urfave/cli/v2"
)

type dataForTable struct {
	Name   string
	Value  string
	Nested struct {
		NName  string
		NValue string
	}
}

func setupTableTest() (*cli.Context, func()) {
	app := cli.NewApp()
	flagSet := flag.FlagSet{}
	ctx := cli.NewContext(app, &flagSet, nil)

	return ctx, func() {}
}

func main() {
	ctx, teardown := setupTableTest()
	defer teardown()

	structItems := []*dataForTable{
		{
			Name:  "foo1",
			Value: "bar1",
			Nested: struct {
				NName  string
				NValue string
			}{
				NName:  "baz1",
				NValue: "qux1",
			},
		},
	}

	var items []interface{}
	for _, item := range structItems {
		items = append(items, item)
	}

	po := output.PrintOptions{
		Fields:   []string{"Name", "Value", "Nested.NName", "Nested.NValue"},
		NoHeader: true,
	}

	output.PrintTable(ctx, os.Stdout, items, &po)

}
Output:

foo1  bar1  baz1  qux1

Types

type OutputOption

type OutputOption string
const (
	Table OutputOption = "table"
	JSON  OutputOption = "json"
	Card  OutputOption = "card"
)

type PrintOptions

type PrintOptions struct {
	// Fields is a list of fields to print
	Fields []string
	// FieldsLong is a list of additional fields to print with "--fields long" flag
	FieldsLong []string
	// ForceFields ignores user provided fields and uses print options instead. Useful when printing secondary data
	ForceFields bool
	// OutputFormat is the output format to use: table, json..
	OutputFormat OutputOption
	// Pager is the pager to use for interactive mode. Default - stdout
	Pager pager.PagerOption
	// NoHeader removes the header in the table output
	NoHeader bool
	// Separator to use in table output
	Separator string
}

Jump to

Keyboard shortcuts

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