Skip to content

Commit

Permalink
feat: logging by enclave name
Browse files Browse the repository at this point in the history
  • Loading branch information
riyasng12 committed Jan 3, 2024
1 parent 797d578 commit ed93085
Show file tree
Hide file tree
Showing 23 changed files with 155 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 4 additions & 2 deletions cli/cmd/bridge/btp/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
8 changes: 5 additions & 3 deletions cli/cmd/bridge/ibc/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion cli/cmd/chains/archway/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/eth/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/hardhat/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/chains/icon/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/kusama/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/neutron/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/chains/polkadot/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 0 additions & 5 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ func run(cmd *cobra.Command, args []string) error {
return nil
}

func init() {
common.GetCli()

}

func Execute() {
err := rootCmd.Execute()

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/social/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/social/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 7 additions & 12 deletions cli/cmd/utility/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -49,30 +49,25 @@ 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 {
cliContext.Logger().SetErrorToStderr()
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())
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/utility/enclaves.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/utility/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/utility/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
33 changes: 16 additions & 17 deletions cli/common/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
60 changes: 58 additions & 2 deletions cli/common/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions cli/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit ed93085

Please sign in to comment.