Skip to content

Commit

Permalink
remove o_trunc
Browse files Browse the repository at this point in the history
  • Loading branch information
LexLuthr committed Oct 11, 2024
1 parent 8c72d13 commit a4dd65f
Show file tree
Hide file tree
Showing 6 changed files with 841 additions and 540 deletions.
214 changes: 115 additions & 99 deletions cmd/curio/guidedsetup/guidedsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,122 +247,138 @@ func complete(d *MigrationData) {
}

func afterRan(d *MigrationData) {
// Determine the current user's shell
shell := os.Getenv("SHELL")
if shell == "" {
fmt.Println("Unable to determine the user's shell.")
return
}

var rcFile string
if strings.Contains(shell, "bash") {
rcFile = ".bashrc"
} else if strings.Contains(shell, "zsh") {
rcFile = ".zshrc"
} else {
d.say(notice, "Not adding DB variables to RC file as shell %s is not BASH or ZSH.", shell)
os.Exit(1)
}

// Get the current user's home directory
usr, err := user.Current()
i, _, err := (&promptui.Select{
Label: d.T("Do you wish to add Harmony DB credentials to your ~/.bashrc or ~/.zshrc file?"),
Items: []string{
d.T("Yes"),
d.T("No")},
Templates: d.selectTemplates,
}).Run()
if err != nil {
d.say(notice, "Error getting user home directory:", err)
d.say(notice, "Aborting remaining steps.", err.Error())
os.Exit(1)
}
rcFilePath := filepath.Join(usr.HomeDir, rcFile)
if i == 0 {
// Determine the current user's shell
shell := os.Getenv("SHELL")
if shell == "" {
fmt.Println("Unable to determine the user's shell.")
return
}

// Read the existing rc file content
file, err := os.OpenFile(rcFilePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
d.say(notice, "Error opening %s file: %s", rcFile, err)
os.Exit(1)
}
defer func(file *os.File) {
_ = file.Close()
}(file)

// Variables to be added or updated
exportLines := map[string]string{
"CURIO_DB_HOST": fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
"CURIO_DB_PORT": fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
"CURIO_DB_NAME": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Database),
"CURIO_DB_USER": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Username),
"CURIO_DB_PASSWORD": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Password),
}

// Flags to track whether we need to append these lines
existingVars := map[string]bool{
"CURIO_DB_HOST": false,
"CURIO_DB_PORT": false,
"CURIO_DB_NAME": false,
"CURIO_DB_USER": false,
"CURIO_DB_PASSWORD": false,
}

var lines []string
scanner := bufio.NewScanner(file)

for scanner.Scan() {
line := scanner.Text()
modified := false

// Check each export line to see if it exists and is not commented out
for key, newValue := range exportLines {
if strings.HasPrefix(line, "export "+key+"=") && !strings.HasPrefix(strings.TrimSpace(line), "#") {
lines = append(lines, newValue)
existingVars[key] = true
modified = true
break
}
var rcFile string
if strings.Contains(shell, "bash") {
rcFile = ".bashrc"
} else if strings.Contains(shell, "zsh") {
rcFile = ".zshrc"
} else {
d.say(notice, "Not adding DB variables to RC file as shell %s is not BASH or ZSH.", shell)
os.Exit(1)
}

// If no modifications were made, retain the original line
if !modified {
lines = append(lines, line)
// Get the current user's home directory
usr, err := user.Current()
if err != nil {
d.say(notice, "Error getting user home directory:", err)
os.Exit(1)
}
}
rcFilePath := filepath.Join(usr.HomeDir, rcFile)

if err := scanner.Err(); err != nil {
d.say(notice, "Error reading %s file: %s", rcFile, err)
os.Exit(1)
}
// Read the existing rc file content
file, err := os.OpenFile(rcFilePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
d.say(notice, "Error opening %s file: %s", rcFile, err)
os.Exit(1)
}

// Append missing export lines
for key, added := range existingVars {
if !added {
lines = append(lines, exportLines[key])
// Variables to be added or updated
exportLines := map[string]string{
"CURIO_DB_HOST": fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
"CURIO_DB_PORT": fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
"CURIO_DB_NAME": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Database),
"CURIO_DB_USER": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Username),
"CURIO_DB_PASSWORD": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Password),
}
}

// Reopen the file in write mode to overwrite with updated content
file, err = os.OpenFile(rcFilePath, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
d.say(notice, "Error opening %s file in write mode:", rcFile, err)
return
}
defer func(file *os.File) {
_ = file.Close()
}(file)
// Flags to track whether we need to append these lines
existingVars := map[string]bool{
"CURIO_DB_HOST": false,
"CURIO_DB_PORT": false,
"CURIO_DB_NAME": false,
"CURIO_DB_USER": false,
"CURIO_DB_PASSWORD": false,
}

// Write all lines back to the rc file
writer := bufio.NewWriter(file)
for _, line := range lines {
if _, err := writer.WriteString(line + "\n"); err != nil {
d.say(notice, "Error writing to %s file: %s", rcFile, err)
return
var lines []string
scanner := bufio.NewScanner(file)

for scanner.Scan() {
line := scanner.Text()
modified := false

// Check each export line to see if it exists and is not commented out
for key, newValue := range exportLines {
if strings.HasPrefix(line, "export "+key+"=") && !strings.HasPrefix(strings.TrimSpace(line), "#") {
lines = append(lines, newValue)
existingVars[key] = true
modified = true
break
}
}

// If no modifications were made, retain the original line
if !modified {
lines = append(lines, line)
}
}

if err := scanner.Err(); err != nil {
d.say(notice, "Error reading %s file: %s", rcFile, err)
os.Exit(1)
}

// Append missing export lines
for key, added := range existingVars {
if !added {
lines = append(lines, exportLines[key])
}
}
}

for i := 1; i < 6; i++ {
err := writer.Flush()
err = file.Close()
if err != nil {
d.say(notice, "Error closing %s file: %s", rcFile, err)
os.Exit(1)
}

// Reopen the file in write mode to overwrite with updated content
file, err = os.OpenFile(rcFilePath, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
d.say(notice, "Error opening %s file in write mode:", rcFile, err)
return
}
defer func(file *os.File) {
_ = file.Close()
}(file)

// Write all lines back to the rc file
writer := bufio.NewWriter(file)
for _, line := range lines {
if _, err := writer.WriteString(line + "\n"); err != nil {
d.say(notice, "Error writing to %s file: %s", rcFile, err)
return
}
}

for i := 1; i < 6; i++ {
err := writer.Flush()
if err != nil {
d.say(notice, "Failed to flush thw writes to file %s: %s", rcFile, err)
d.say(notice, "Retrying.......(%d/5)", i)
continue
}
d.say(notice, "Failed to flush thw writes to file %s: %s", rcFile, err)
d.say(notice, "Retrying.......(%d/5)", i)
continue
os.Exit(1)
}
d.say(notice, "Failed to flush thw writes to file %s: %s", rcFile, err)
os.Exit(1)
}

d.say(plain, "Try the web interface with %s ", code.Render("curio run --layers=gui"))
Expand Down
77 changes: 63 additions & 14 deletions cmd/curio/guidedsetup/servicesetup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package guidedsetup

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -119,6 +122,20 @@ func createEnvFile(d *MigrationData) {
var layers []string
var repoPath, nodeName string

// Define the environment variables to be added or updated
envVars := map[string]string{
"CURIO_LAYERS": fmt.Sprintf("export CURIO_LAYERS=%s", strings.Join(layers, ",")),
"CURIO_ALL_REMAINING_FIELDS_ARE_OPTIONAL": "true",
"CURIO_DB_HOST": fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
"CURIO_DB_PORT": fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
"CURIO_DB_NAME": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Database),
"CURIO_DB_USER": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Username),
"CURIO_DB_PASSWORD": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Password),
"CURIO_REPO_PATH": repoPath,
"CURIO_NODE_NAME": nodeName,
"FIL_PROOFS_USE_MULTICORE_SDR": "1",
}

// Take user input to remaining env vars
for {
i, _, err := (&promptui.Select{
Expand All @@ -127,6 +144,7 @@ func createEnvFile(d *MigrationData) {
d.T("CURIO_LAYERS: %s", ""),
d.T("CURIO_REPO_PATH: %s", "~/.curio"),
d.T("CURIO_NODE_NAME: %s", ""),
d.T("Add additional variables like FIL_PROOFS_PARAMETER_CACHE."),
d.T("Continue update the env file.")},
Size: 6,
Templates: d.selectTemplates,
Expand Down Expand Up @@ -161,29 +179,60 @@ func createEnvFile(d *MigrationData) {
}
continue
case 3:
// Define the environment variables to be added or updated
envVars := map[string]string{
"CURIO_LAYERS": fmt.Sprintf("export CURIO_LAYERS=%s", strings.Join(layers, ",")),
"CURIO_ALL_REMAINING_FIELDS_ARE_OPTIONAL": "true",
"CURIO_DB_HOST": fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
"CURIO_DB_PORT": fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
"CURIO_DB_NAME": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Database),
"CURIO_DB_USER": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Username),
"CURIO_DB_PASSWORD": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Password),
"CURIO_REPO_PATH": repoPath,
"CURIO_NODE_NAME": nodeName,
"FIL_PROOFS_USE_MULTICORE_SDR": "1",
// Ask if the user wants to add additional variables
additionalVars, err := (&promptui.Prompt{
Label: d.T("Do you want to add additional variables like FIL_PROOFS_PARAMETER_CACHE? (y/n)"),
Validate: func(input string) error {
if strings.EqualFold(input, "y") || strings.EqualFold(input, "yes") {
return nil
}
if strings.EqualFold(input, "n") || strings.EqualFold(input, "no") {
return nil
}
return errors.New("incorrect input")
},
}).Run()
if err != nil || strings.Contains(strings.ToLower(additionalVars), "n") {
d.say(notice, "No additional variables added")
continue
}

// Capture multiline input for additional variables
fmt.Println("Start typing your additional environment variables one variable per line. Use Ctrl+D to finish:")
reader := bufio.NewReader(os.Stdin)

var additionalEnvVars []string
for {
text, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break // End of input when Ctrl+D is pressed
}
d.say(notice, "Error reading input: %s", err)
os.Exit(1)
}
additionalEnvVars = append(additionalEnvVars, text)
}

for _, envVar := range additionalEnvVars {
parts := strings.SplitN(envVar, "=", 2)
if len(parts) == 2 {
envVars[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
} else {
d.say(notice, "Skipping invalid input: %s", envVar)
}
}
continue
case 4:
// Open the file with truncation (this clears the file if it exists)
file, err := os.OpenFile(envFilePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
d.say(notice, "Error opening or creating file %s: %s", envFilePath, err)
os.Exit(1)
}
defer func() {
defer func(file *os.File) {
_ = file.Close()
}()
}(file)

// Write the new environment variables to the file
for key, value := range envVars {
Expand Down
Loading

0 comments on commit a4dd65f

Please sign in to comment.