flagger

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2025 License: MIT Imports: 13 Imported by: 4

README

set

GoDoc

Boilerplate utilities to set flags for logging, observability, etc.

Authors

  • Christophe Lambin

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultLog = Log{
	Level:  "info",
	Format: "text",
}
View Source
var DefaultProm = Prom{
	Addr: ":9100",
	Path: "/metrics",
}

Functions

func SetFlags

func SetFlags(flags *flag.FlagSet, configuration any)

SetFlags configures flags as per the configuration struct. Each exported field creates a flag by the field's name, in lowercase, updating the value of field, If a field is a struct, SetFlags recursively creates flags for that struct's fields.

SetFlags panics if configuration is not a pointer.

Example
package main

import (
	"bytes"
	"flag"
	"fmt"
	"time"

	"codeberg.org/clambin/go-common/flagger"
)

func main() {
	type config struct {
		String    string        `flagger.usage:"a string"` // flagger.usage sets usage text
		Int       int           `flagger.usage:"an int"`
		Float     float64       `flagger.usage:"a float"`
		Bool      bool          `flagger.name:"boolean" flagger.usage:"a bool"` // flagger.name overrides the name
		Duration  time.Duration `flagger.usage:"a duration"`
		Skipped   bool          `flagger.skip:"true"`
		SubStruct struct {
			String string `flagger.usage:"a substring"`
			Int    int    `flagger.name:"int" flagger.usage:"another int"`
		} `flagger.name:"sub"`
	}
	// in a real application, we would use flag.CommandLine directly
	f := flag.NewFlagSet("", flag.PanicOnError)
	flagger.SetFlags(f, &config{String: "foo", Int: 42, Float: 3.14, Bool: true, Duration: 1 * time.Second})
	var output bytes.Buffer
	f.SetOutput(&output)
	f.Usage()
	fmt.Println(output.String())

}
Output:

Usage:
  -boolean
    	a bool (default true)
  -duration duration
    	a duration (default 1s)
  -float float
    	a float (default 3.14)
  -int int
    	an int (default 42)
  -string string
    	a string (default "foo")
  -sub.int int
    	another int
  -sub.string string
    	a substring

Types

type Log

type Log struct {
	Level  string `flagger.usage:"log level"`
	Format string `flagger.usage:"log format"`
}

Log creates a new slog.Logger with the configured Level and Format (json/text). If `Level` is invalid, INFO is used. If `Format` is invalid, a slog.TextHandler will be created.

func (Log) Logger

func (l Log) Logger(w io.Writer, opts *slog.HandlerOptions) *slog.Logger

Logger returns a new slog.Logger that writes to w, configured as per opts, with the level and format set in Log{}. If opts is nil, the default options are used.

type Prom

type Prom struct {
	Addr string `flagger.usage:"prometheus listen address"`
	Path string `flagger.usage:"prometheus path"`
}

Prom runs a Prometheus scraper

func (Prom) Serve

func (p Prom) Serve(ctx context.Context) error

Jump to

Keyboard shortcuts

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