2020-08-06 20:58:47 +03:00
|
|
|
// Package config holds CLI configuration.
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
"github.com/neilotoole/sq/cli/buildinfo"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/userdriver"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
)
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
const (
|
|
|
|
EnvarLogPath = "SQ_LOGFILE"
|
|
|
|
EnvarLogTruncate = "SQ_LOGFILE_TRUNCATE"
|
|
|
|
|
|
|
|
// EnvarConfigDir is the legacy envar for config location.
|
|
|
|
// Instead use EnvarConfig.
|
|
|
|
EnvarConfigDir = "SQ_CONFIGDIR"
|
|
|
|
|
|
|
|
// EnvarConfig is the envar for config location.
|
|
|
|
EnvarConfig = "SQ_CONFIG"
|
|
|
|
)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Config holds application config/session data.
|
|
|
|
type Config struct {
|
2020-08-09 16:46:46 +03:00
|
|
|
// Version is the config version. This will allow sq to
|
2023-04-19 08:28:09 +03:00
|
|
|
// upgrade config files if needed. It must be a valid semver.
|
|
|
|
Version string `yaml:"config_version" json:"config_version"`
|
2020-08-09 16:46:46 +03:00
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Options contains default settings, such as output format.
|
|
|
|
Options Options `yaml:"options" json:"options"`
|
2020-08-09 16:46:46 +03:00
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Collection is the set of data sources.
|
|
|
|
Collection *source.Collection `yaml:"collection" json:"collection"`
|
2020-08-09 16:46:46 +03:00
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Ext holds sq config extensions, such as user driver config.
|
|
|
|
Ext Ext `yaml:"-" json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// String returns a log/debug-friendly representation.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (c *Config) String() string {
|
|
|
|
return stringz.SprintJSON(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ext holds additional config (extensions) loaded from other
|
2022-12-18 09:07:38 +03:00
|
|
|
// config files, e.g. ~/.config/sq/ext/*.sq.yml.
|
2020-08-06 20:58:47 +03:00
|
|
|
type Ext struct {
|
|
|
|
UserDrivers []*userdriver.DriverDef `yaml:"user_drivers" json:"user_drivers"`
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Options contains default config values.
|
|
|
|
type Options struct {
|
2021-02-22 10:37:00 +03:00
|
|
|
// Format is the default output format: json, table, etc.
|
|
|
|
Format Format `yaml:"output_format" json:"output_format"`
|
|
|
|
|
|
|
|
// Header determines if a header should be printed (if relevant
|
|
|
|
// for the output format).
|
|
|
|
Header bool `yaml:"output_header" json:"output_header"`
|
|
|
|
|
|
|
|
// PingTimeout is the allowed time for a ping.
|
|
|
|
PingTimeout time.Duration `yaml:"ping_timeout" json:"ping_timeout"`
|
|
|
|
|
|
|
|
// ShellCompletionTimeout is the time allowed for the shell
|
|
|
|
// completion callback to execute.
|
|
|
|
ShellCompletionTimeout time.Duration `yaml:"shell_completion_timeout" json:"shell_completion_timeout"`
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a config instance with default options set.
|
|
|
|
func New() *Config {
|
|
|
|
cfg := &Config{}
|
|
|
|
|
|
|
|
// By default, we want header to be true; this is
|
2020-08-09 16:46:46 +03:00
|
|
|
// 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.
|
2023-04-19 08:28:09 +03:00
|
|
|
cfg.Options.Header = true
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2020-08-09 16:46:46 +03:00
|
|
|
initCfg(cfg)
|
2020-08-06 20:58:47 +03:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2020-08-09 16:46:46 +03:00
|
|
|
// initCfg checks if required values are present, and if not, sets them.
|
|
|
|
func initCfg(cfg *Config) {
|
2023-04-19 08:28:09 +03:00
|
|
|
if cfg.Collection == nil {
|
|
|
|
cfg.Collection = &source.Collection{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Version == "" {
|
|
|
|
cfg.Version = buildinfo.Version
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
if cfg.Options.Format == "" {
|
|
|
|
cfg.Options.Format = FormatTable
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
if cfg.Options.PingTimeout == 0 {
|
2020-08-09 16:46:46 +03:00
|
|
|
// 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?
|
2023-04-19 08:28:09 +03:00
|
|
|
cfg.Options.PingTimeout = 10 * time.Second
|
2021-02-22 10:37:00 +03:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
if cfg.Options.ShellCompletionTimeout == 0 {
|
|
|
|
cfg.Options.ShellCompletionTimeout = time.Millisecond * 500
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// Format is an output format such as json or xml.
|
2020-08-06 20:58:47 +03:00
|
|
|
type Format string
|
|
|
|
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
|
|
|
func (f *Format) UnmarshalText(text []byte) error {
|
|
|
|
switch Format(text) {
|
|
|
|
default:
|
2023-04-02 22:49:45 +03:00
|
|
|
return errz.Errorf("unknown output format {%s}", string(text))
|
2020-08-08 06:06:56 +03:00
|
|
|
case FormatJSON, FormatJSONA, FormatJSONL, FormatTable, FormatRaw,
|
2023-04-19 08:28:09 +03:00
|
|
|
FormatHTML, FormatMarkdown, FormatXLSX, FormatXML,
|
|
|
|
FormatCSV, FormatTSV, FormatYAML:
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*f = Format(text)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// String returns the format value.
|
|
|
|
func (f Format) String() string {
|
|
|
|
return string(f)
|
|
|
|
}
|
|
|
|
|
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"
|
2023-04-19 08:28:09 +03:00
|
|
|
FormatYAML Format = "yaml"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|