Skip to content
/ cli Public

Utility functions for running command line applications in Go.

License

Notifications You must be signed in to change notification settings

fraugster/cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

github.com/fraugster/cli

Build Status GoDoc License

Package cli implements utility functions for running command line applications.

The following simple features are currently implemented:

  • creating a context.Context which is closed when SIGINT, SIGQUIT or SIGTERM is received.
  • context aware reading lines from stdin into a channel
  • printing values using user a defined format (e.g. json, yml or table)

Motivation

CLI applications often perform similar tasks such as handling stop signals, reading from stdin and printing results to stdout. This package provides these functions in a single and coherent repository. Applications built with github.com/fraugster/cli treat context.Context as first class citizen to remain responsive and implement graceful shutdown. Naturally this is visible when consuming (multi-line) input from stdin. The cli.Print function is especially useful since it encourages developers to support multiple machine readable output formats which makes it easy to pipe the results of one application into another (e.g. my-app -o=json | jq …).

Installation

$ go get github.com/fraugster/cli

Usage

package main

import (
	"context"
	"flag"
	"fmt"
	"time"

	"github.com/fraugster/cli"
)

func main() {
	// Create a context that is done when SIGINT, SIGQUIT or SIGTERM is received
	ctx := cli.Context()

	// Let the user decide what output format she prefers.
	format := flag.String("output", "json", "Output format. One of json|yaml|table|raw")
	flag.Parse()

	// Reading a single line from stdin (returns "" if context is canceled).
	fmt.Print("Please insert your name: ")
	name := cli.ReadLine(ctx)
	fmt.Println("Hello", name)

	// Continuously read lines from stdin and return them in a channel.
	fmt.Println("You have 10 seconds to talk to me")
	ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
	defer cancel()

	var inputs []string
	for line := range cli.ReadLines(ctx) {
		inputs = append(inputs, line)
		fmt.Println("OK, go on")
	}
	fmt.Println("Time is up")
    
	// Print something to stdout in a format specified by the user.
	fmt.Println("Your inputs were:")
	cli.Print(*format, inputs)
}

Dependencies

  • gopkg.in/yaml.v2 for YAML output
  • github.com/stretchr/testify to run unit tests

License

This package is licensed under the the BSD 3-Clause License. Please see the LICENSE file for details.

Contributing

Contributions are welcome but must be discussed first in a new github issue before Pull Requests will be accepted.

About

Utility functions for running command line applications in Go.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages