clicontext

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: Apache-2.0 Imports: 4 Imported by: 7

Documentation

Overview

Package clicontext contains utilities for using context.Context in command-line-interfaces.

Example
package main

import (
	"context"
	"fmt"
	"os"

	"htdvisser.dev/exp/clicontext"
)

func main() {
	var app interface {
		Run(ctx context.Context, args ...string) error
	}

	// func main()
	{
		ctx, exit := clicontext.WithInterruptAndExit(context.Background())
		defer exit()

		if err := app.Run(ctx, os.Args[1:]...); err != nil {
			fmt.Fprintln(os.Stderr, err)
			return
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetExitCode

func GetExitCode(ctx context.Context) (code int, ok bool)

GetExitCode returns the exit code from the context.

func SetExitCode

func SetExitCode(ctx context.Context, code int) bool

SetExitCode sets the exit code into the context and returns true if it did so successfully.

func WithExitCode

func WithExitCode(parent context.Context, dst *int) context.Context

WithExitCode returns a context that can be used to set exit code

func WithInterrupt

func WithInterrupt(parent context.Context, extraInterruptSignals ...os.Signal) context.Context

WithInterrupt returns a copy of parent with a new Done channel, which is closed when the process receives an interrupt signal or when the parent context's Done channel is closed. The optional extra interrupt signals are added to the default interrupt signals.

func WithInterruptAndExit

func WithInterruptAndExit(parent context.Context, extraInterruptSignals ...os.Signal) (ctx context.Context, exit func())

WithInterruptAndExit is similar to WithInterrupt, but also returns a func that exits the program with an appropriate status code for the interrupt signal.

func WithTemporaryInterrupt

func WithTemporaryInterrupt(parent context.Context, extraInterruptSignals ...os.Signal) (context.Context, context.CancelFunc)

WithTemporaryInterrupt is similar to WithInterrupt, but also releases resources when the returned context.CancelFunc is called.

Types

This section is empty.

Jump to

Keyboard shortcuts

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