From 4ca2a7fdfc7bc7f889a748e3b74c262d93bf01b8 Mon Sep 17 00:00:00 2001 From: shreyasbhat0 Date: Tue, 5 Dec 2023 15:02:17 +0530 Subject: [PATCH] feat: update tutorial command --- cli/cmd/utility/tutorial.go | 16 +++++++++ cli/common/interfaces.go | 10 +++--- cli/common/logs.go | 72 ++++++++++++++----------------------- 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/cli/cmd/utility/tutorial.go b/cli/cmd/utility/tutorial.go index fb03b138..004f3066 100644 --- a/cli/cmd/utility/tutorial.go +++ b/cli/cmd/utility/tutorial.go @@ -20,4 +20,20 @@ var TutorialCmd = common.NewDiveCommandBuilder(). func tutorial(cmd *cobra.Command, args []string) { + cli := common.GetCli() + + cli.Logger().SetOutputToStdout() + + err := common.ValidateArgs(args) + + if err != nil { + cli.Logger().Error(common.CodeOf(err), common.Errorcf(common.CodeOf(err), "error %s !!! \n%s", err, cmd.UsageString()).Error()) + } + + cli.Logger().Info("Redirecting to YouTube.....") + + if err := common.OpenFile(tutorialURL); err != nil { + cli.Logger().Fatalf(common.InvalidCommandError, common.Errorcf(common.CodeOf(err), "Failed to open Dive YouTube chanel with error %v", err).Error()) + } + } diff --git a/cli/common/interfaces.go b/cli/common/interfaces.go index c9e3320b..e27b99fb 100644 --- a/cli/common/interfaces.go +++ b/cli/common/interfaces.go @@ -18,11 +18,11 @@ type Logger interface { Warn(message string) Error(errorCode ErrorCode, errorMessage string) Fatal(errorCode ErrorCode, errorMessage string) - Infof(message string) - Warnf(message string) - Debugf(message string) - Errorf(errorCode ErrorCode, errorMessage string) - Fatalf(errorCode ErrorCode, errorMessage string) + Infof(format string, args ...interface{}) + Warnf(format string, args ...interface{}) + Debugf(format string, args ...interface{}) + Errorf(errorCode ErrorCode, format string, args ...interface{}) + Fatalf(errorCode ErrorCode, format string, args ...interface{}) } type Spinner interface { diff --git a/cli/common/logs.go b/cli/common/logs.go index 5ee15f03..2154a75b 100644 --- a/cli/common/logs.go +++ b/cli/common/logs.go @@ -27,7 +27,7 @@ func NewDiveLogger(infoFilePath string, errorFilePath string) *diveLogger { }) ditLogger := &lumberjack.Logger{ - // Log file abbsolute path, os agnostic + // Log file absolute path, os agnostic Filename: filepath.ToSlash(infoFilePath), LocalTime: true, } @@ -66,69 +66,49 @@ func (d *diveLogger) SetErrorToStderr() { func (d *diveLogger) SetOutputToStdout() { d.log.SetOutput(os.Stdout) } -func (d *diveLogger) Debug(message string) { - - d.log.WithFields(logrus.Fields{ - "level": "🐞 debug", - }).Debug(message) +func (d *diveLogger) logWithFields(level logrus.Level, kind string, format string, args ...interface{}) { + if d.log.IsLevelEnabled(level) { + d.log.WithFields(logrus.Fields{"kind": kind}).Logf(level, format, args...) + } } -func (d *diveLogger) Info(message string) { - d.log.WithFields(logrus.Fields{ - "level": "ℹī¸ info", - }).Info(message) +func (d *diveLogger) Debug(message string) { + d.logWithFields(logrus.DebugLevel, "🐞 debug", message) } -func (d *diveLogger) Warn(message string) { - d.log.WithFields(logrus.Fields{ - "level": "⚠ī¸ warn", - }).Warn(message) +func (d *diveLogger) Info(message string) { + d.logWithFields(logrus.InfoLevel, "ℹī¸ info", message) } -func (d *diveLogger) Error(errorCode ErrorCode, errorMessage string) { - d.log.WithFields(logrus.Fields{ - "level": "🛑 error", - "error_code": errorCode, - }).Error(errorMessage) +func (d *diveLogger) Warn(message string) { + d.logWithFields(logrus.WarnLevel, "⚠ī¸ warn", message) } -func (d *diveLogger) Fatal(errorCode ErrorCode, errorMessage string) { - d.log.WithFields(logrus.Fields{ - "level": "💀 fatal", - "error_code": errorCode, - }).Fatal(errorMessage) +func (d *diveLogger) Error(errorCode ErrorCode, errorMessage string) { + d.logWithFields(logrus.ErrorLevel, "🛑 error", "%s", errorMessage) } -func (d *diveLogger) Infof(message string) { - d.log.WithFields(logrus.Fields{ - "level": "ℹī¸ info", - }).Infof("%s", message) +func (d *diveLogger) Fatal(errorCode ErrorCode, errorMessage string) { + d.logWithFields(logrus.FatalLevel, "💀 fatal", "%s", errorMessage) } -func (d *diveLogger) Warnf(message string) { - d.log.WithFields(logrus.Fields{ - "level": "⚠ī¸ warn", - }).Warnf("%s", message) +func (d *diveLogger) Infof(format string, args ...interface{}) { + d.logWithFields(logrus.InfoLevel, "ℹī¸ info", format, args...) } -func (d *diveLogger) Debugf(message string) { - d.log.WithFields(logrus.Fields{ - "level": "🐞 debug", - }).Debugf("%s", message) +func (d *diveLogger) Warnf(format string, args ...interface{}) { + d.logWithFields(logrus.WarnLevel, "⚠ī¸ warn", format, args...) +} +func (d *diveLogger) Debugf(format string, args ...interface{}) { + d.logWithFields(logrus.DebugLevel, "🐞 debug", format, args...) } -func (d *diveLogger) Errorf(errorCode ErrorCode, errorMessage string) { - d.log.WithFields(logrus.Fields{ - "level": "🛑 error", - "error_code": errorCode, - }).Errorf("%s", errorMessage) +func (d *diveLogger) Errorf(errorCode ErrorCode, format string, args ...interface{}) { + d.logWithFields(logrus.ErrorLevel, "🛑 error", format, args...) } -func (d *diveLogger) Fatalf(errorCode ErrorCode, errorMessage string) { - d.log.WithFields(logrus.Fields{ - "level": "💀 fatal", - "error_code": errorCode, - }).Fatalf("%s", errorMessage) +func (d *diveLogger) Fatalf(errorCode ErrorCode, format string, args ...interface{}) { + d.logWithFields(logrus.FatalLevel, "💀 fatal", format, args...) }