mirror of
https://github.com/edgebox-iot/edgeboxctl.git
synced 2026-09-24 05:41:43 +02:00
Release/1.3.0 (#40)
* Implemented scaffolding for BrowserDev tasks and implemented taskGetBrowserDevPassword * Added SetBrowserDevPasswordFile and ReplaceTextInFile funcs * Added browserDevProxyPath * Polished code and added missing pieces * Improved Makefile with info and install + build processes * Added vscode tasks support * Added run command and reverted build-all to old logic, added run vscode task * Added GetBrowserDevStatus task and into schedules * Fixed check for tasks.GetBrowserStatus() * Reverted to no result log in Exec command
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"io"
|
||||
"errors"
|
||||
"os/exec"
|
||||
"bufio"
|
||||
"path/filepath"
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/shirou/gopsutil/host"
|
||||
"github.com/go-yaml/yaml"
|
||||
)
|
||||
|
||||
type cloudflaredTunnelJson struct {
|
||||
@@ -516,3 +518,83 @@ func ApplyUpdates() {
|
||||
utils.WriteOption("UPDATING_SYSTEM", "false")
|
||||
}
|
||||
|
||||
func FetchBrowserDevPasswordFromFile() (string, error) {
|
||||
fmt.Println("Executing FetchBrowserDevPasswordFromFile")
|
||||
|
||||
// Read the "password" entry on the yaml file
|
||||
// Read the yaml file in system.GetPath(BrowserDevPasswordFileLocation)
|
||||
yamlFile, err := ioutil.ReadFile(utils.GetPath(utils.BrowserDevPasswordFileLocation))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Parse the yaml file and get the "password" entry
|
||||
var yamlFileMap yaml.MapSlice
|
||||
err = yaml.Unmarshal(yamlFile, &yamlFileMap)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, item := range yamlFileMap {
|
||||
key, value := item.Key, item.Value
|
||||
if key == "password" {
|
||||
if pwString, ok := value.(string); ok {
|
||||
return pwString, nil
|
||||
} else {
|
||||
return "", errors.New("password value is not a string")
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user