pflagheaders

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 4 Imported by: 0

README

HTTP Headers for spf13/pflag

fgm codecov Go Report Card OpenSSF Scorecard

This package allows use of repeated CLI flags defining HTTP headers, like:

CLI usage

# Long format
mycommand --header "Accept: text/plain" --header "Authorization: bearer sometoken"

# Short format
mycommand -H "Accept: text/plain" -H "Authorization: bearer sometoken"

# Repeated headers are supported and combined
mycommand -H "X-Array-Header: value1" -H "X-Array-Header: value2"
# Will return a slice value with value1 and value2 for key  X-Array-Header

# Headers are canonicalized
mycommand -H "content-type: application/json"
# Will have key Content-Type

Code usage

Simple
package main

import (
	"fmt"

	"github.com/spf13/pflag"
	"github.com/fgm/pflagheaders"
)

func main() {
	// HeaderFlag provides a preconfigured default flag
	h := pflagheaders.HeaderFlag()
	pflag.Parse()

	fmt.Printf("Headers:\n%s\n", h)
	// The resulting http.Header is available after Parse:
	fmt.Printf("Inner header:\n%#v\n", h.Header)
}
In projects using spf13/cobra
// In cmd/root.go
package cmd

import (
	pfh "github.com/fgm/pflagheaders"
	"github.com/spf13/cobra" // and others
)

var header = &pfh.Header{}

var rootCmd = &cobra.Command{
	/* generated by Cobra */
	Run: func(_ *cobra.Command, _ []string) { fmt.Println(h.Header) }
}

func init() {
	cobra.OnInitialize(initConfig) // Generated by Cobra
	// ...other lines generated by Cobra
	// You can use the provided constants, or use other values.
	rootCmd.Flags().VarP(header, pfh.NameLong, pfh.NameShort, pfh.Help)
}

Documentation

Index

Constants

View Source
const Help = `` /* 127-byte string literal not displayed */

Help is the recommended help string for header flags.

View Source
const NameLong = "header"

NameLong is the recommanded long name for a header flag.

View Source
const NameShort = "H"

NameShort is the recommended short name for a header flag.

View Source
const Type = "stringSlice"

Type is the type expected by spf13/pflag for these data.

Variables

View Source
var ErrFormat = errors.New(`header value is not a "name: value" string`)

ErrFormat is returned on ill-formatted header flag values.

Functions

func HeaderFlagP

func HeaderFlagP(header *Header)

HeaderFlagP initializes a global default header flag.

Types

type Header struct {
	http.Header
}

Header supports parsing HTTP headers as CLI arguments using spf13/pflag.

If the same header is passed multiple times on the CLI, the values are aggregated under the header key, not replaced.

func HeaderFlag

func HeaderFlag() *Header

HeaderFlag returns a global default header flag.

func (*Header) Set

func (h *Header) Set(s string) error

Set is part of pflag.Value.

func (Header) String

func (h Header) String() string

String implements fmt.Stringer and part of pflag.Value.

It needs to have a value receiver to work on both header and *header.

func (Header) Type

func (h Header) Type() string

Type is part of pflag.Value.

Jump to

Keyboard shortcuts

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