2020-08-06 20:58:47 +03:00
|
|
|
// Package config holds CLI configuration.
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-04-23 01:31:28 +03:00
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/cli/buildinfo"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/userdriver"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
2020-08-23 13:42:15 +03:00
|
|
|
"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 (
|
2023-05-05 17:32:50 +03:00
|
|
|
// EnvarLogPath is the log file path.
|
|
|
|
EnvarLogPath = "SQ_LOG_FILE"
|
|
|
|
|
|
|
|
// EnvarLogLevel is the log level. It maps to a slog.Level.
|
|
|
|
EnvarLogLevel = "SQ_LOG_LEVEL"
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
// EnvarLogFormat is the log format. It maps to a slog.Level.
|
|
|
|
EnvarLogFormat = "SQ_LOG_FORMAT"
|
|
|
|
|
2023-05-05 17:32:50 +03:00
|
|
|
// EnvarLogEnabled turns logging on or off.
|
|
|
|
EnvarLogEnabled = "SQ_LOG"
|
2023-04-19 08:28:09 +03:00
|
|
|
|
|
|
|
// 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.
|
2023-04-26 18:16:42 +03:00
|
|
|
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.
|
2023-04-26 18:16:42 +03:00
|
|
|
Options 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a config instance with default options set.
|
|
|
|
func New() *Config {
|
2023-04-26 18:16:42 +03:00
|
|
|
cfg := &Config{
|
|
|
|
Collection: &source.Collection{},
|
|
|
|
Options: options.Options{},
|
|
|
|
Version: buildinfo.Version,
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
return cfg
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:31:28 +03:00
|
|
|
// Valid returns an error if cfg is not valid.
|
|
|
|
func Valid(cfg *Config) error {
|
|
|
|
if cfg == nil {
|
|
|
|
return errz.New("config is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !semver.IsValid(cfg.Version) {
|
|
|
|
return errz.Errorf("config: invalid '.config_version': %s", cfg.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Collection != nil {
|
|
|
|
if _, err := source.VerifyIntegrity(cfg.Collection); err != nil {
|
|
|
|
return errz.Wrap(err, "config: invalid '.sources'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|