Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Davim committed Nov 20, 2020
1 parent 9eb99cd commit 5fda4f6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
30 changes: 7 additions & 23 deletions internal/app/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,41 @@ import (
)

type Logger struct {
*logger.Logger
SlackWebhook string
baseLogger *logger.Logger
}

func (l *Logger) Info(message string) {
l.baseLogger.Info(message)
}

func (l *Logger) Debug(message string) {
if flags.debug {
l.baseLogger.Debug(message)
l.Logger.Debug(message)
}
}

func (l *Logger) Verbose(message string) {
if flags.verbose {
l.baseLogger.Info(message)
l.Logger.Info(message)
}
}

func (l *Logger) Error(message string) {
if _, err := url.ParseRequestURI(l.SlackWebhook); err == nil {
notifySlack(message, l.SlackWebhook, true, flags.apply)
}
l.baseLogger.Error(message)
}

func (l *Logger) Errorf(message string, args ...interface{}) {
l.baseLogger.Errorf(message, args...)
}

func (l *Logger) Warning(message string) {
l.baseLogger.Warning(message)
}

func (l *Logger) Notice(message string) {
l.baseLogger.Notice(message)
l.Logger.Error(message)
}

func (l *Logger) Critical(message string) {
if _, err := url.ParseRequestURI(l.SlackWebhook); err == nil {
notifySlack(message, l.SlackWebhook, true, flags.apply)
}
l.baseLogger.Critical(message)
l.Logger.Critical(message)
}

func (l *Logger) Fatal(message string) {
if _, err := url.ParseRequestURI(l.SlackWebhook); err == nil {
notifySlack(message, l.SlackWebhook, true, flags.apply)
}
l.baseLogger.Fatal(message)
l.Logger.Fatal(message)
}

func initLogs(verbose bool, noColors bool) {
Expand All @@ -71,5 +55,5 @@ func initLogs(verbose bool, noColors bool) {
if noColors {
colors = 0
}
log.baseLogger, _ = logger.New("logger", colors, os.Stdout, logLevel)
log.Logger, _ = logger.New("logger", colors, os.Stdout, logLevel)
}
12 changes: 6 additions & 6 deletions internal/azure/azblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ var p pipeline.Pipeline
// auth checks for AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY in the environment
// if env vars are set, it will authenticate and create an azblob request pipeline
// returns false and error message if credentials are not set or are invalid
func auth() (bool, string) {
func auth() error {
accountName, accountKey = os.Getenv("AZURE_STORAGE_ACCOUNT"), os.Getenv("AZURE_STORAGE_ACCESS_KEY")
if len(accountName) != 0 && len(accountKey) != 0 {
log.Println("AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY are set in the environment. They will be used to connect to Azure storage.")
// Create a default request pipeline
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
if err == nil {
p = azblob.NewPipeline(credential, azblob.PipelineOptions{})
return true, ""
return nil
}
return false, err.Error()
return err

}
return false, "either the AZURE_STORAGE_ACCOUNT or AZURE_STORAGE_ACCESS_KEY environment variable is not set"
return fmt.Errorf("either the AZURE_STORAGE_ACCOUNT or AZURE_STORAGE_ACCESS_KEY environment variable is not set")
}

// ReadFile reads a file from storage container and saves it in a desired location.
func ReadFile(containerName string, filename string, outFile string, noColors bool) {
style = aurora.NewAurora(!noColors)
if ok, err := auth(); !ok {
log.Fatal(style.Bold(style.Red("ERROR: " + err)))
if err := auth(); err != nil {
log.Fatal(style.Bold(style.Red("ERROR: " + err.Error())))
}

URL, _ := url.Parse(
Expand Down

0 comments on commit 5fda4f6

Please sign in to comment.