From 664e2c2e7ca52876bad0d6b82c675961cef36f47 Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Tue, 3 Dec 2024 23:37:13 +0100 Subject: [PATCH] Added SetBrowserDevPasswordFile and ReplaceTextInFile funcs --- internal/system/system.go | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/internal/system/system.go b/internal/system/system.go index 2249579..14687af 100644 --- a/internal/system/system.go +++ b/internal/system/system.go @@ -548,3 +548,53 @@ func FetchBrowserDevPasswordFromFile() (string, error) { return "", errors.New("password key not found") } +func SetBrowserDevPasswordFile(password string) error { + // Get current password from file + currentPassword, err := FetchBrowserDevPasswordFromFile() + if err != nil { + fmt.Println("Error fetching current password from file.") + return err + } + + // Write the new password on the file using ReplaceTextInFile + err = ReplaceTextInFile(utils.GetPath(utils.BrowserDevPasswordFileLocation), currentPassword, password) + if err != nil { + fmt.Println("Error writing new password to file.") + return err + } + + return nil +} + +func ReplaceTextInFile(filePath string, oldText string, newText string) error { + // Open the file for reading + file, err := os.OpenFile(filePath, os.O_RDWR, 0644) + if err != nil { + return err + } + + // Read the file contents + data, err := ioutil.ReadAll(file) + if err != nil { + return err + } + + // Close the file + err = file.Close() + if err != nil { + return err + } + + // Replace the text in the file + newData := strings.Replace(string(data), oldText, newText, -1) + + // Write the new data back to the file + err = ioutil.WriteFile(filePath, []byte(newData), 0644) + if err != nil { + return err + } + + return nil +} + +