Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions cmd/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmd

import (
"os"

"github.com/mattn/go-isatty"
"github.com/ttacon/chalk"
)

var tty bool

func init() {
// Detect if we're in a TTY or not
tty = isatty.IsTerminal(os.Stdout.Fd())
}

var (
// Styles
Underline = TextStyle{chalk.Underline}
Bold = TextStyle{chalk.Bold}

// Colors
Black = Color{chalk.Black}
Red = Color{chalk.Red}
Green = Color{chalk.Green}
Yellow = Color{chalk.Yellow}
Blue = Color{chalk.Blue}
Magenta = Color{chalk.Magenta}
Cyan = Color{chalk.Cyan}
White = Color{chalk.White}
)

// A type that wraps chalk.TextStyle but adds detections for if we're in a TTY
type TextStyle struct {
underlying chalk.TextStyle
}

func (t TextStyle) TextStyle(val string) string {
if !tty {
// Don't style if we're not in a TTY
return val
}

return t.underlying.TextStyle(val)
}

// A type that wraps chalk.Color but adds detections for if we're in a TTY
type Color struct {
underlying chalk.Color
}

func (c Color) Color(val string) string {
if !tty {
// Don't style if we're not in a TTY
return val
}

return c.underlying.Color(val)
}
23 changes: 10 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func ensureToken(ctx context.Context, requiredScopes []string, signals chan os.S
log.WithContext(ctx).Error(err)
return
}
log.WithContext(ctx).Debug("Got token 1!")
log.WithContext(ctx).Debug("Got token")

tokenChan <- tok

Expand All @@ -148,7 +148,7 @@ func ensureToken(ctx context.Context, requiredScopes []string, signals chan os.S

u := config.AuthCodeURL(oAuthStateString, oauth2.AccessTypeOnline, audienceOption)

log.WithContext(ctx).Infof("Log in here: %v", u)
log.WithContext(ctx).Infof("Follow this link to authenticate: %v", Underline.TextStyle(u))

// Start the webserver
log.WithContext(ctx).Trace("Starting webserver to listen for callback, press Ctrl+C to cancel")
Expand All @@ -166,7 +166,7 @@ func ensureToken(ctx context.Context, requiredScopes []string, signals chan os.S
var token *oauth2.Token
select {
case token = <-tokenChan:
log.WithContext(ctx).Debug("Got token 2!")
// Keep working
case <-signals:
log.WithContext(ctx).Debug("Received interrupt, exiting")
return ctx, errors.New("cancelled")
Expand All @@ -175,9 +175,11 @@ func ensureToken(ctx context.Context, requiredScopes []string, signals chan os.S
// Stop the server
err = srv.Shutdown(ctx)
if err != nil {
log.WithContext(ctx).WithError(err).Info("failed to shutdown auth callback server, but continuing anyways")
log.WithContext(ctx).WithError(err).Warn("failed to shutdown auth callback server, but continuing anyway")
}

log.WithContext(ctx).Info("Authenticated successfully ✅")

// Set the token
return context.WithValue(ctx, sdp.UserTokenContextKey{}, token.AccessToken), nil
}
Expand Down Expand Up @@ -277,6 +279,10 @@ func init() {

// Run this before we do anything to set up the loglevel
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
formatter := new(log.TextFormatter)
formatter.DisableTimestamp = true
log.SetFormatter(formatter)

// Read env vars
var lvl log.Level

Expand All @@ -290,7 +296,6 @@ func init() {
lvl = log.InfoLevel
}
log.SetLevel(lvl)
log.WithField("level", lvl).Infof("set log level from config")

if viper.GetBool("json-log") {
log.SetFormatter(&log.JSONFormatter{})
Expand All @@ -317,11 +322,3 @@ func initConfig() {
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv() // read in environment variables that match
}

// must panics if the passed in error is not nil
// use this for init-time error checking of viper/cobra stuff that sometimes errors if the flag does not exist
func must(err error) {
if err != nil {
panic(fmt.Errorf("error initialising: %w", err))
}
}
Loading