From 4d8f2b53dfd671d972d10ea995db4b6970cdca20 Mon Sep 17 00:00:00 2001 From: shivamsouravjha Date: Mon, 26 Feb 2024 19:05:27 +0000 Subject: [PATCH] refactor:context cancell is create yaml --- cli/util.go | 5 +++-- pkg/platform/yaml/reportDb/reportDb.go | 2 +- pkg/platform/yaml/yaml.go | 7 +++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cli/util.go b/cli/util.go index 9fe7ecd0e..6560ccb5c 100644 --- a/cli/util.go +++ b/cli/util.go @@ -1,6 +1,7 @@ package cli import ( + "context" "errors" "fmt" "io" @@ -35,9 +36,9 @@ func ExtractInstallationId(isNewConfigPath bool) (string, error) { return installationId, nil } -func GenerateTelemetryConfigFile(logger *zap.Logger, id string) error { +func GenerateTelemetryConfigFile(ctx context.Context, logger *zap.Logger, id string) error { path := utils.FetchHomeDirectory(true) - yaml.CreateYamlFile(path, "installation-id", logger) + yaml.CreateYamlFile(ctx, logger, path, "installation-id") data := []byte{} diff --git a/pkg/platform/yaml/reportDb/reportDb.go b/pkg/platform/yaml/reportDb/reportDb.go index 7b64fdede..bafa684f0 100755 --- a/pkg/platform/yaml/reportDb/reportDb.go +++ b/pkg/platform/yaml/reportDb/reportDb.go @@ -100,7 +100,7 @@ func (fe *TestReport) InsertReport(ctx context.Context, testRunId string, testSe testReport.Name = fmt.Sprintf("report-%v", lastIndex) } - _, err := yaml.CreateYamlFile(fe.Path, testReport.Name, fe.Logger) + _, err := yaml.CreateYamlFile(ctx, fe.Logger, fe.Path, testReport.Name) if err != nil { return err } diff --git a/pkg/platform/yaml/yaml.go b/pkg/platform/yaml/yaml.go index 10cfb2588..7ae69e1c7 100755 --- a/pkg/platform/yaml/yaml.go +++ b/pkg/platform/yaml/yaml.go @@ -35,7 +35,7 @@ func (nd *NetworkTrafficDoc) GetKind() string { func Write(ctx context.Context, logger *zap.Logger, path, fileName string, docRead platform.KindSpecifier) error { // doc, _ := docRead.(*NetworkTrafficDoc) - isFileEmpty, err := CreateYamlFile(path, fileName, logger) + isFileEmpty, err := CreateYamlFile(ctx, logger, path, fileName) if err != nil { return err } @@ -101,7 +101,7 @@ func GetNextID() int64 { } // createYamlFile is used to create the yaml file along with the path directory (if does not exists) -func CreateYamlFile(path string, fileName string, Logger *zap.Logger) (bool, error) { +func CreateYamlFile(ctx context.Context, Logger *zap.Logger, path string, fileName string) (bool, error) { // checks id the yaml exists yamlPath, err := ValidatePath(filepath.Join(path, fileName+".yaml")) if err != nil { @@ -129,8 +129,7 @@ func CreateYamlFile(path string, fileName string, Logger *zap.Logger) (bool, err keployPath = filepath.Join(strings.TrimSuffix(path, filepath.Base(path))) } Logger.Debug("the path to the generated keploy directory", zap.Any("path", keployPath)) - //TODO: use CommandContext - cmd := exec.Command("sudo", "chmod", "-R", "777", keployPath) + cmd := exec.CommandContext(ctx, "sudo", "chmod", "-R", "777", keployPath) err = cmd.Run() if err != nil { Logger.Error("failed to set the permission of keploy directory", zap.Error(err))