pungi

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MPL-2.0 Imports: 8 Imported by: 0

README

pungi

Reusable config cobra command for viper-backed CLI tools.

import "github.com/flamingoosesoftwareinc/pungi"

root.AddCommand(pungi.NewCmd(pungi.Options{}))

Commands

myapp config list              # print all key/value pairs
myapp config get <key>         # print single value (dot-path)
myapp config set <key> <value> # write to config file
myapp config edit              # open in $EDITOR

Options

pungi.NewCmd(pungi.Options{
    Validate: func(key string) error { ... },  // reject unknown keys (default: accept all)
    Sort:     func(keys []string) { ... },      // custom sort for list output (default: alphabetical)
})

Documentation

Overview

Package pungi provides a reusable `config` cobra command for viper-backed CLI tools. Supports list, get, set, and edit subcommands with dot-path YAML navigation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptAll

func AcceptAll(_ string) error

AcceptAll is the default validator — accepts any key.

func Edit

func Edit() error

Edit opens the config file in $EDITOR.

func Get

func Get(w io.Writer, key string) error

Get prints a single config value to w.

func List

func List(w io.Writer, sorter KeySorter)

List prints all config key/value pairs to w, sorted by the provided sorter.

func NewCmd

func NewCmd(opts Options) *cobra.Command

NewCmd returns a `config` cobra.Command with list, get, set, edit subcommands.

func Set

func Set(key, value string) error

Set persists a key/value pair into the config FILE through a fresh viper instance loaded only from that file — never the global one. The global instance carries every cobra-bound flag, so writing it serializes flag state into the user's config, leaving a blank entry for every flag the command declared. A fresh instance never saw a flag: legitimate user config in any namespace round-trips untouched and flag keys cannot leak in. The global instance is updated in-memory afterwards so the running process stays coherent.

func SortAlphabetical

func SortAlphabetical(keys []string)

SortAlphabetical is the default sorter — alphabetical order.

Types

type KeySorter

type KeySorter func(keys []string)

KeySorter defines sort order for config keys in list output.

type KeyValidator

type KeyValidator func(key string) error

KeyValidator validates a config key before writing. Return an error to reject.

type Options

type Options struct {
	// Validate is called before set operations. Defaults to AcceptAll.
	Validate KeyValidator
	// Sort defines key ordering for list output. Defaults to SortAlphabetical.
	Sort KeySorter
}

Options configures the config command.

Jump to

Keyboard shortcuts

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