Skip to content

Commit

Permalink
fix outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
LexLuthr committed Oct 14, 2024
1 parent f8dc1f8 commit 4fe510c
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 678 deletions.
2 changes: 1 addition & 1 deletion apt/make_debs.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func part2(base, product, extra string) {
OrPanic(os.MkdirAll(base, 0755))
OrPanic(copyFile("curio", path.Join(base, "curio")))
OrPanic(copyFile("sptool", path.Join(base, "sptool")))
base = path.Join(dir, "lib", "systemd", "system")
base = path.Join(dir, "etc", "systemd", "system")
OrPanic(os.MkdirAll(base, 0755))
OrPanic(copyFile("apt/curio.service", path.Join(base, "curio.service")))
base = path.Join(dir, "usr", "share", "bash-completion", "completions")
Expand Down
78 changes: 10 additions & 68 deletions cmd/curio/guidedsetup/guidedsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,74 +284,16 @@ func afterRan(d *MigrationData) {
}
rcFilePath := filepath.Join(usr.HomeDir, rcFile)

// 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)
}

// 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
}
}

// 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])
}
}

err = file.Close()
if err != nil {
d.say(notice, "Error closing %s file: %s", rcFile, err)
os.Exit(1)
lines := []string{
fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
fmt.Sprintf("export CURIO_DB_USER=%s", d.HarmonyCfg.Username),
fmt.Sprintf("export CURIO_DB_PASSWORD=%s", d.HarmonyCfg.Password),
fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
fmt.Sprintf("export CURIO_DB_NAME=%s", d.HarmonyCfg.Database),
}

// Reopen the file in write mode to overwrite with updated content
file, err = os.OpenFile(rcFilePath, os.O_WRONLY|os.O_APPEND, 0644)
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
Expand All @@ -372,12 +314,12 @@ func afterRan(d *MigrationData) {
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, "Failed to flush the 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)
os.Exit(1)
d.say(notice, "Finished updating the %s file", rcFile)
break
}
}

Expand Down
65 changes: 28 additions & 37 deletions cmd/curio/guidedsetup/servicesetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,20 @@ func createEnvFile(d *MigrationData) {
// Define the path to the environment file
envFilePath := "/etc/curio.env"

var layers []string
var repoPath, nodeName string
var layers, additionalEnvVars, envVars []string

envVars := map[string]string{}
repoPath := "~/.curio"
nodeName := "ChangeME"

// Take user input to remaining env vars
for {
i, _, err := (&promptui.Select{
Label: d.T("Enter the info to configure your Curio node"),
Items: []string{
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("CURIO_LAYERS: %s", layers),
d.T("CURIO_REPO_PATH: %s", repoPath),
d.T("CURIO_NODE_NAME: %s", nodeName),
d.T("Add additional variables like FIL_PROOFS_PARAMETER_CACHE: %s", envVars),
d.T("Continue update the env file.")},
Size: 6,
Templates: d.selectTemplates,
Expand Down Expand Up @@ -189,7 +189,6 @@ func createEnvFile(d *MigrationData) {
d.say(plain, "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 {
Expand All @@ -205,47 +204,39 @@ func createEnvFile(d *MigrationData) {
for _, envVar := range additionalEnvVars {
parts := strings.SplitN(envVar, "=", 2)
if len(parts) == 2 {
envVars[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
envVars = append(envVars, fmt.Sprintf("%s=%s", strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])))
} else {
d.say(notice, "Skipping invalid input: %s", envVar)
}
}
continue
case 4:
// Define the environment variables to be added or updated
defenvVars := 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",
defenvVars := []string{
fmt.Sprintf("CURIO_LAYERS=%s", strings.Join(layers, ",")),
"CURIO_ALL_REMAINING_FIELDS_ARE_OPTIONAL=true",
fmt.Sprintf("CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
fmt.Sprintf("CURIO_DB_USER=%s", d.HarmonyCfg.Username),
fmt.Sprintf("CURIO_DB_PASSWORD=%s", d.HarmonyCfg.Password),
fmt.Sprintf("CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
fmt.Sprintf("CURIO_DB_NAME=%s", d.HarmonyCfg.Database),
fmt.Sprintf("CURIO_REPO_PATH=%s", repoPath),
fmt.Sprintf("CURIO_NODE_NAME=%s", nodeName),
"FIL_PROOFS_USE_MULTICORE_SDR=1",
}
for s, s2 := range defenvVars {
envVars[s] = s2
var w string
for _, s := range defenvVars {
w += fmt.Sprintf("%s\n", s)
}

// 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)
for _, s := range envVars {
w += fmt.Sprintf("%s\n", s)
}
defer func(file *os.File) {
_ = file.Close()
}(file)

// Write the new environment variables to the file
for key, value := range envVars {
line := fmt.Sprintf("%s=%s\n", key, value)
if _, err := file.WriteString(line); err != nil {
d.say(notice, "Error writing to file %s: %s", envFilePath, err)
os.Exit(1)
}
err = os.WriteFile(envFilePath, []byte(w), 0644)
if err != nil {
d.say(notice, "Error writing to file %s: %s", envFilePath, err)
os.Exit(1)
}
d.say(notice, "Service env file /etc/curio.env created successfully.")
return
Expand Down
Loading

0 comments on commit 4fe510c

Please sign in to comment.