graphql-engine/cli/logger_hook.go
Aravind K P c7ac1ede3f cli: add migrate and metadata packages
>

### Description
>
This PR adds two new packages which implement the CLI requirements specified in RFC:https://github.com/hasura/lux/blob/cloud/docs/rfcs/20210614_github_integration.md

1. `pkg/metadata`

![image](https://user-images.githubusercontent.com/8335904/122384828-b4757d80-cf89-11eb-9e21-ef116fb928e9.png)

2. `pkg/migrate`

![image](https://user-images.githubusercontent.com/8335904/122510554-68771700-d023-11eb-9f5d-046d2c0cf18a.png)

### Changelog

- [x] `CHANGELOG.md` is updated with user-facing content relevant to this PR. If no changelog is required, then add the `no-changelog-required` label.

### Affected components

- [x] CLI

https://github.com/hasura/graphql-engine-mono/pull/1598

GitOrigin-RevId: 0e2bce498386c5aae68dbca0fe383a6afff9d1a9
2021-06-18 06:25:14 +00:00

62 lines
1.3 KiB
Go

package cli
import (
"io"
"github.com/briandowns/spinner"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
)
type spinnerHook struct {
logger *logrus.Logger
spinner *spinner.Spinner
}
func newSpinnerHandlerHook(parent *logrus.Logger, spinner *spinner.Spinner, isTerminal, noColor bool) *spinnerHook {
logger := logrus.New()
logger.Out = parent.Out
if parent.Out != io.Discard {
if isTerminal {
if noColor {
logger.Formatter = &logrus.TextFormatter{
DisableColors: true,
DisableTimestamp: true,
}
} else {
logger.Formatter = &logrus.TextFormatter{
ForceColors: true,
DisableTimestamp: true,
}
}
logger.Out = colorable.NewColorableStderr()
} else {
logger.Formatter = &logrus.JSONFormatter{
PrettyPrint: false,
}
}
logger.Level = parent.GetLevel()
}
return &spinnerHook{
logger: logger,
spinner: spinner,
}
}
// Levels returns all levels this hook should be registered to
func (hook *spinnerHook) Levels() []logrus.Level {
return logrus.AllLevels
}
// Fire is triggered on new log entries
func (hook *spinnerHook) Fire(entry *logrus.Entry) error {
if hook.spinner.Active() {
hook.spinner.Stop()
defer func() {
hook.spinner.Start()
}()
}
entry.Logger = hook.logger
return nil
}