Merge lp:~jamesodhunt/snappy/move-utility-functions into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by James Hunt
Status: Work in progress
Proposed branch: lp:~jamesodhunt/snappy/move-utility-functions
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 266 lines (+122/-106)
2 files modified
partition/bootloader_uboot.go (+0/-106)
partition/utils.go (+122/-0)
To merge this branch: bzr merge lp:~jamesodhunt/snappy/move-utility-functions
Reviewer Review Type Date Requested Status
Sergio Schvezov Needs Fixing
Review via email: mp+248340@code.launchpad.net

Description of the change

* partition/bootloader_uboot.go: Move utility functions to utils.go.
* partition.utils.go: runCommandWithStdout(): Fix bug where an
  additional empty line is included

To post a comment you must log in.
Revision history for this message
Michael Vogt (mvo) wrote :

Thanks for this branch. Some comments inline (I copied them from https://code.launchpad.net/~jamesodhunt/snappy/add-lsblk-cache/+merge/248245 I hope I did not forgot a relevant one).

I also feel that the new functions in utils.go needs tests. It does not have to be done in this MP and I'm happy to merge this once my questions below are answered but I think we should add them soon.

Revision history for this message
Sergio Schvezov (sergiusens) wrote :

I added a couple of inline comments and have additional comments here:

* it doesn't seem hard to add tests, can you add them?
* it seems the original intention was to move this to a package itself, not a compilation unit, what made you make your mind up on putting it into the same package?

Thanks.

review: Needs Fixing
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

One more inline comment about the output last line trim.

review: Needs Fixing
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

I set this to work in progress to clear out the review queue

Unmerged revisions

135. By James Hunt

* partition/bootloader_uboot.go: Move utility functions to utils.go.
* partition.utils.go: runCommandWithStdout(): Fix bug where an
  additional empty line is included in the output.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'partition/bootloader_uboot.go'
2--- partition/bootloader_uboot.go 2015-01-27 08:25:19 +0000
3+++ partition/bootloader_uboot.go 2015-02-03 09:40:51 +0000
4@@ -5,7 +5,6 @@
5 package partition
6
7 import (
8- "bufio"
9 "fmt"
10 "os"
11 "path"
12@@ -172,46 +171,6 @@
13 return u.otherRootfs
14 }
15
16-// FIXME: put into utils package
17-func readLines(path string) (lines []string, err error) {
18-
19- file, err := os.Open(path)
20-
21- if err != nil {
22- return nil, err
23- }
24-
25- defer file.Close()
26-
27- scanner := bufio.NewScanner(file)
28- for scanner.Scan() {
29- lines = append(lines, scanner.Text())
30- }
31-
32- return lines, scanner.Err()
33-}
34-
35-// FIXME: put into utils package
36-func writeLines(lines []string, path string) (err error) {
37-
38- file, err := os.Create(path)
39-
40- if err != nil {
41- return err
42- }
43-
44- defer file.Close()
45-
46- writer := bufio.NewWriter(file)
47-
48- for _, line := range lines {
49- if _, err = fmt.Fprintln(writer, line); err != nil {
50- return err
51- }
52- }
53- return writer.Flush()
54-}
55-
56 // Returns name=value entries from the specified file, removing all
57 // blank lines and comments.
58 func getNameValuePairs(file string) (vars []string, err error) {
59@@ -379,68 +338,3 @@
60 return err
61 }
62
63-// Write lines to file atomically. File does not have to preexist.
64-// FIXME: put into utils package
65-func atomicFileUpdate(file string, lines []string) (err error) {
66- tmpFile := fmt.Sprintf("%s.NEW", file)
67-
68- if err := writeLines(lines, tmpFile); err != nil {
69- return err
70- }
71-
72- // atomic update
73- if err = os.Rename(tmpFile, file); err != nil {
74- return err
75- }
76-
77- return err
78-}
79-
80-// Rewrite the specified file, applying the specified set of changes.
81-// Lines not in the changes slice are left alone.
82-// If the original file does not contain any of the name entries (from
83-// the corresponding ConfigFileChange objects), those entries are
84-// appended to the file.
85-//
86-// FIXME: put into utils package
87-func modifyNameValueFile(file string, changes []ConfigFileChange) (err error) {
88- var lines []string
89- var updated []ConfigFileChange
90-
91- if lines, err = readLines(file); err != nil {
92- return err
93- }
94-
95- var new []string
96-
97- for _, line := range lines {
98- for _, change := range changes {
99- if strings.HasPrefix(line, fmt.Sprintf("%s=", change.Name)) {
100- line = fmt.Sprintf("%s=%s", change.Name, change.Value)
101- updated = append(updated, change)
102- }
103- }
104- new = append(new, line)
105- }
106-
107- lines = new
108-
109- for _, change := range changes {
110- var got bool = false
111- for _, update := range updated {
112- if update.Name == change.Name {
113- got = true
114- break
115- }
116- }
117-
118- if got == false {
119- // name/value pair did not exist in original
120- // file, so append
121- lines = append(lines, fmt.Sprintf("%s=%s",
122- change.Name, change.Value))
123- }
124- }
125-
126- return atomicFileUpdate(file, lines)
127-}
128
129=== modified file 'partition/utils.go'
130--- partition/utils.go 2015-01-27 07:59:45 +0000
131+++ partition/utils.go 2015-02-03 09:40:51 +0000
132@@ -6,6 +6,7 @@
133 "os"
134 "os/exec"
135 "strings"
136+ "bufio"
137 )
138
139 // Return true if given path exists.
140@@ -75,5 +76,126 @@
141
142 output = strings.Split(string(bytes), "\n")
143
144+ // remove last element if it's empty
145+ if len(output) > 1 {
146+ last := output[len(output)-1]
147+ if last == "" {
148+ output = output[:len(output)-1]
149+ }
150+ }
151+
152 return output, err
153 }
154+
155+// Return a string slice of all lines in file specified by path
156+func readLines(path string) (lines []string, err error) {
157+
158+ file, err := os.Open(path)
159+
160+ if err != nil {
161+ return nil, err
162+ }
163+
164+ defer file.Close()
165+
166+ scanner := bufio.NewScanner(file)
167+ for scanner.Scan() {
168+ lines = append(lines, scanner.Text())
169+ }
170+
171+ return lines, scanner.Err()
172+}
173+
174+// Write lines slice to file specified by path
175+func writeLines(lines []string, path string) (err error) {
176+
177+ file, err := os.Create(path)
178+
179+ if err != nil {
180+ return err
181+ }
182+
183+ defer file.Close()
184+
185+ writer := bufio.NewWriter(file)
186+
187+ for _, line := range lines {
188+ if _, err = fmt.Fprintln(writer, line); err != nil {
189+ return err
190+ }
191+ }
192+ return writer.Flush()
193+}
194+
195+// Write lines to file atomically. File does not have to preexist.
196+func atomicFileUpdate(file string, lines []string) (err error) {
197+ tmpFile := fmt.Sprintf("%s.NEW", file)
198+
199+ if err := writeLines(lines, tmpFile); err != nil {
200+ return err
201+ }
202+
203+ // atomic update
204+ if err = os.Rename(tmpFile, file); err != nil {
205+ return err
206+ }
207+
208+ return err
209+}
210+
211+// Rewrite the specified file, applying the specified set of changes.
212+// Lines not in the changes slice are left alone.
213+// If the original file does not contain any of the name entries (from
214+// the corresponding ConfigFileChange objects), those entries are
215+// appended to the file.
216+//
217+func modifyNameValueFile(file string, changes []ConfigFileChange) (err error) {
218+ var lines []string
219+ var updated []ConfigFileChange
220+
221+ if lines, err = readLines(file); err != nil {
222+ return err
223+ }
224+
225+ var new []string
226+
227+ for _, line := range lines {
228+ for _, change := range changes {
229+ if strings.HasPrefix(line, fmt.Sprintf("%s=", change.Name)) {
230+ line = fmt.Sprintf("%s=%s", change.Name, change.Value)
231+ updated = append(updated, change)
232+ }
233+ }
234+ new = append(new, line)
235+ }
236+
237+ lines = new
238+
239+ for _, change := range changes {
240+ var got bool = false
241+ for _, update := range updated {
242+ if update.Name == change.Name {
243+ got = true
244+ break
245+ }
246+ }
247+
248+ if got == false {
249+ // name/value pair did not exist in original
250+ // file, so append
251+ lines = append(lines, fmt.Sprintf("%s=%s",
252+ change.Name, change.Value))
253+ }
254+ }
255+
256+ return atomicFileUpdate(file, lines)
257+}
258+
259+func getLsblkOutput() ([]string, error) {
260+ return runCommandWithStdout(
261+ "/bin/lsblk",
262+ "--ascii",
263+ "--output=NAME,LABEL,PKNAME,MOUNTPOINT",
264+ "--pairs")
265+}
266+

Subscribers

People subscribed via source and target branches