Skip to content

Commit

Permalink
feat: add interfaces for context , logger and spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Nov 28, 2023
1 parent cb78cd4 commit 038a934
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
1 change: 1 addition & 0 deletions cli/common/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package common
91 changes: 91 additions & 0 deletions cli/common/interfaces.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions cli/common/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package common
1 change: 1 addition & 0 deletions cli/common/spinner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package common
62 changes: 56 additions & 6 deletions cli/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"time"

"github.com/google/go-github/github"
"github.com/kurtosis-tech/stacktrace"
Expand All @@ -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"`
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 038a934

Please sign in to comment.