From ed930858a2e2d518121799fe6ae56b5c5f6193fa Mon Sep 17 00:00:00 2001 From: riyasng12 Date: Wed, 3 Jan 2024 16:38:10 +0530 Subject: [PATCH] feat: logging by enclave name --- cli/cmd/bridge/bridge.go | 2 +- cli/cmd/bridge/btp/cmd.go | 6 ++-- cli/cmd/bridge/ibc/cmd.go | 8 +++-- cli/cmd/chains/archway/cmd.go | 2 +- cli/cmd/chains/eth/cmd.go | 2 +- cli/cmd/chains/hardhat/cmd.go | 2 +- cli/cmd/chains/icon/cmd.go | 4 +-- cli/cmd/chains/kusama/cmd.go | 2 +- cli/cmd/chains/neutron/cmd.go | 2 +- cli/cmd/chains/polkadot/cmd.go | 2 +- cli/cmd/root.go | 5 --- cli/cmd/social/discord.go | 2 +- cli/cmd/social/twitter.go | 2 +- cli/cmd/utility/clean.go | 19 ++++------- cli/cmd/utility/enclaves.go | 2 +- cli/cmd/utility/tutorial.go | 2 +- cli/cmd/utility/version.go | 2 +- cli/common/cli.go | 33 +++++++++---------- cli/common/constants.go | 3 +- cli/common/files.go | 60 ++++++++++++++++++++++++++++++++-- cli/common/interfaces.go | 6 ++++ cli/common/types.go | 15 +++++++++ cli/common/utils.go | 28 ++++++++++++++++ 23 files changed, 155 insertions(+), 56 deletions(-) diff --git a/cli/cmd/bridge/bridge.go b/cli/cmd/bridge/bridge.go index 9906dcf3..a66d055b 100644 --- a/cli/cmd/bridge/bridge.go +++ b/cli/cmd/bridge/bridge.go @@ -21,7 +21,7 @@ This will create an relay to connect two different chains and pass any messages Build() func bridge(cmd *cobra.Command, args []string) { - cli := common.GetCli() + cli := common.GetCli(common.EnclaveName) validArgs := cmd.ValidArgs for _, c := range cmd.Commands() { validArgs = append(validArgs, c.Name()) diff --git a/cli/cmd/bridge/btp/cmd.go b/cli/cmd/bridge/btp/cmd.go index 401d9fc9..61070c10 100644 --- a/cli/cmd/bridge/btp/cmd.go +++ b/cli/cmd/bridge/btp/cmd.go @@ -35,7 +35,7 @@ var BtpRelayCmd = common.NewDiveCommandBuilder(). func btpRelay(cmd *cobra.Command, args []string) { - cliContext := common.GetCli() + cliContext := common.GetCli(common.EnclaveName) err := common.ValidateArgs(args) if err != nil { @@ -62,10 +62,12 @@ func btpRelay(cmd *cobra.Command, args []string) { } serviceFileName := fmt.Sprintf(common.DiveOutFile, common.EnclaveName, shortUuid) + serviceName := fmt.Sprintf("btp-bridge-%s-%s", chainA, chainB) - err = cliContext.FileHandler().WriteFile(serviceFileName, []byte(result)) + err = common.WriteBridgeResponseData(serviceName, result, cliContext, serviceFileName) if err != nil { cliContext.Fatal(err) } + cliContext.StopSpinnerIfNotVerbose(fmt.Sprintf("BTP Setup Completed between %s and %s. Please find service details in current working directory(%s)\n", chainA, chainB, serviceFileName), common.DiveLogs) } diff --git a/cli/cmd/bridge/ibc/cmd.go b/cli/cmd/bridge/ibc/cmd.go index bb27dfa9..4a7f9ab0 100644 --- a/cli/cmd/bridge/ibc/cmd.go +++ b/cli/cmd/bridge/ibc/cmd.go @@ -30,7 +30,7 @@ Along with that setup and starts the ibc relayer to establish communication betw func ibcRelay(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) @@ -50,10 +50,12 @@ func ibcRelay(cmd *cobra.Command, args []string) { serviceFileName := fmt.Sprintf(common.DiveOutFile, common.EnclaveName, shortUuid) - err = cliContext.FileHandler().WriteFile(serviceFileName, []byte(result)) + serviceName := fmt.Sprintf("ibc-bridge-%s-%s", chainA, chainB) + + err = common.WriteBridgeResponseData(serviceName, result, cliContext, serviceFileName) if err != nil { cliContext.Fatal(err) } - + cliContext.StopSpinnerIfNotVerbose(fmt.Sprintf("IBC Setup Completed between %s and %s. Please find service details in current working directory(%s)\n", chainA, chainB, serviceFileName), common.DiveLogs) } diff --git a/cli/cmd/chains/archway/cmd.go b/cli/cmd/chains/archway/cmd.go index 57dc1658..a5dd9552 100644 --- a/cli/cmd/chains/archway/cmd.go +++ b/cli/cmd/chains/archway/cmd.go @@ -27,7 +27,7 @@ var ArchwayCmd = common.NewDiveCommandBuilder(). func archway(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) if err != nil { diff --git a/cli/cmd/chains/eth/cmd.go b/cli/cmd/chains/eth/cmd.go index 767db728..17c4609e 100644 --- a/cli/cmd/chains/eth/cmd.go +++ b/cli/cmd/chains/eth/cmd.go @@ -18,7 +18,7 @@ It establishes a connection to the Ethereum network and allows the node in execu func eth(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) diff --git a/cli/cmd/chains/hardhat/cmd.go b/cli/cmd/chains/hardhat/cmd.go index a62402fe..33e6a341 100644 --- a/cli/cmd/chains/hardhat/cmd.go +++ b/cli/cmd/chains/hardhat/cmd.go @@ -17,7 +17,7 @@ It establishes a connection to the hardhat network and allows the node in execut Build() func hardhat(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) diff --git a/cli/cmd/chains/icon/cmd.go b/cli/cmd/chains/icon/cmd.go index 16f56d32..5d56df17 100644 --- a/cli/cmd/chains/icon/cmd.go +++ b/cli/cmd/chains/icon/cmd.go @@ -46,7 +46,7 @@ var IconDecentralizeCmd = common.NewDiveCommandBuilder(). func icon(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) if err != nil { @@ -105,7 +105,7 @@ func icon(cmd *cobra.Command, args []string) { func iconDecentralization(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) diff --git a/cli/cmd/chains/kusama/cmd.go b/cli/cmd/chains/kusama/cmd.go index a6dff01e..97f5622e 100644 --- a/cli/cmd/chains/kusama/cmd.go +++ b/cli/cmd/chains/kusama/cmd.go @@ -43,7 +43,7 @@ var KusamaCmd = common.NewDiveCommandBuilder(). Build() func kusama(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) if err != nil { diff --git a/cli/cmd/chains/neutron/cmd.go b/cli/cmd/chains/neutron/cmd.go index 769a7937..36ad7bf7 100644 --- a/cli/cmd/chains/neutron/cmd.go +++ b/cli/cmd/chains/neutron/cmd.go @@ -25,7 +25,7 @@ var NeutronCmd = common.NewDiveCommandBuilder(). func neutron(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) if err != nil { diff --git a/cli/cmd/chains/polkadot/cmd.go b/cli/cmd/chains/polkadot/cmd.go index 65b594aa..a85b1671 100644 --- a/cli/cmd/chains/polkadot/cmd.go +++ b/cli/cmd/chains/polkadot/cmd.go @@ -38,7 +38,7 @@ var PolkadotCmd = common.NewDiveCommandBuilder(). Build() func polkadot(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index a64ba147..75f1402d 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -41,11 +41,6 @@ func run(cmd *cobra.Command, args []string) error { return nil } -func init() { - common.GetCli() - -} - func Execute() { err := rootCmd.Execute() diff --git a/cli/cmd/social/discord.go b/cli/cmd/social/discord.go index 5c0be3dc..68b8e599 100644 --- a/cli/cmd/social/discord.go +++ b/cli/cmd/social/discord.go @@ -15,7 +15,7 @@ var DiscordCmd = common.NewDiveCommandBuilder(). func discord(cmd *cobra.Command, args []string) { - cli := common.GetCli() + cli := common.GetCli(common.EnclaveName) err := common.ValidateArgs(args) if err != nil { diff --git a/cli/cmd/social/twitter.go b/cli/cmd/social/twitter.go index e95f6289..7a9d72cf 100644 --- a/cli/cmd/social/twitter.go +++ b/cli/cmd/social/twitter.go @@ -20,7 +20,7 @@ community, and follow our social media presence directly from the Twitter homepa func twitter(cmd *cobra.Command, args []string) { - cli := common.GetCli() + cli := common.GetCli(common.EnclaveName) cli.Logger().SetOutputToStdout() err := common.ValidateArgs(args) diff --git a/cli/cmd/utility/clean.go b/cli/cmd/utility/clean.go index f6598d7c..90365811 100644 --- a/cli/cmd/utility/clean.go +++ b/cli/cmd/utility/clean.go @@ -15,7 +15,7 @@ var CleanCmd = common.NewDiveCommandBuilder(). SetRun(clean).Build() func clean(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) @@ -49,22 +49,17 @@ func clean(cmd *cobra.Command, args []string) { cliContext.Logger().SetErrorToStderr() cliContext.Logger().Fatal(common.CodeOf(err), err.Error()) } - for _, enclave := range enclaves { - err = cliContext.FileHandler().RemoveFiles([]string{fmt.Sprintf(common.DiveOutFile, enclave.Name, enclave.ShortUuid), fmt.Sprintf(common.ServiceFilePath, enclave.Name, enclave.ShortUuid)}) - if err != nil { - cliContext.Logger().SetErrorToStderr() - cliContext.Logger().Fatal(common.CodeOf(err), err.Error()) - } + + err = cliContext.FileHandler().RemoveAllDir() + if err != nil { + cliContext.Logger().SetErrorToStderr() + cliContext.Logger().Fatal(common.CodeOf(err), err.Error()) } cliContext.Logger().Info(fmt.Sprintf("Enclaves Cleaned %v", enclavesInfo)) } else { cliContext.StartSpinnerIfNotVerbose(fmt.Sprintf("Cleaning Dive By Enclave %s", common.EnclaveName), common.DiveLogs) - shortUuid, err := cliContext.Context().GetShortUuid(common.EnclaveName) - if err != nil { - cliContext.Fatal(err) - } err = cliContext.Context().CleanEnclaveByName(common.EnclaveName) if err != nil { @@ -72,7 +67,7 @@ func clean(cmd *cobra.Command, args []string) { cliContext.Logger().Fatal(common.CodeOf(err), err.Error()) } - err = cliContext.FileHandler().RemoveFiles([]string{fmt.Sprintf(common.DiveOutFile, common.EnclaveName, shortUuid), fmt.Sprintf(common.ServiceFilePath, common.EnclaveName, shortUuid)}) + err = cliContext.FileHandler().RemoveDir(common.EnclaveName) if err != nil { cliContext.Logger().SetErrorToStderr() cliContext.Logger().Fatal(common.CodeOf(err), err.Error()) diff --git a/cli/cmd/utility/enclaves.go b/cli/cmd/utility/enclaves.go index be17eebb..ed505012 100644 --- a/cli/cmd/utility/enclaves.go +++ b/cli/cmd/utility/enclaves.go @@ -16,7 +16,7 @@ var EnclavesCmd = common.NewDiveCommandBuilder(). SetRun(enclave).Build() func enclave(cmd *cobra.Command, args []string) { - cliContext := common.GetCliWithKurtosisContext() + cliContext := common.GetCliWithKurtosisContext(common.EnclaveName) err := common.ValidateArgs(args) if err != nil { diff --git a/cli/cmd/utility/tutorial.go b/cli/cmd/utility/tutorial.go index 004f3066..ed748d06 100644 --- a/cli/cmd/utility/tutorial.go +++ b/cli/cmd/utility/tutorial.go @@ -20,7 +20,7 @@ var TutorialCmd = common.NewDiveCommandBuilder(). func tutorial(cmd *cobra.Command, args []string) { - cli := common.GetCli() + cli := common.GetCli(common.EnclaveName) cli.Logger().SetOutputToStdout() diff --git a/cli/cmd/utility/version.go b/cli/cmd/utility/version.go index 65f99652..bb8fcdef 100644 --- a/cli/cmd/utility/version.go +++ b/cli/cmd/utility/version.go @@ -29,7 +29,7 @@ var VersionCmd = common.NewDiveCommandBuilder(). func version(cmd *cobra.Command, args []string) { - cli := common.GetCli() + cli := common.GetCli(common.EnclaveName) err := common.ValidateArgs(args) diff --git a/cli/common/cli.go b/cli/common/cli.go index 681bd101..5c2d3ad7 100644 --- a/cli/common/cli.go +++ b/cli/common/cli.go @@ -21,41 +21,40 @@ type Cli struct { } func initCli() (*Cli, error) { - fileHandler := NewDiveFileHandler() - - pwd, err := fileHandler.GetPwd() - - if err != nil { - return nil, WrapMessageToError(err, "Failed To Initialize CLi") - } - errorLogFilePath := filepath.Join(pwd, DiveLogDirectory, DiveErrorLogFile) - infoLogFilePath := filepath.Join(pwd, DiveLogDirectory, DiveDitLogFile) return &Cli{ - log: NewDiveLogger(infoLogFilePath, errorLogFilePath), - spinner: NewDiveSpinner(), - context: NewDiveContext1(), - fileHandler: fileHandler, + spinner: NewDiveSpinner(), + context: NewDiveContext1(), }, nil } -func GetCli() *Cli { +func GetCli(enclaveName string) *Cli { var err error initOnce.Do(func() { cliContext, err = initCli() }) + fileHandler := NewDiveFileHandler() + + pwd, err := fileHandler.GetPwd() + if err != nil { - fmt.Println("Failed to get CLI context:", err) + fmt.Println("Failed To Initialize CLi", err) os.Exit(1) } + logDirPath := fmt.Sprintf(DiveLogDirectory, enclaveName) + errorLogFilePath := filepath.Join(pwd, logDirPath, DiveErrorLogFile) + infoLogFilePath := filepath.Join(pwd, logDirPath, DiveDitLogFile) + + cliContext.log = NewDiveLogger(infoLogFilePath, errorLogFilePath) + cliContext.fileHandler = fileHandler return cliContext } -func GetCliWithKurtosisContext() *Cli { - +func GetCliWithKurtosisContext(enclaveName string) *Cli { + cliContext = GetCli(enclaveName) _, err := cliContext.Context().GetKurtosisContext() if err != nil { diff --git a/cli/common/constants.go b/cli/common/constants.go index 0fa36c40..824d61df 100644 --- a/cli/common/constants.go +++ b/cli/common/constants.go @@ -26,9 +26,10 @@ const ( DivePolkaDotGrafanaPath = "/package_io/grafana.star" DiveDryRun = false DiveDefaultParallelism = 4 - DiveLogDirectory = "/logs/" + DiveLogDirectory = "/logs/%s" DiveDitLogFile = "dive.log" DiveErrorLogFile = "error.log" + DiveOutFileDirectory = "/output/" DiveOutFile = "dive_%s_%s.json" ServiceFilePath = "services_%s_%s.json" DiveAppDir = ".dive" diff --git a/cli/common/files.go b/cli/common/files.go index 39dd6bcd..2f152a6a 100644 --- a/cli/common/files.go +++ b/cli/common/files.go @@ -47,8 +47,13 @@ func (df *diveFileHandler) ReadJson(fileName string, obj interface{}) error { if err != nil { return WrapMessageToErrorf(ErrPath, "Failed to get present working dir %s", err.Error()) } + outputDirPath := filepath.Join(pwd, DiveOutFileDirectory, EnclaveName) - filePath = filepath.Join(pwd, fileName) + err = df.MkdirAll(outputDirPath, 0755) + if err != nil { + return WrapMessageToError(err, "Failed to Create Output Directory") + } + filePath = filepath.Join(outputDirPath, fileName) } @@ -129,7 +134,14 @@ func (df *diveFileHandler) WriteFile(fileName string, data []byte) error { if err != nil { return WrapMessageToErrorf(ErrWriteFile, "%s .Failed to Write File %s", err, fileName) } - filePath := filepath.Join(pwd, fileName) + outputDirPath := filepath.Join(pwd, DiveOutFileDirectory, EnclaveName) + + filePath := filepath.Join(outputDirPath, fileName) + + err = df.MkdirAll(outputDirPath, 0755) + if err != nil { + return WrapMessageToError(err, "Failed to Create Output Directory") + } file, err := df.OpenFile(filePath, "write|append|create|truncate", 0644) @@ -298,6 +310,50 @@ func (df *diveFileHandler) RemoveFiles(fileNames []string) error { return nil } +// The `RemoveDir` function is a method of the `diveFileHandler` struct. It is responsible for +// removing output directories from the file system. +func (df *diveFileHandler) RemoveDir(enclaveName string) error { + + pwd, err := df.GetPwd() + + if err != nil { + return WrapMessageToErrorf(ErrPath, "Failed To Remove Directory") + } + dirPath := filepath.Join(pwd, DiveOutFileDirectory, enclaveName) + + _, err = os.Stat(dirPath) + if err == nil { + err = os.RemoveAll(dirPath) + if err != nil { + return WrapMessageToErrorf(ErrInvalidFile, "%s Failed To Remove Directory %s", err, enclaveName) + } + } + + return nil +} + +// The `RemoveAllDir` function is a method of the `diveFileHandler` struct. It is responsible for +// removing all output directories from the file system. +func (df *diveFileHandler) RemoveAllDir() error { + + pwd, err := df.GetPwd() + + if err != nil { + return WrapMessageToErrorf(ErrPath, "Failed To Remove Directory") + } + dirPath := filepath.Join(pwd, DiveOutFileDirectory) + + _, err = os.Stat(dirPath) + if err == nil { + err = os.RemoveAll(dirPath) + if err != nil { + return WrapMessageToErrorf(ErrInvalidFile, "%s Failed To Remove Output Directory", err) + } + } + + 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) { diff --git a/cli/common/interfaces.go b/cli/common/interfaces.go index a86774a1..9ea8d435 100644 --- a/cli/common/interfaces.go +++ b/cli/common/interfaces.go @@ -163,6 +163,12 @@ type FileHandler interface { // RemoveFiles removes multiple files specified by the fileNames. RemoveFiles(fileNames []string) error + // RemoveDir removes the output directories by enclaveName. + RemoveDir(enclaveName string) error + + // RemoveDir removes all the output directories. + RemoveAllDir() error + // GetAppDirPathOrAppFilePath returns the path to the application directory or a specific file within it // based on the provided fileName. GetAppDirPathOrAppFilePath(fileName string) (string, error) diff --git a/cli/common/types.go b/cli/common/types.go index c3da14b8..ac465da2 100644 --- a/cli/common/types.go +++ b/cli/common/types.go @@ -70,6 +70,21 @@ func (dive *DiveMultipleServiceResponse) EncodeToString() (string, error) { type Services map[string]*DiveServiceResponse +type DiveBridgeResponse struct { + Responses map[string]interface{} `json:"bridge-response"` +} + +type BridgeServices map[string]*DiveBridgeResponse + +func (dive *DiveBridgeResponse) Decode(responseData []byte) (*DiveBridgeResponse, error) { + err := json.Unmarshal(responseData, &dive.Responses) + if err != nil { + return nil, WrapMessageToError(ErrDataUnMarshall, err.Error()) + } + + return dive, nil +} + // The EnclaveInfo type represents information about an enclave, including its name, UUID, short UUID, // creation time, and status. // @property {string} Name - The name of the enclave. diff --git a/cli/common/utils.go b/cli/common/utils.go index f88adfd3..b30e8687 100644 --- a/cli/common/utils.go +++ b/cli/common/utils.go @@ -46,6 +46,34 @@ func WriteServiceResponseData(serviceName string, data DiveServiceResponse, cliC return nil } +// The function writes bridge response data to a JSON file. +func WriteBridgeResponseData(serviceName string, data string, cliContext *Cli, fileName string) error { + bridgeResponse := DiveBridgeResponse{} + diveBridgeResponse, err := bridgeResponse.Decode([]byte(data)) + if err != nil { + return WrapMessageToErrorf(ErrDataUnMarshall, "Failed To Unmarshall data") + } + + var jsonDataFromFile = BridgeServices{} + err = cliContext.FileHandler().ReadJson(fileName, &jsonDataFromFile) + + if err != nil { + return WrapMessageToErrorf(err, "Failed To Read %s", fileName) + } + + _, ok := jsonDataFromFile[serviceName] + if !ok { + jsonDataFromFile[serviceName] = diveBridgeResponse + } + + err = cliContext.FileHandler().WriteJson(fileName, jsonDataFromFile) + if err != nil { + return WrapMessageToErrorf(err, "Failed To Write %s", fileName) + } + + return nil +} + // The OpenFile function opens a file using the appropriate command based on the operating system. func OpenFile(URL string) error { var args []string