stillbox/internal/common/common.go

55 lines
826 B
Go
Raw Normal View History

2024-07-14 13:47:48 -04:00
package common
import (
2024-10-31 00:10:53 -04:00
"fmt"
"strconv"
2024-07-14 13:47:48 -04:00
"github.com/spf13/cobra"
)
2024-10-22 08:39:15 -04:00
const (
TimeFormat = "Jan 2 15:04:05"
)
2024-07-14 13:47:48 -04:00
type cmdOptions interface {
Options(*cobra.Command, []string) error
Execute() error
}
func RunE(c cmdOptions) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
err := c.Options(cmd, args)
if err != nil {
cmd.SilenceUsage = true
return err
}
err = c.Execute()
if err != nil {
cmd.SilenceUsage = true
}
return err
}
}
2024-07-27 19:25:16 -04:00
func PtrTo[T any](t T) *T {
return &t
}
func PtrOrNull[T comparable](val T) *T {
var zero T
if val == zero {
return nil
}
return &val
}
2024-10-31 00:10:53 -04:00
func FmtFloat(v float64, places ...int) string {
if len(places) > 0 {
return fmt.Sprintf("%."+strconv.Itoa(places[0])+"f", v)
}
return fmt.Sprintf("%.4f", v)
}