From 038a93486e7e762fb93645fda16f2121edc831c9 Mon Sep 17 00:00:00 2001 From: shreyasbhat0 Date: Tue, 28 Nov 2023 19:00:10 +0530 Subject: [PATCH] feat: add interfaces for context , logger and spinner --- cli/common/constants.go | 2 +- cli/common/files.go | 1 + cli/common/interfaces.go | 91 ++++++++++++++++++++++++++++++++++++++++ cli/common/logs.go | 1 + cli/common/spinner.go | 1 + cli/common/types.go | 62 ++++++++++++++++++++++++--- 6 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 cli/common/files.go create mode 100644 cli/common/interfaces.go create mode 100644 cli/common/logs.go create mode 100644 cli/common/spinner.go diff --git a/cli/common/constants.go b/cli/common/constants.go index a66fa8fd..a25a7c11 100644 --- a/cli/common/constants.go +++ b/cli/common/constants.go @@ -25,7 +25,7 @@ const ( DiveIconNodeAlreadyRunning = "Icon Node Already Running" DiveLogDirectory = "/logs/" DiveDitLogFile = "divelog.log" - DiveErorLogFile = "error.log" + DiveErrorLogFile = "error.log" DiveOutFile = "dive.json" ServiceFilePath = "services.json" starlarkScript = ` diff --git a/cli/common/files.go b/cli/common/files.go new file mode 100644 index 00000000..805d0c79 --- /dev/null +++ b/cli/common/files.go @@ -0,0 +1 @@ +package common diff --git a/cli/common/interfaces.go b/cli/common/interfaces.go new file mode 100644 index 00000000..d31a4307 --- /dev/null +++ b/cli/common/interfaces.go @@ -0,0 +1,91 @@ +package common + +import ( + "os" + + "github.com/kurtosis-tech/kurtosis/api/golang/core/kurtosis_core_rpc_api_bindings" + "github.com/spf13/cobra" +) + +type Logger interface { + SetOutput(*os.File) + Debug(errorCode int8, errorMessage string) + Info(errorCode int8, errorMessage string) + Warn(errorCode int8, errorMessage string) + Error(errorCode int8, errorMessage string) + Fatal(errorCode int8, errorMessage string) + Infof(format string, errorCode int8, errorMessage string) + Warnf(format string, errorCode int8, errorMessage string) + Errorf(format string, errorCode int8, errorMessage string) + Fatalf(format string, errorCode int8, errorMessage string) +} + +type Spinner interface { + SetMessage(message string, color string) + SetColor(color string) + Start(message string) + Stop(message string) +} + +type Context interface { + CheckSkippedInstructions() + CleanAll() + Clean(enclaveName string) + CreateEnclave(enclaveName string) + GetEnclaves() []string + GetSerializedData(response chan *kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine) (string, map[string]string, map[string]bool, error) + InitialiseKurtosisContext() + StopServices() + StopService() +} + +type FileHandler interface { + ReadFromFile(filePath string) ([]byte, error) + ReadFromJson(filePath string, obj interface{}) (string, error) + WriteToFile(filePath string, data []byte) error + WriteToJson(filePath string, data interface{}) error +} + +// CommandBuilder is an interface for building a Cobra command. +type CommandBuilder interface { + // AddCommand adds a subcommand to the command. + AddCommand(cmd *cobra.Command) CommandBuilder + + // Add Persistant Bool Flag + AddBoolPersistantFlag(p *bool, name string, value bool, usage string) CommandBuilder + + // Add Persistant Bool Flag with Short hand + AddBoolPersistantFlagWithShortHand(p *bool, name string, value bool, usage string, shorthand string) CommandBuilder + + // Add Persistant String Flag + AddStringPersistantFlag(p *string, name string, value string, usage string) CommandBuilder + + // Add Persistant String Flag with Short hand + AddStringPersistantFlagWithShortHand(p *string, name string, shorthand string, value string, usage string) CommandBuilder + + // Add StringFlag adds a string flag to the command that persists + AddStringFlag(name string, value string, usage string) CommandBuilder + + // Add StringFlag adds a string flag to the command that persists with short hand + AddStringFlagWithShortHand(p *string, name string, shorthand string, value string, usage string) CommandBuilder + + // Add BooFlag adds a boolean flag to the command that persists + AddBoolFlag(name string, value bool, usage string) CommandBuilder + + AddBoolFlagWithShortHand(name string, shorthand string, value bool, usage string) CommandBuilder + + // Build constructs and returns the Cobra command. + Build() *cobra.Command + + // SetUse sets the Use field of the command. + SetUse(use string) CommandBuilder + + // SetShort sets the Short field of the command. + SetShort(short string) CommandBuilder + + // SetLong sets the Long field of the command. + SetLong(long string) CommandBuilder + + // SetRun sets the Run field of the command. + SetRun(run func(cmd *cobra.Command, args []string)) CommandBuilder +} diff --git a/cli/common/logs.go b/cli/common/logs.go new file mode 100644 index 00000000..805d0c79 --- /dev/null +++ b/cli/common/logs.go @@ -0,0 +1 @@ +package common diff --git a/cli/common/spinner.go b/cli/common/spinner.go new file mode 100644 index 00000000..805d0c79 --- /dev/null +++ b/cli/common/spinner.go @@ -0,0 +1 @@ +package common diff --git a/cli/common/types.go b/cli/common/types.go index 63fb17a3..ed4e395f 100644 --- a/cli/common/types.go +++ b/cli/common/types.go @@ -10,6 +10,7 @@ import ( "os/exec" "path/filepath" "runtime" + "time" "github.com/google/go-github/github" "github.com/kurtosis-tech/stacktrace" @@ -18,6 +19,9 @@ import ( "github.com/sirupsen/logrus" ) +var lastChecked time.Time +var latestVersion = "" + type DiveserviceResponse struct { ServiceName string `json:"service_name,omitempty"` PublicEndpoint string `json:"endpoint_public,omitempty"` @@ -74,17 +78,63 @@ func GetLatestVersion() string { // Repo Name repo := "DIVE" owner := "HugoByte" + userHomeDir, err := os.UserHomeDir() - // Create a new github client - client := github.NewClient(nil) - release, _, err := client.Repositories.GetLatestRelease(context.Background(), owner, repo) if err != nil { fmt.Println(err) return "" } + cachedFile := filepath.Join(userHomeDir, "/.dive/version_cache.txt") + + if time.Since(lastChecked).Hours() > 1 { + cachedVersion, err := ReadConfigFile(cachedFile) + fmt.Println("here ") + + if err == nil && string(cachedVersion) != "" { + latestVersion = string(cachedVersion) + fmt.Println("here 1") + } else { + fmt.Println("here 2") + client := github.NewClient(nil) + release, _, err := client.Repositories.GetLatestRelease(context.Background(), owner, repo) + if err != nil { + fmt.Println(err) + return "" + } + + latestVersion = release.GetName() + writeCache(cachedFile, latestVersion) + } + lastChecked = time.Now() + + } + + return latestVersion +} + +func writeCache(filePath string, latestVersion string) { + // Extract the directory path from the file path + dir := filepath.Dir(filePath) + + // Create the directory if it does not exist + if err := os.MkdirAll(dir, os.ModePerm); err != nil { + fmt.Println("Error creating directory:", err) + return + } + + // Write the latest version to the cache file + file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + fmt.Println("Error opening file:", err) + return + } - // Print the release version. - return release.GetName() + defer file.Close() + + _, err = file.WriteString(latestVersion) + if err != nil { + fmt.Println("Error writing to cache:", err) + } } func ReadConfigFile(filePath string) ([]byte, error) { @@ -146,7 +196,7 @@ func setupLogger() *logrus.Logger { }) ditFilePath := pwd + DiveLogDirectory + DiveDitLogFile - errorFilePath := pwd + DiveLogDirectory + DiveErorLogFile + errorFilePath := pwd + DiveLogDirectory + DiveErrorLogFile ditLogger := &lumberjack.Logger{ // Log file abbsolute path, os agnostic