stillbox/internal/common/common.go
2024-11-24 10:38:19 -05:00

65 lines
788 B
Go

package common
import (
"github.com/urfave/cli/v2"
)
const (
AppName = "stillbox"
EnvPrefix = "STILLBOX_"
)
const (
TimeFormat = "Jan 2 15:04:05"
)
type cmdOptions interface {
Options(*cli.Context) error
Execute() error
}
func Action(c cmdOptions) cli.ActionFunc {
return func(ctx *cli.Context) error {
err := c.Options(ctx)
if err != nil {
return err
}
return c.Execute()
}
}
func PtrTo[T any](t T) *T {
return &t
}
func NilIfZero[T comparable](val T) *T {
var zero T
if val == zero {
return nil
}
return &val
}
func ZeroIfNil[T any](v *T) T {
var zero T
if v == nil {
return zero
}
return *v
}
func DefaultIfNilOrZero[T comparable](v *T, def T) T {
if v == nil {
return def
}
var zero T
if *v == zero {
return def
}
return *v
}