Skip to content

Commit a4dd65f

Browse files
committed
remove o_trunc
1 parent 8c72d13 commit a4dd65f

File tree

6 files changed

+841
-540
lines changed

6 files changed

+841
-540
lines changed

cmd/curio/guidedsetup/guidedsetup.go

+115-99
Original file line numberDiff line numberDiff line change
@@ -247,122 +247,138 @@ func complete(d *MigrationData) {
247247
}
248248

249249
func afterRan(d *MigrationData) {
250-
// Determine the current user's shell
251-
shell := os.Getenv("SHELL")
252-
if shell == "" {
253-
fmt.Println("Unable to determine the user's shell.")
254-
return
255-
}
256-
257-
var rcFile string
258-
if strings.Contains(shell, "bash") {
259-
rcFile = ".bashrc"
260-
} else if strings.Contains(shell, "zsh") {
261-
rcFile = ".zshrc"
262-
} else {
263-
d.say(notice, "Not adding DB variables to RC file as shell %s is not BASH or ZSH.", shell)
264-
os.Exit(1)
265-
}
266-
267-
// Get the current user's home directory
268-
usr, err := user.Current()
250+
i, _, err := (&promptui.Select{
251+
Label: d.T("Do you wish to add Harmony DB credentials to your ~/.bashrc or ~/.zshrc file?"),
252+
Items: []string{
253+
d.T("Yes"),
254+
d.T("No")},
255+
Templates: d.selectTemplates,
256+
}).Run()
269257
if err != nil {
270-
d.say(notice, "Error getting user home directory:", err)
258+
d.say(notice, "Aborting remaining steps.", err.Error())
271259
os.Exit(1)
272260
}
273-
rcFilePath := filepath.Join(usr.HomeDir, rcFile)
261+
if i == 0 {
262+
// Determine the current user's shell
263+
shell := os.Getenv("SHELL")
264+
if shell == "" {
265+
fmt.Println("Unable to determine the user's shell.")
266+
return
267+
}
274268

275-
// Read the existing rc file content
276-
file, err := os.OpenFile(rcFilePath, os.O_RDWR|os.O_CREATE, 0644)
277-
if err != nil {
278-
d.say(notice, "Error opening %s file: %s", rcFile, err)
279-
os.Exit(1)
280-
}
281-
defer func(file *os.File) {
282-
_ = file.Close()
283-
}(file)
284-
285-
// Variables to be added or updated
286-
exportLines := map[string]string{
287-
"CURIO_DB_HOST": fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
288-
"CURIO_DB_PORT": fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
289-
"CURIO_DB_NAME": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Database),
290-
"CURIO_DB_USER": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Username),
291-
"CURIO_DB_PASSWORD": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Password),
292-
}
293-
294-
// Flags to track whether we need to append these lines
295-
existingVars := map[string]bool{
296-
"CURIO_DB_HOST": false,
297-
"CURIO_DB_PORT": false,
298-
"CURIO_DB_NAME": false,
299-
"CURIO_DB_USER": false,
300-
"CURIO_DB_PASSWORD": false,
301-
}
302-
303-
var lines []string
304-
scanner := bufio.NewScanner(file)
305-
306-
for scanner.Scan() {
307-
line := scanner.Text()
308-
modified := false
309-
310-
// Check each export line to see if it exists and is not commented out
311-
for key, newValue := range exportLines {
312-
if strings.HasPrefix(line, "export "+key+"=") && !strings.HasPrefix(strings.TrimSpace(line), "#") {
313-
lines = append(lines, newValue)
314-
existingVars[key] = true
315-
modified = true
316-
break
317-
}
269+
var rcFile string
270+
if strings.Contains(shell, "bash") {
271+
rcFile = ".bashrc"
272+
} else if strings.Contains(shell, "zsh") {
273+
rcFile = ".zshrc"
274+
} else {
275+
d.say(notice, "Not adding DB variables to RC file as shell %s is not BASH or ZSH.", shell)
276+
os.Exit(1)
318277
}
319278

320-
// If no modifications were made, retain the original line
321-
if !modified {
322-
lines = append(lines, line)
279+
// Get the current user's home directory
280+
usr, err := user.Current()
281+
if err != nil {
282+
d.say(notice, "Error getting user home directory:", err)
283+
os.Exit(1)
323284
}
324-
}
285+
rcFilePath := filepath.Join(usr.HomeDir, rcFile)
325286

326-
if err := scanner.Err(); err != nil {
327-
d.say(notice, "Error reading %s file: %s", rcFile, err)
328-
os.Exit(1)
329-
}
287+
// Read the existing rc file content
288+
file, err := os.OpenFile(rcFilePath, os.O_RDWR|os.O_CREATE, 0644)
289+
if err != nil {
290+
d.say(notice, "Error opening %s file: %s", rcFile, err)
291+
os.Exit(1)
292+
}
330293

331-
// Append missing export lines
332-
for key, added := range existingVars {
333-
if !added {
334-
lines = append(lines, exportLines[key])
294+
// Variables to be added or updated
295+
exportLines := map[string]string{
296+
"CURIO_DB_HOST": fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
297+
"CURIO_DB_PORT": fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
298+
"CURIO_DB_NAME": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Database),
299+
"CURIO_DB_USER": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Username),
300+
"CURIO_DB_PASSWORD": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Password),
335301
}
336-
}
337302

338-
// Reopen the file in write mode to overwrite with updated content
339-
file, err = os.OpenFile(rcFilePath, os.O_WRONLY|os.O_TRUNC, 0644)
340-
if err != nil {
341-
d.say(notice, "Error opening %s file in write mode:", rcFile, err)
342-
return
343-
}
344-
defer func(file *os.File) {
345-
_ = file.Close()
346-
}(file)
303+
// Flags to track whether we need to append these lines
304+
existingVars := map[string]bool{
305+
"CURIO_DB_HOST": false,
306+
"CURIO_DB_PORT": false,
307+
"CURIO_DB_NAME": false,
308+
"CURIO_DB_USER": false,
309+
"CURIO_DB_PASSWORD": false,
310+
}
347311

348-
// Write all lines back to the rc file
349-
writer := bufio.NewWriter(file)
350-
for _, line := range lines {
351-
if _, err := writer.WriteString(line + "\n"); err != nil {
352-
d.say(notice, "Error writing to %s file: %s", rcFile, err)
353-
return
312+
var lines []string
313+
scanner := bufio.NewScanner(file)
314+
315+
for scanner.Scan() {
316+
line := scanner.Text()
317+
modified := false
318+
319+
// Check each export line to see if it exists and is not commented out
320+
for key, newValue := range exportLines {
321+
if strings.HasPrefix(line, "export "+key+"=") && !strings.HasPrefix(strings.TrimSpace(line), "#") {
322+
lines = append(lines, newValue)
323+
existingVars[key] = true
324+
modified = true
325+
break
326+
}
327+
}
328+
329+
// If no modifications were made, retain the original line
330+
if !modified {
331+
lines = append(lines, line)
332+
}
333+
}
334+
335+
if err := scanner.Err(); err != nil {
336+
d.say(notice, "Error reading %s file: %s", rcFile, err)
337+
os.Exit(1)
338+
}
339+
340+
// Append missing export lines
341+
for key, added := range existingVars {
342+
if !added {
343+
lines = append(lines, exportLines[key])
344+
}
354345
}
355-
}
356346

357-
for i := 1; i < 6; i++ {
358-
err := writer.Flush()
347+
err = file.Close()
359348
if err != nil {
349+
d.say(notice, "Error closing %s file: %s", rcFile, err)
350+
os.Exit(1)
351+
}
352+
353+
// Reopen the file in write mode to overwrite with updated content
354+
file, err = os.OpenFile(rcFilePath, os.O_WRONLY|os.O_APPEND, 0644)
355+
if err != nil {
356+
d.say(notice, "Error opening %s file in write mode:", rcFile, err)
357+
return
358+
}
359+
defer func(file *os.File) {
360+
_ = file.Close()
361+
}(file)
362+
363+
// Write all lines back to the rc file
364+
writer := bufio.NewWriter(file)
365+
for _, line := range lines {
366+
if _, err := writer.WriteString(line + "\n"); err != nil {
367+
d.say(notice, "Error writing to %s file: %s", rcFile, err)
368+
return
369+
}
370+
}
371+
372+
for i := 1; i < 6; i++ {
373+
err := writer.Flush()
374+
if err != nil {
375+
d.say(notice, "Failed to flush thw writes to file %s: %s", rcFile, err)
376+
d.say(notice, "Retrying.......(%d/5)", i)
377+
continue
378+
}
360379
d.say(notice, "Failed to flush thw writes to file %s: %s", rcFile, err)
361-
d.say(notice, "Retrying.......(%d/5)", i)
362-
continue
380+
os.Exit(1)
363381
}
364-
d.say(notice, "Failed to flush thw writes to file %s: %s", rcFile, err)
365-
os.Exit(1)
366382
}
367383

368384
d.say(plain, "Try the web interface with %s ", code.Render("curio run --layers=gui"))

cmd/curio/guidedsetup/servicesetup.go

+63-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package guidedsetup
22

33
import (
4+
"bufio"
5+
"errors"
46
"fmt"
7+
"io"
58
"os"
69
"os/exec"
710
"path/filepath"
@@ -119,6 +122,20 @@ func createEnvFile(d *MigrationData) {
119122
var layers []string
120123
var repoPath, nodeName string
121124

125+
// Define the environment variables to be added or updated
126+
envVars := map[string]string{
127+
"CURIO_LAYERS": fmt.Sprintf("export CURIO_LAYERS=%s", strings.Join(layers, ",")),
128+
"CURIO_ALL_REMAINING_FIELDS_ARE_OPTIONAL": "true",
129+
"CURIO_DB_HOST": fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
130+
"CURIO_DB_PORT": fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
131+
"CURIO_DB_NAME": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Database),
132+
"CURIO_DB_USER": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Username),
133+
"CURIO_DB_PASSWORD": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Password),
134+
"CURIO_REPO_PATH": repoPath,
135+
"CURIO_NODE_NAME": nodeName,
136+
"FIL_PROOFS_USE_MULTICORE_SDR": "1",
137+
}
138+
122139
// Take user input to remaining env vars
123140
for {
124141
i, _, err := (&promptui.Select{
@@ -127,6 +144,7 @@ func createEnvFile(d *MigrationData) {
127144
d.T("CURIO_LAYERS: %s", ""),
128145
d.T("CURIO_REPO_PATH: %s", "~/.curio"),
129146
d.T("CURIO_NODE_NAME: %s", ""),
147+
d.T("Add additional variables like FIL_PROOFS_PARAMETER_CACHE."),
130148
d.T("Continue update the env file.")},
131149
Size: 6,
132150
Templates: d.selectTemplates,
@@ -161,29 +179,60 @@ func createEnvFile(d *MigrationData) {
161179
}
162180
continue
163181
case 3:
164-
// Define the environment variables to be added or updated
165-
envVars := map[string]string{
166-
"CURIO_LAYERS": fmt.Sprintf("export CURIO_LAYERS=%s", strings.Join(layers, ",")),
167-
"CURIO_ALL_REMAINING_FIELDS_ARE_OPTIONAL": "true",
168-
"CURIO_DB_HOST": fmt.Sprintf("export CURIO_DB_HOST=%s", strings.Join(d.HarmonyCfg.Hosts, ",")),
169-
"CURIO_DB_PORT": fmt.Sprintf("export CURIO_DB_PORT=%s", d.HarmonyCfg.Port),
170-
"CURIO_DB_NAME": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Database),
171-
"CURIO_DB_USER": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Username),
172-
"CURIO_DB_PASSWORD": fmt.Sprintf("export CURIO_DB_HOST=%s", d.HarmonyCfg.Password),
173-
"CURIO_REPO_PATH": repoPath,
174-
"CURIO_NODE_NAME": nodeName,
175-
"FIL_PROOFS_USE_MULTICORE_SDR": "1",
182+
// Ask if the user wants to add additional variables
183+
additionalVars, err := (&promptui.Prompt{
184+
Label: d.T("Do you want to add additional variables like FIL_PROOFS_PARAMETER_CACHE? (y/n)"),
185+
Validate: func(input string) error {
186+
if strings.EqualFold(input, "y") || strings.EqualFold(input, "yes") {
187+
return nil
188+
}
189+
if strings.EqualFold(input, "n") || strings.EqualFold(input, "no") {
190+
return nil
191+
}
192+
return errors.New("incorrect input")
193+
},
194+
}).Run()
195+
if err != nil || strings.Contains(strings.ToLower(additionalVars), "n") {
196+
d.say(notice, "No additional variables added")
197+
continue
176198
}
177199

200+
// Capture multiline input for additional variables
201+
fmt.Println("Start typing your additional environment variables one variable per line. Use Ctrl+D to finish:")
202+
reader := bufio.NewReader(os.Stdin)
203+
204+
var additionalEnvVars []string
205+
for {
206+
text, err := reader.ReadString('\n')
207+
if err != nil {
208+
if err == io.EOF {
209+
break // End of input when Ctrl+D is pressed
210+
}
211+
d.say(notice, "Error reading input: %s", err)
212+
os.Exit(1)
213+
}
214+
additionalEnvVars = append(additionalEnvVars, text)
215+
}
216+
217+
for _, envVar := range additionalEnvVars {
218+
parts := strings.SplitN(envVar, "=", 2)
219+
if len(parts) == 2 {
220+
envVars[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
221+
} else {
222+
d.say(notice, "Skipping invalid input: %s", envVar)
223+
}
224+
}
225+
continue
226+
case 4:
178227
// Open the file with truncation (this clears the file if it exists)
179228
file, err := os.OpenFile(envFilePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
180229
if err != nil {
181230
d.say(notice, "Error opening or creating file %s: %s", envFilePath, err)
182231
os.Exit(1)
183232
}
184-
defer func() {
233+
defer func(file *os.File) {
185234
_ = file.Close()
186-
}()
235+
}(file)
187236

188237
// Write the new environment variables to the file
189238
for key, value := range envVars {

0 commit comments

Comments
 (0)