2018-06-24 16:47:01 +03:00
|
|
|
// Package cli and it's sub packages implements the command line tool for Hasura
|
|
|
|
// GraphQL Engine. The CLI operates on a directory, denoted by
|
|
|
|
// "ExecutionDirectory" in the "ExecutionContext" struct.
|
|
|
|
//
|
|
|
|
// The ExecutionContext is passed to all the subcommands so that a singleton
|
|
|
|
// context is available for the execution. Logger and Spinner comes from the same
|
|
|
|
// context.
|
|
|
|
package cli
|
2018-06-24 16:40:48 +03:00
|
|
|
|
|
|
|
import (
|
2020-02-24 19:14:46 +03:00
|
|
|
"io/ioutil"
|
2018-07-09 16:47:38 +03:00
|
|
|
"net/url"
|
2018-06-24 16:40:48 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-02-24 19:14:46 +03:00
|
|
|
"strings"
|
2018-06-24 16:40:48 +03:00
|
|
|
"time"
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/briandowns/spinner"
|
2019-01-28 16:55:28 +03:00
|
|
|
"github.com/gofrs/uuid"
|
2020-02-24 19:14:46 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/metadata/actions/types"
|
|
|
|
"github.com/hasura/graphql-engine/cli/plugins"
|
2020-01-08 06:06:09 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/telemetry"
|
|
|
|
"github.com/hasura/graphql-engine/cli/util"
|
2018-07-04 15:43:52 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/version"
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2018-06-24 16:47:01 +03:00
|
|
|
"github.com/spf13/viper"
|
2019-12-25 17:44:02 +03:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
|
|
|
|
2018-06-27 15:04:09 +03:00
|
|
|
// Other constants used in the package
|
|
|
|
const (
|
|
|
|
// Name of the global configuration directory
|
2019-02-14 12:37:47 +03:00
|
|
|
GlobalConfigDirName = ".hasura"
|
2018-06-27 15:04:09 +03:00
|
|
|
// Name of the global configuration file
|
2019-02-14 12:37:47 +03:00
|
|
|
GlobalConfigFileName = "config.json"
|
2019-02-04 13:51:29 +03:00
|
|
|
|
|
|
|
// Name of the file to store last update check time
|
|
|
|
LastUpdateCheckFileName = "last_update_check_at"
|
2018-06-27 15:04:09 +03:00
|
|
|
)
|
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
// String constants
|
|
|
|
const (
|
|
|
|
StrTelemetryNotice = `Help us improve Hasura! The cli collects anonymized usage stats which
|
|
|
|
allow us to keep improving Hasura at warp speed. To opt-out or read more,
|
|
|
|
visit https://docs.hasura.io/1.0/graphql/manual/guides/telemetry.html
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// ConfigVersion defines the version of the Config.
|
|
|
|
type ConfigVersion int
|
2019-02-14 12:37:47 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
const (
|
|
|
|
// V1 represents config version 1
|
|
|
|
V1 ConfigVersion = iota + 1
|
|
|
|
// V2 represents config version 2
|
|
|
|
V2
|
|
|
|
)
|
2019-02-14 12:37:47 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// ServerConfig has the config values required to contact the server
|
|
|
|
type ServerConfig struct {
|
2018-06-24 16:47:01 +03:00
|
|
|
// Endpoint for the GraphQL Engine
|
2020-02-24 19:14:46 +03:00
|
|
|
Endpoint string `yaml:"endpoint"`
|
2019-02-14 12:37:47 +03:00
|
|
|
// AccessKey (deprecated) (optional) Admin secret key required to query the endpoint
|
2020-02-24 19:14:46 +03:00
|
|
|
AccessKey string `yaml:"access_key,omitempty"`
|
2019-02-14 12:37:47 +03:00
|
|
|
// AdminSecret (optional) Admin secret required to query the endpoint
|
2020-02-24 19:14:46 +03:00
|
|
|
AdminSecret string `yaml:"admin_secret,omitempty"`
|
2018-07-09 16:47:38 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
ParsedEndpoint *url.URL `yaml:"-"`
|
2019-02-14 12:37:47 +03:00
|
|
|
}
|
|
|
|
|
2018-07-09 16:47:38 +03:00
|
|
|
// ParseEndpoint ensures the endpoint is valid.
|
2019-02-14 12:37:47 +03:00
|
|
|
func (s *ServerConfig) ParseEndpoint() error {
|
|
|
|
nurl, err := url.Parse(s.Endpoint)
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-14 12:37:47 +03:00
|
|
|
s.ParsedEndpoint = nurl
|
2018-07-09 16:47:38 +03:00
|
|
|
return nil
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// Config represents configuration required for the CLI to function
|
|
|
|
type Config struct {
|
|
|
|
// Version of the config.
|
|
|
|
Version ConfigVersion `yaml:"version"`
|
|
|
|
|
|
|
|
// ServerConfig to be used by CLI to contact server.
|
|
|
|
ServerConfig `yaml:",inline"`
|
|
|
|
|
|
|
|
// MetadataDirectory defines the directory where the metadata files were stored.
|
|
|
|
MetadataDirectory string `yaml:"metadata_directory"`
|
|
|
|
// MigrationsDirectory defines the directory where the migration files were stored.
|
|
|
|
MigrationsDirectory string `yaml:"migrations_directory,omitempty"`
|
|
|
|
// ActionConfig defines the config required to create or generate codegen for an action.
|
|
|
|
ActionConfig types.ActionExecutionConfig `yaml:"actions"`
|
|
|
|
}
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// ExecutionContext contains various contextual information required by the cli
|
|
|
|
// at various points of it's execution. Values are filled in by the
|
|
|
|
// initializers and passed on to each command. Commands can also fill in values
|
|
|
|
// to be used further down the line.
|
2018-06-24 16:40:48 +03:00
|
|
|
type ExecutionContext struct {
|
2018-06-24 16:47:01 +03:00
|
|
|
// CMDName is the name of CMD (os.Args[0]). To be filled in later to
|
|
|
|
// correctly render example strings etc.
|
2018-06-24 16:40:48 +03:00
|
|
|
CMDName string
|
2018-06-24 16:47:01 +03:00
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
// ID is a unique ID for this Execution
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// ServerUUID is the unique ID for the server this execution is contacting.
|
|
|
|
ServerUUID string
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// Spinner is the global spinner object used to show progress across the cli.
|
2018-06-24 16:40:48 +03:00
|
|
|
Spinner *spinner.Spinner
|
2018-06-24 16:47:01 +03:00
|
|
|
// Logger is the global logger object to print logs.
|
|
|
|
Logger *logrus.Logger
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// ExecutionDirectory is the directory in which command is being executed.
|
2018-06-24 16:40:48 +03:00
|
|
|
ExecutionDirectory string
|
2018-06-24 16:47:01 +03:00
|
|
|
// MigrationDir is the name of directory where migrations are stored.
|
|
|
|
MigrationDir string
|
2020-02-24 19:14:46 +03:00
|
|
|
// MetadataDir is the name of directory where metadata files are stored.
|
|
|
|
MetadataDir string
|
2018-06-24 16:47:01 +03:00
|
|
|
// ConfigFile is the file where endpoint etc. are stored.
|
|
|
|
ConfigFile string
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// Config is the configuration object storing the endpoint and admin secret
|
2018-06-24 16:47:01 +03:00
|
|
|
// information after reading from config file or env var.
|
2020-02-24 19:14:46 +03:00
|
|
|
Config *Config
|
2018-06-24 16:47:01 +03:00
|
|
|
|
|
|
|
// GlobalConfigDir is the ~/.hasura-graphql directory to store configuration
|
|
|
|
// globally.
|
|
|
|
GlobalConfigDir string
|
|
|
|
// GlobalConfigFile is the file inside GlobalConfigDir where values are
|
|
|
|
// stored.
|
|
|
|
GlobalConfigFile string
|
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
// GlobalConfig holds all the configuration options.
|
|
|
|
GlobalConfig *GlobalConfig
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// IsStableRelease indicates if the CLI release is stable or not.
|
|
|
|
IsStableRelease bool
|
2018-07-04 15:43:52 +03:00
|
|
|
// Version indicates the version object
|
|
|
|
Version *version.Version
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// Viper indicates the viper object for the execution
|
|
|
|
Viper *viper.Viper
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// LogLevel indicates the logrus default logging level
|
|
|
|
LogLevel string
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2019-07-30 09:55:38 +03:00
|
|
|
// NoColor indicates if the outputs shouldn't be colorized
|
|
|
|
NoColor bool
|
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
// Telemetry collects the telemetry data throughout the execution
|
|
|
|
Telemetry *telemetry.Data
|
2019-02-04 13:51:29 +03:00
|
|
|
|
|
|
|
// LastUpdateCheckFile is the file where the timestamp of last update check is stored
|
|
|
|
LastUpdateCheckFile string
|
2019-02-14 09:16:36 +03:00
|
|
|
|
|
|
|
// SkipUpdateCheck will skip the auto update check if set to true
|
|
|
|
SkipUpdateCheck bool
|
2019-12-25 17:44:02 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// PluginsConfig defines the config for plugins
|
|
|
|
PluginsConfig *plugins.Config
|
|
|
|
|
|
|
|
// CodegenAssetsRepo defines the config to handle codegen-assets repo
|
|
|
|
CodegenAssetsRepo *util.GitUtil
|
|
|
|
|
|
|
|
// InitTemplatesRepo defines the config to handle init-templates repo
|
|
|
|
InitTemplatesRepo *util.GitUtil
|
|
|
|
|
2019-12-25 17:44:02 +03:00
|
|
|
// IsTerminal indicates whether the current session is a terminal or not
|
|
|
|
IsTerminal bool
|
2019-01-28 16:55:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecutionContext returns a new instance of execution context
|
|
|
|
func NewExecutionContext() *ExecutionContext {
|
|
|
|
ec := &ExecutionContext{}
|
|
|
|
ec.Telemetry = telemetry.BuildEvent()
|
|
|
|
ec.Telemetry.Version = version.BuildVersion
|
|
|
|
return ec
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// Prepare as the name suggests, prepares the ExecutionContext ec by
|
|
|
|
// initializing most of the variables to sensible defaults, if it is not already
|
|
|
|
// set.
|
2018-06-24 16:40:48 +03:00
|
|
|
func (ec *ExecutionContext) Prepare() error {
|
2018-06-24 16:47:01 +03:00
|
|
|
// set the command name
|
2018-06-24 16:40:48 +03:00
|
|
|
cmdName := os.Args[0]
|
|
|
|
if len(cmdName) == 0 {
|
|
|
|
cmdName = "hasura"
|
|
|
|
}
|
|
|
|
ec.CMDName = cmdName
|
|
|
|
|
2019-12-25 17:44:02 +03:00
|
|
|
ec.IsTerminal = terminal.IsTerminal(int(os.Stdout.Fd()))
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
// set spinner
|
|
|
|
ec.setupSpinner()
|
|
|
|
|
|
|
|
// set logger
|
|
|
|
ec.setupLogger()
|
|
|
|
|
2018-06-28 08:40:18 +03:00
|
|
|
// populate version
|
|
|
|
ec.setVersion()
|
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
// setup global config
|
|
|
|
err := ec.setupGlobalConfig()
|
|
|
|
if err != nil {
|
2019-02-04 13:51:29 +03:00
|
|
|
return errors.Wrap(err, "setting up global config failed")
|
2019-01-28 16:55:28 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// setup plugins path
|
|
|
|
err = ec.setupPlugins()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "setting up plugins path failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ec.setupCodegenAssetsRepo()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "setting up codegen-assets repo failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ec.setupInitTemplatesRepo()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "setting up init-templates repo failed")
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:51:29 +03:00
|
|
|
ec.LastUpdateCheckFile = filepath.Join(ec.GlobalConfigDir, LastUpdateCheckFileName)
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2019-02-04 13:51:29 +03:00
|
|
|
// initialize a blank server config
|
2020-02-24 19:14:46 +03:00
|
|
|
if ec.Config == nil {
|
|
|
|
ec.Config = &Config{}
|
2019-01-28 16:55:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate an execution id
|
|
|
|
if ec.ID == "" {
|
|
|
|
id := "00000000-0000-0000-0000-000000000000"
|
|
|
|
u, err := uuid.NewV4()
|
|
|
|
if err == nil {
|
|
|
|
id = u.String()
|
|
|
|
} else {
|
|
|
|
ec.Logger.Debugf("generating uuid for execution ID failed, %v", err)
|
|
|
|
}
|
|
|
|
ec.ID = id
|
|
|
|
ec.Logger.Debugf("execution id: %v", ec.ID)
|
|
|
|
}
|
|
|
|
ec.Telemetry.ExecutionID = ec.ID
|
2018-06-24 16:47:01 +03:00
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// setupPlugins create and returns the inferred paths for hasura. By default, it assumes
|
|
|
|
// $HOME/.hasura as the base path
|
|
|
|
func (ec *ExecutionContext) setupPlugins() error {
|
|
|
|
base := filepath.Join(ec.GlobalConfigDir, "plugins")
|
|
|
|
base, err := filepath.Abs(base)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot get absolute path")
|
|
|
|
}
|
|
|
|
ec.PluginsConfig = plugins.New(base)
|
|
|
|
ec.PluginsConfig.Logger = ec.Logger
|
|
|
|
return ec.PluginsConfig.Prepare()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *ExecutionContext) setupCodegenAssetsRepo() error {
|
|
|
|
base := filepath.Join(ec.GlobalConfigDir, util.ActionsCodegenDirName)
|
|
|
|
base, err := filepath.Abs(base)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot get absolute path")
|
|
|
|
}
|
|
|
|
ec.CodegenAssetsRepo = util.NewGitUtil(util.ActionsCodegenRepoURI, base, "")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *ExecutionContext) setupInitTemplatesRepo() error {
|
|
|
|
base := filepath.Join(ec.GlobalConfigDir, util.InitTemplatesDirName)
|
|
|
|
base, err := filepath.Abs(base)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot get absolute path")
|
|
|
|
}
|
|
|
|
ec.InitTemplatesRepo = util.NewGitUtil(util.InitTemplatesRepoURI, base, "")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// Validate prepares the ExecutionContext ec and then validates the
|
|
|
|
// ExecutionDirectory to see if all the required files and directories are in
|
|
|
|
// place.
|
2018-06-24 16:40:48 +03:00
|
|
|
func (ec *ExecutionContext) Validate() error {
|
2020-02-24 19:14:46 +03:00
|
|
|
// ensure plugins index exists
|
|
|
|
err := ec.PluginsConfig.Repo.EnsureCloned()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ensuring plugins index failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure codegen-assets repo exists
|
|
|
|
err = ec.CodegenAssetsRepo.EnsureCloned()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ensuring codegen-assets repo failed")
|
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// validate execution directory
|
2020-02-24 19:14:46 +03:00
|
|
|
err = ec.validateDirectory()
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
2018-06-24 16:47:01 +03:00
|
|
|
return errors.Wrap(err, "validating current directory failed")
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// set names of config file
|
2018-06-24 16:40:48 +03:00
|
|
|
ec.ConfigFile = filepath.Join(ec.ExecutionDirectory, "config.yaml")
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// read config and parse the values into Config
|
2018-06-24 16:40:48 +03:00
|
|
|
err = ec.readConfig()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot read config")
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// set name of migration directory
|
|
|
|
ec.MigrationDir = filepath.Join(ec.ExecutionDirectory, ec.Config.MigrationsDirectory)
|
|
|
|
if _, err := os.Stat(ec.MigrationDir); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(ec.MigrationDir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot create migrations directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ec.Config.Version == V2 && ec.Config.MetadataDirectory != "" {
|
|
|
|
// set name of metadata directory
|
|
|
|
ec.MetadataDir = filepath.Join(ec.ExecutionDirectory, ec.Config.MetadataDirectory)
|
|
|
|
if _, err := os.Stat(ec.MetadataDir); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(ec.MetadataDir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot create metadata directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ec.Logger.Debug("graphql engine endpoint: ", ec.Config.ServerConfig.Endpoint)
|
|
|
|
ec.Logger.Debug("graphql engine admin_secret: ", ec.Config.ServerConfig.AdminSecret)
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2018-07-03 20:10:13 +03:00
|
|
|
// get version from the server and match with the cli version
|
2019-01-28 16:55:28 +03:00
|
|
|
err = ec.checkServerVersion()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "version check")
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// get the server feature flags
|
|
|
|
err = ec.Version.GetServerFeatureFlags()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error in getting server feature flags")
|
|
|
|
}
|
|
|
|
|
|
|
|
state := util.GetServerState(ec.Config.ServerConfig.Endpoint, ec.Config.ServerConfig.AdminSecret, ec.Version.ServerSemver, ec.Logger)
|
2019-01-28 16:55:28 +03:00
|
|
|
ec.ServerUUID = state.UUID
|
|
|
|
ec.Telemetry.ServerUUID = ec.ServerUUID
|
|
|
|
ec.Logger.Debugf("server: uuid: %s", ec.ServerUUID)
|
|
|
|
|
|
|
|
return nil
|
2018-07-03 20:10:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ec *ExecutionContext) checkServerVersion() error {
|
2020-02-24 19:14:46 +03:00
|
|
|
v, err := version.FetchServerVersion(ec.Config.ServerConfig.Endpoint)
|
2018-07-03 20:10:13 +03:00
|
|
|
if err != nil {
|
2018-07-04 15:43:52 +03:00
|
|
|
return errors.Wrap(err, "failed to get version from server")
|
2018-07-03 20:10:13 +03:00
|
|
|
}
|
2018-07-04 15:43:52 +03:00
|
|
|
ec.Version.SetServerVersion(v)
|
2019-01-28 16:55:28 +03:00
|
|
|
ec.Telemetry.ServerVersion = ec.Version.GetServerVersion()
|
2018-07-04 15:43:52 +03:00
|
|
|
isCompatible, reason := ec.Version.CheckCLIServerCompatibility()
|
|
|
|
ec.Logger.Debugf("versions: cli: [%s] server: [%s]", ec.Version.GetCLIVersion(), ec.Version.GetServerVersion())
|
|
|
|
ec.Logger.Debugf("compatibility check: [%v] %v", isCompatible, reason)
|
|
|
|
if !isCompatible {
|
2020-01-16 06:53:18 +03:00
|
|
|
ec.Logger.Warnf("[cli: %s] [server: %s] version mismatch: %s", ec.Version.GetCLIVersion(), ec.Version.GetServerVersion(), reason)
|
2018-07-03 20:10:13 +03:00
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// WriteConfig writes the configuration from ec.Config or input config
|
|
|
|
func (ec *ExecutionContext) WriteConfig(config *Config) error {
|
|
|
|
var cfg *Config
|
|
|
|
if config != nil {
|
|
|
|
cfg = config
|
|
|
|
} else {
|
|
|
|
cfg = ec.Config
|
|
|
|
}
|
|
|
|
y, err := yaml.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(ec.ConfigFile, y, 0644)
|
|
|
|
}
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// readConfig reads the configuration from config file, flags and env vars,
|
|
|
|
// through viper.
|
2018-06-24 16:40:48 +03:00
|
|
|
func (ec *ExecutionContext) readConfig() error {
|
2018-06-24 16:47:01 +03:00
|
|
|
// need to get existing viper because https://github.com/spf13/viper/issues/233
|
|
|
|
v := ec.Viper
|
|
|
|
v.SetEnvPrefix("HASURA_GRAPHQL")
|
2020-02-24 19:14:46 +03:00
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2018-06-24 16:47:01 +03:00
|
|
|
v.AutomaticEnv()
|
|
|
|
v.SetConfigName("config")
|
2020-02-24 19:14:46 +03:00
|
|
|
v.SetDefault("version", "1")
|
2019-01-28 16:55:28 +03:00
|
|
|
v.SetDefault("endpoint", "http://localhost:8080")
|
2019-02-14 12:37:47 +03:00
|
|
|
v.SetDefault("admin_secret", "")
|
2019-01-28 16:55:28 +03:00
|
|
|
v.SetDefault("access_key", "")
|
2020-02-24 19:14:46 +03:00
|
|
|
v.SetDefault("metadata_directory", "")
|
|
|
|
v.SetDefault("migrations_directory", "migrations")
|
|
|
|
v.SetDefault("actions.kind", "synchronous")
|
|
|
|
v.SetDefault("actions.handler_webhook_baseurl", "http://localhost:3000")
|
|
|
|
v.SetDefault("actions.codegen.framework", "")
|
|
|
|
v.SetDefault("actions.codegen.output_dir", "")
|
|
|
|
v.SetDefault("actions.codegen.uri", "")
|
2018-06-24 16:47:01 +03:00
|
|
|
v.AddConfigPath(ec.ExecutionDirectory)
|
|
|
|
err := v.ReadInConfig()
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
2019-02-14 12:37:47 +03:00
|
|
|
return errors.Wrap(err, "cannot read config from file/env")
|
|
|
|
}
|
|
|
|
adminSecret := v.GetString("admin_secret")
|
|
|
|
if adminSecret == "" {
|
|
|
|
adminSecret = v.GetString("access_key")
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
ec.Config = &Config{
|
|
|
|
Version: ConfigVersion(v.GetInt("version")),
|
|
|
|
ServerConfig: ServerConfig{
|
|
|
|
Endpoint: v.GetString("endpoint"),
|
|
|
|
AdminSecret: adminSecret,
|
|
|
|
},
|
|
|
|
MetadataDirectory: v.GetString("metadata_directory"),
|
|
|
|
MigrationsDirectory: v.GetString("migrations_directory"),
|
|
|
|
ActionConfig: types.ActionExecutionConfig{
|
|
|
|
Kind: v.GetString("actions.kind"),
|
|
|
|
HandlerWebhookBaseURL: v.GetString("actions.handler_webhook_baseurl"),
|
|
|
|
Codegen: &types.CodegenExecutionConfig{
|
|
|
|
Framework: v.GetString("actions.codegen.framework"),
|
|
|
|
OutputDir: v.GetString("actions.codegen.output_dir"),
|
|
|
|
URI: v.GetString("actions.codegen.uri"),
|
|
|
|
},
|
|
|
|
},
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
return ec.Config.ServerConfig.ParseEndpoint()
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// setupSpinner creates a default spinner if the context does not already have
|
|
|
|
// one.
|
2018-06-24 16:40:48 +03:00
|
|
|
func (ec *ExecutionContext) setupSpinner() {
|
|
|
|
if ec.Spinner == nil {
|
|
|
|
spnr := spinner.New(spinner.CharSets[7], 100*time.Millisecond)
|
|
|
|
spnr.Writer = os.Stderr
|
|
|
|
ec.Spinner = spnr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 15:04:09 +03:00
|
|
|
// Spin stops any existing spinner and starts a new one with the given message.
|
|
|
|
func (ec *ExecutionContext) Spin(message string) {
|
2019-12-25 17:44:02 +03:00
|
|
|
if ec.IsTerminal {
|
|
|
|
ec.Spinner.Stop()
|
|
|
|
ec.Spinner.Prefix = message
|
|
|
|
ec.Spinner.Start()
|
|
|
|
} else {
|
|
|
|
ec.Logger.Println(message)
|
|
|
|
}
|
2018-06-27 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2018-06-24 16:47:01 +03:00
|
|
|
// setupLogger creates a default logger if context does not have one set.
|
2018-06-24 16:40:48 +03:00
|
|
|
func (ec *ExecutionContext) setupLogger() {
|
|
|
|
if ec.Logger == nil {
|
|
|
|
logger := logrus.New()
|
|
|
|
ec.Logger = logger
|
|
|
|
}
|
2018-06-24 16:47:01 +03:00
|
|
|
|
|
|
|
if ec.LogLevel != "" {
|
|
|
|
level, err := logrus.ParseLevel(ec.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
ec.Logger.WithError(err).Error("error parsing log-level flag")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ec.Logger.SetLevel(level)
|
|
|
|
}
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
ec.Logger.Hooks = make(logrus.LevelHooks)
|
|
|
|
ec.Logger.AddHook(newSpinnerHandlerHook(ec.Logger, ec.Spinner, ec.IsTerminal, ec.NoColor))
|
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
// set the logger for telemetry
|
|
|
|
if ec.Telemetry.Logger == nil {
|
|
|
|
ec.Telemetry.Logger = ec.Logger
|
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:40:18 +03:00
|
|
|
// SetVersion sets the version inside context, according to the variable
|
2018-06-24 16:47:01 +03:00
|
|
|
// 'version' set during build context.
|
|
|
|
func (ec *ExecutionContext) setVersion() {
|
2018-07-04 15:43:52 +03:00
|
|
|
if ec.Version == nil {
|
|
|
|
ec.Version = version.New()
|
2018-06-24 16:47:01 +03:00
|
|
|
}
|
|
|
|
}
|