Help us shape the future of Go! Take the 2026 Go Developer Survey

cli

module
v1.0.1002 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT

README

Go Module CLI

Create golang cli application, include powerful configuration. this cli based on

# cli library 
github.com/urfave/cli/v2
# validator library
github.com/go-playground/validator/v10

Installation

go get code.afis.me/modules/go/cli
Upgrading to the latest version
go get -u code.afis.me/modules/go/cli
Upgrade or downgrade with tag version if available
go get -u code.afis.me/modules/go/cli@v1.0.0

Auto parsing struct configuration

var ConfigManual = map[string]string{
	"listener:host": `listener address, must be valid rules hostname_rfc1123
	ex: localhost, 127.0.0.1, service.domain, 10.10.10.10, etc.`,
	"listener:port:insecure": `listener port, must be valid port number
	this port use for http or gprc with insecure option`,
	"listener:port:secure": `listener port, must be valid port number
	this port use for https or gprc with secure option`,
	"listener:disable:secure": `force disable https or grpc with secure option`,
	"listener:origin:allow": `allow origin headers, only valid with http transport and grpc-web.
	value must be regex, ex: 
	- (?:^|\s)((https?:\/\/)?(?:localhost|[\w-]+(?:\.[\w-]+)+)(:\d+)?(\/\S*)?)
	- .*\.afis\.co\.id`,
	"listener:origin:disallow": `disallow origin headers, only valid with http transport and grpc-web.
	same rules with --listener-origin-allow`,
}

// options
type Options struct {
    Manual   map[string]string `cli:"manual"`
    Listener struct {
        Host string `yaml:"host" default:"0.0.0.0" desc:"listener:host" validate:"required,hostname_rfc1123"`
        Port struct {
            Insecure int `yaml:"insecure" default:"3310" desc:"listener:port:insecure" validate:"required,max=65535"`
            Secure   int `yaml:"secure"  default:"3311" desc:"listener:port:secure" validate:"required,max=65535,nefield=Insecure"`
        } `yaml:"port"`
        Disable struct {
            Secure bool `yaml:"secure" desc:"listener:disable:secure"`
        } `yaml:"disable"`
        Origin struct {
            Allow    []string `yaml:"allow" desc:"listener:origin:allow"`
            Disallow []string `yaml:"disallow" desc:"listener:origin:disallow"`
        } `yaml:"origin"`
    } `yaml:"listener"`
}

To this cli argument options

GLOBAL OPTIONS:
   --listener-host string             (default: "0.0.0.0") [$listener_host, $LISTENER_HOST]
                                      listener address, must be valid rules hostname_rfc1123
                                      ex: localhost, 127.0.0.1, service.domain, 10.10.10.10, etc.
   --listener-port-insecure int       (default: 3310) [$listener_port_insecure, $LISTENER_PORT_INSECURE]
                                      listener port, must be valid port number
                                      this port use for http or gprc with insecure option
   --listener-port-secure int         (default: 3311) [$listener_port_secure, $LISTENER_PORT_SECURE]
                                      listener port, must be valid port number
                                      this port use for https or gprc with secure option
   --listener-disable-secure          (default: false) [$listener_disable_secure, $LISTENER_DISABLE_SECURE]
                                      force disable https or grpc with secure option
   --listener-origin-allow string     (accepts multiple inputs) [$listener_origin_allow, $LISTENER_ORIGIN_ALLOW]
                                      allow origin headers, only valid with http transport and grpc-web.
                                      value must be regex, ex: 
                                      - (?:^|\s)((https?:\/\/)?(?:localhost|[\w-]+(?:\.[\w-]+)+)(:\d+)?(\/\S*)?)
                                      - .*\.afis\.co\.id
   --listener-origin-disallow string  (accepts multiple inputs) [$listener_origin_disallow, $LISTENER_ORIGIN_DISALLOW]
                                      disallow origin headers, only valid with http transport and grpc-web.
                                      same rules with --listener-origin-allow
   --help, -h                         (default: false)
                                      show help

Config type data support

string, []string
int, int16, int32, int64, []int, []int16, []int32, []int64
float32, float64, []float32, []float64

Use Vault as a secret engine

 VAULT_ADDRESS=https://vault.example.me \
 VAULT_TOKEN=token VAULT_VARIABLE_SECRET=production/config  \
 go run main.go

Use Vault as a config

config must be decoded to base64 (standard encoding), supported yaml and json

 VAULT_ADDRESS=https://vault.example.me \
 VAULT_TOKEN=token VAULT_VARIABLE_SECRET=production/config  \
 VAULT_CONFIG=production/config/services/rates:configkey \
 go run main.go
 
 # or you can use with command line argument
 VAULT_ADDRESS=https://vault.example.me \
 VAULT_TOKEN=token VAULT_VARIABLE_SECRET=production/config  \
 go run main.go --config vault:production/config/services/rates:configkey

Example config.yaml

listener:
  host: 0.0.0.0
  port:
    insecure: 3310
    secure: 3311
  origin:
    allow:
      - localhost
      - afis.me
proxy:
    primary:
        host: $(rates_proxy_primary) # get value from secret engine

Directories

Path Synopsis
lib

Jump to

Keyboard shortcuts

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