Skip to content

Commit

Permalink
chore: add code comments to commons
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Dec 12, 2023
1 parent 0143f15 commit 1845adf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
5 changes: 0 additions & 5 deletions cli/cmd/utility/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ func clean(cmd *cobra.Command, args []string) {
cliContext.Logger().Error(common.InvalidCommandError, err.Error())
}

if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

enclaves, err := cliContext.Context().GetEnclaves()
if err != nil {
cliContext.Logger().SetErrorToStderr()
Expand Down
1 change: 1 addition & 0 deletions cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
DiveErrorLogFile = "error.log"
DiveOutFile = "dive_%s.json"
ServiceFilePath = "services_%s.json"
DiveAppDir = ".dive"
removeServiceStarlarkScript = `
def run(plan,args):
plan.remove_service(name=args["service_name"])
Expand Down
29 changes: 29 additions & 0 deletions cli/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (dc *diveContext) GetContext() context.Context {
return dc.ctx
}

// The `GetKurtosisContext` function is a method of the `diveContext` struct.
// Used to Get the kurtosis context from the kurtosis engine
func (dc *diveContext) GetKurtosisContext() (*kurtosis_context.KurtosisContext, error) {
dc.mu.Lock()
defer dc.mu.Unlock()
Expand All @@ -41,6 +43,8 @@ func (dc *diveContext) GetKurtosisContext() (*kurtosis_context.KurtosisContext,
return dc.kurtosisContext, nil
}

// The `GetEnclaves` function is a method of the `diveContext` struct.
// Used to Get List Of Enclaves Running / Stopped
func (dc *diveContext) GetEnclaves() ([]EnclaveInfo, error) {
enclavesInfo, err := dc.kurtosisContext.GetEnclaves(dc.ctx)

Expand All @@ -64,6 +68,8 @@ func (dc *diveContext) GetEnclaves() ([]EnclaveInfo, error) {
return enclaves, nil
}

// The `GetEnclaveContext` function is a method of the `diveContext` struct.
// Used to Get the Kurtosis Enclave Context
func (dc *diveContext) GetEnclaveContext(enclaveName string) (*enclaves.EnclaveContext, error) {
// check enclave exist
enclaveInfo, err := dc.checkEnclaveExist(enclaveName)
Expand All @@ -79,6 +85,8 @@ func (dc *diveContext) GetEnclaveContext(enclaveName string) (*enclaves.EnclaveC

}

// The `CleanEnclaves` function is a method of the `diveContext` struct.
// Used to Cleans given Running Enclaves
func (dc *diveContext) CleanEnclaves() ([]*EnclaveInfo, error) {
enclaves, err := dc.kurtosisContext.Clean(dc.ctx, true)

Expand All @@ -95,6 +103,8 @@ func (dc *diveContext) CleanEnclaves() ([]*EnclaveInfo, error) {
return enclaveInfo, nil
}

// The `CleanEnclaveByName` function is a method of the `diveContext` struct.
// Used to Clean given Enclave
func (dc *diveContext) CleanEnclaveByName(enclaveName string) error {

enclaveInfo, err := dc.checkEnclaveExist(enclaveName)
Expand All @@ -109,11 +119,14 @@ func (dc *diveContext) CleanEnclaveByName(enclaveName string) error {
return nil
}

// The `CheckSkippedInstructions` function is a method of the `diveContext` struct.
// Used to Check the Skipped Instructions
func (dc *diveContext) CheckSkippedInstructions(instructions map[string]bool) bool {

return len(instructions) != 0
}

// The `StopService` function is a method of the `diveContext` struct. It is used to stop given service name from a specific enclave.
func (dc *diveContext) StopService(serviceName string, enclaveName string) error {

enclaveContext, err := dc.GetEnclaveContext(enclaveName)
Expand All @@ -131,6 +144,8 @@ func (dc *diveContext) StopService(serviceName string, enclaveName string) error
return nil
}

// The `StopServices` function is a method of the `diveContext` struct. It is used to stop all
// services from a specific enclave.
func (dc *diveContext) StopServices(enclaveName string) error {

enclaveCtx, err := dc.GetEnclaveContext(enclaveName)
Expand Down Expand Up @@ -159,6 +174,8 @@ func (dc *diveContext) StopServices(enclaveName string) error {
return nil
}

// The `RemoveServices` function is a method of the `diveContext` struct. It is used to remove all
// services from a specific enclave.
func (dc *diveContext) RemoveServices(enclaveName string) error {
enclaveCtx, err := dc.GetEnclaveContext(enclaveName)

Expand Down Expand Up @@ -186,6 +203,8 @@ func (dc *diveContext) RemoveServices(enclaveName string) error {
return nil
}

// The `RemoveService` function is a method of the `diveContext` struct. It is used to remove a
// specific service from an enclave.
func (dc *diveContext) RemoveService(serviceName string, enclaveName string) error {
enclaveContext, err := dc.GetEnclaveContext(enclaveName)
if err != nil {
Expand All @@ -202,6 +221,8 @@ func (dc *diveContext) RemoveService(serviceName string, enclaveName string) err
return nil
}

// The `CreateEnclave` function is used to create a new enclave with the specified name. It takes in
// the name of the enclave as a parameter and returns an `EnclaveContext` object and an error.
func (dc *diveContext) CreateEnclave(enclaveName string) (*enclaves.EnclaveContext, error) {

enclaveContext, err := dc.kurtosisContext.CreateEnclave(dc.ctx, enclaveName)
Expand All @@ -213,6 +234,9 @@ func (dc *diveContext) CreateEnclave(enclaveName string) (*enclaves.EnclaveConte
return enclaveContext, nil
}

// The `initKurtosisContext` function is responsible for initializing the Kurtosis context by creating
// a new instance of `KurtosisContext` using the `NewKurtosisContextFromLocalEngine` function from the
// `kurtosis_context` package. If there is an error during initialization, it returns an error.
func (dc *diveContext) initKurtosisContext() (*kurtosis_context.KurtosisContext, error) {

kurtosisContext, err := kurtosis_context.NewKurtosisContextFromLocalEngine()
Expand Down Expand Up @@ -242,6 +266,9 @@ func (dc *diveContext) checkEnclaveExist(enclaveName string) (*EnclaveInfo, erro
}, nil
}

// The `RemoveServicesByServiceNames` function is used to remove multiple services from an enclave. It
// takes in a map of service names and their corresponding IDs, as well as the name of the enclave from
// which the services should be removed.
func (dc *diveContext) RemoveServicesByServiceNames(services map[string]string, enclaveName string) error {
enclaveCtx, err := dc.GetEnclaveContext(enclaveName)

Expand All @@ -263,6 +290,8 @@ func (dc *diveContext) RemoveServicesByServiceNames(services map[string]string,
return nil
}

// The `Exit` function is used to terminate the program with a specified exit status code. It calls the
// `os.Exit` function, which immediately terminates the program with the given status code.
func (dc *diveContext) Exit(statusCode int) {
os.Exit(statusCode)
}
42 changes: 38 additions & 4 deletions cli/common/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"strings"
)

const appDir = ".dive"

type diveFileHandler struct{}

// The function returns a new instance of the diveFileHandler struct.
func NewDiveFileHandler() *diveFileHandler {
return &diveFileHandler{}
}

// The `ReadFile` method is responsible for reading the contents of a file given its file path.
func (df *diveFileHandler) ReadFile(filePath string) ([]byte, error) {

fileData, err := os.ReadFile(filePath)
Expand All @@ -33,6 +34,8 @@ func (df *diveFileHandler) ReadFile(filePath string) ([]byte, error) {
return fileData, nil
}

// The `ReadJson` method is responsible for reading a JSON file and unmarshaling its contents into
// the provided object.
func (df *diveFileHandler) ReadJson(fileName string, obj interface{}) error {

var filePath string
Expand Down Expand Up @@ -62,6 +65,9 @@ func (df *diveFileHandler) ReadJson(fileName string, obj interface{}) error {

return nil
}

// The `ReadAppFile` method is responsible for reading the contents of a file located in the
// application directory.
func (df *diveFileHandler) ReadAppFile(fileName string) ([]byte, error) {

appFilePath, err := df.GetAppDirPathOrAppFilePath(fileName)
Expand All @@ -78,6 +84,8 @@ func (df *diveFileHandler) ReadAppFile(fileName string) ([]byte, error) {
return data, nil
}

// The `WriteAppFile` method is responsible for writing data to a file located in the application
// directory.
func (df *diveFileHandler) WriteAppFile(fileName string, data []byte) error {

appFileDir, err := df.GetAppDirPathOrAppFilePath("")
Expand Down Expand Up @@ -112,6 +120,8 @@ func (df *diveFileHandler) WriteAppFile(fileName string, data []byte) error {
return nil
}

// The `WriteFile` method is responsible for writing data to a file. It takes the file name and the
// data to be written as parameters.
func (df *diveFileHandler) WriteFile(fileName string, data []byte) error {

pwd, err := df.GetPwd()
Expand All @@ -138,6 +148,8 @@ func (df *diveFileHandler) WriteFile(fileName string, data []byte) error {
return nil
}

// The `WriteJson` method is responsible for serializing the provided data object into JSON format and
// writing it to a file.
func (df *diveFileHandler) WriteJson(fileName string, data interface{}) error {

serializedData, err := json.Marshal(data)
Expand All @@ -153,6 +165,10 @@ func (df *diveFileHandler) WriteJson(fileName string, data interface{}) error {
return nil
}

// The `GetPwd()` function is a method of the `diveFileHandler` struct. It is responsible for
// retrieving the present working directory (PWD) and returning it as a string. It uses the
// `os.Getwd()` function to get the PWD and returns it along with any error that occurred during the
// process.
func (df *diveFileHandler) GetPwd() (string, error) {

pwd, err := os.Getwd()
Expand All @@ -162,6 +178,8 @@ func (df *diveFileHandler) GetPwd() (string, error) {
return pwd, err
}

// The `MkdirAll` function is a method of the `diveFileHandler` struct. It is responsible for creating
// a directory at the specified `dirPath` if it does not already exist.
func (df *diveFileHandler) MkdirAll(dirPath string, permission fs.FileMode) error {

_, err := os.Stat(dirPath)
Expand All @@ -177,6 +195,10 @@ func (df *diveFileHandler) MkdirAll(dirPath string, permission fs.FileMode) erro
return nil
}

// The `OpenFile` method is responsible for opening a file given its file path, file open mode, and
// permission. It uses the `os.OpenFile` function to open the file with the specified mode and
// permission. If there is an error during the file opening process, it returns an error with a wrapped
// message. Otherwise, it returns the opened file.
func (df *diveFileHandler) OpenFile(filePath string, fileOpenMode string, permission int) (*os.File, error) {
mode := parseFileOpenMode(fileOpenMode)
file, err := os.OpenFile(filePath, mode, fs.FileMode(permission))
Expand All @@ -188,6 +210,10 @@ func (df *diveFileHandler) OpenFile(filePath string, fileOpenMode string, permis

}

// The `GetHomeDir()` function is a method of the `diveFileHandler` struct. It is responsible for
// retrieving the user's home directory and returning it as a string. It uses the `os.UserHomeDir()`
// function to get the home directory and returns it along with any error that occurred during the
// process.
func (df *diveFileHandler) GetHomeDir() (string, error) {

uhd, err := os.UserHomeDir()
Expand All @@ -197,6 +223,8 @@ func (df *diveFileHandler) GetHomeDir() (string, error) {
return uhd, err
}

// The function `parseFileOpenMode` takes a string representing file open modes separated by "|" and
// returns the corresponding integer value.
func parseFileOpenMode(fileOpenMode string) int {
modes := strings.Split(fileOpenMode, "|")

Expand All @@ -222,6 +250,8 @@ func parseFileOpenMode(fileOpenMode string) int {
return mode
}

// The `RemoveFile` function is a method of the `diveFileHandler` struct. It is responsible for
// removing a file from the file system.
func (df *diveFileHandler) RemoveFile(fileName string) error {

pwd, err := df.GetPwd()
Expand All @@ -244,6 +274,8 @@ func (df *diveFileHandler) RemoveFile(fileName string) error {
return nil
}

// The `RemoveFiles` function is a method of the `diveFileHandler` struct. It is responsible for
// removing multiple files from the file system.
func (df *diveFileHandler) RemoveFiles(fileNames []string) error {

pwd, err := df.GetPwd()
Expand All @@ -266,6 +298,8 @@ func (df *diveFileHandler) RemoveFiles(fileNames []string) error {
return nil
}

// The `GetAppDirPathOrAppFilePath` function is a method of the `diveFileHandler` struct. It is
// responsible for returning the file path of a file located in the application directory.
func (df *diveFileHandler) GetAppDirPathOrAppFilePath(fileName string) (string, error) {

var path string
Expand All @@ -274,9 +308,9 @@ func (df *diveFileHandler) GetAppDirPathOrAppFilePath(fileName string) (string,
return "", WrapMessageToErrorf(err, "Failed To Write App File %s", fileName)
}
if fileName == "" {
path = filepath.Join(uhd, appDir)
path = filepath.Join(uhd, DiveAppDir)
} else {
path = filepath.Join(uhd, appDir, fileName)
path = filepath.Join(uhd, DiveAppDir, fileName)
}

return path, nil
Expand Down

0 comments on commit 1845adf

Please sign in to comment.