sq/cli/config/config.go

121 lines
3.3 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
// Package config holds CLI configuration.
package config
import (
"time"
"github.com/neilotoole/sq/drivers/userdriver"
"github.com/neilotoole/sq/libsq/errz"
"github.com/neilotoole/sq/libsq/notify"
"github.com/neilotoole/sq/libsq/source"
"github.com/neilotoole/sq/libsq/stringz"
)
// Config holds application config/session data.
type Config struct {
// Version is the config version. This will allow sq to
// upgrade config files if needed.
Version string `yaml:"version" json:"version"`
// Defaults contains default settings, such as output format.
Defaults Defaults `yaml:"defaults" json:"defaults"`
// Sources is the set of data sources.
Sources *source.Set `yaml:"sources" json:"sources"`
// Notifications holds notification config.
2020-08-06 20:58:47 +03:00
Notification *Notification `yaml:"notification" json:"notification"`
// Ext holds sq config extensions, such as user driver config.
Ext Ext `yaml:"-" json:"-"`
}
func (c *Config) String() string {
return stringz.SprintJSON(c)
}
// Ext holds additional config (extensions) loaded from other
// config files, e.g. ~/.config/sq/ext/*.sq.yml
type Ext struct {
UserDrivers []*userdriver.DriverDef `yaml:"user_drivers" json:"user_drivers"`
}
// Defaults contains sq default values.
type Defaults struct {
2020-08-06 20:58:47 +03:00
Timeout time.Duration `yaml:"timeout" json:"timeout"`
Format Format `yaml:"output_format" json:"output_format"`
Header bool `yaml:"output_header" json:"output_header"`
}
// Notification holds notification configuration.
type Notification struct {
Enabled []string `yaml:"enabled" json:"enabled"`
Destinations []notify.Destination `yaml:"destinations" json:"destinations"`
}
// New returns a config instance with default options set.
func New() *Config {
cfg := &Config{}
// By default, we want header to be true; this is
// ugly wrt initCfg, as the zero value of a bool
2020-08-06 20:58:47 +03:00
// is false, but we actually want it to be true for Header.
cfg.Defaults.Header = true
2020-08-06 20:58:47 +03:00
initCfg(cfg)
2020-08-06 20:58:47 +03:00
return cfg
}
// initCfg checks if required values are present, and if not, sets them.
func initCfg(cfg *Config) {
2020-08-06 20:58:47 +03:00
if cfg.Sources == nil {
cfg.Sources = &source.Set{}
}
if cfg.Notification == nil {
cfg.Notification = &Notification{}
}
if cfg.Defaults.Format == "" {
cfg.Defaults.Format = FormatTable
2020-08-06 20:58:47 +03:00
}
if cfg.Defaults.Timeout == 0 {
// Probably should be setting this in the New function,
// but we haven't yet defined cli's behavior wrt
// a zero timeout. Does it mean no timeout?
cfg.Defaults.Timeout = 10 * time.Second
2020-08-06 20:58:47 +03:00
}
}
// Format is a sq output format such as json or xml.
type Format string
// UnmarshalText implements encoding.TextUnmarshaler.
func (f *Format) UnmarshalText(text []byte) error {
switch Format(text) {
default:
return errz.Errorf("unknown output format %q", string(text))
2020-08-08 06:06:56 +03:00
case FormatJSON, FormatJSONA, FormatJSONL, FormatTable, FormatRaw,
2020-08-06 20:58:47 +03:00
FormatHTML, FormatMarkdown, FormatXLSX, FormatXML, FormatCSV, FormatTSV:
}
*f = Format(text)
return nil
}
2020-08-08 06:06:56 +03:00
// Output format values.
2020-08-06 20:58:47 +03:00
const (
FormatJSON Format = "json"
FormatJSONL Format = "jsonl"
FormatJSONA Format = "jsona"
2020-08-08 06:06:56 +03:00
FormatTable Format = "table"
2020-08-06 20:58:47 +03:00
FormatRaw Format = "raw"
FormatHTML Format = "html"
FormatMarkdown Format = "markdown"
FormatXLSX Format = "xlsx"
FormatXML Format = "xml"
FormatCSV Format = "csv"
FormatTSV Format = "tsv"
)