Move logic to check if deviceinfo file exists to pkgs/deviceinfo (MR 37)
This is mostly a preparatory commit to later be able to read the deviceinfo from multiple places. It has a bit better encapsulation, and makes the functions methods, so that they can update deviceinfo file in-place.
This commit is contained in:
committed by
Clayton Craft
parent
b2cdfe9da4
commit
c87b926a53
@@ -49,19 +49,9 @@ func main() {
|
||||
|
||||
log.Default().SetFlags(log.Lmicroseconds)
|
||||
|
||||
deviceinfoFile := "/etc/deviceinfo"
|
||||
if exists, err := misc.Exists(deviceinfoFile); !exists {
|
||||
log.Printf("NOTE: %q not found, this file is required by mkinitfs.\n", deviceinfoFile)
|
||||
return
|
||||
} else if err != nil {
|
||||
retCode = 1
|
||||
log.Printf("received unexpected error when getting status for %q: %s", deviceinfoFile, err)
|
||||
return
|
||||
}
|
||||
|
||||
devinfo, err := deviceinfo.ReadDeviceinfo(deviceinfoFile)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
var devinfo deviceinfo.DeviceInfo
|
||||
if err := devinfo.ReadDeviceinfo("/etc/deviceinfo"); err != nil {
|
||||
log.Println("Error reading deviceinfo:", err)
|
||||
retCode = 1
|
||||
return
|
||||
}
|
||||
|
@@ -11,6 +11,8 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
|
||||
)
|
||||
|
||||
type DeviceInfo struct {
|
||||
@@ -21,24 +23,31 @@ type DeviceInfo struct {
|
||||
UbootBoardname string
|
||||
}
|
||||
|
||||
func ReadDeviceinfo(file string) (DeviceInfo, error) {
|
||||
var deviceinfo DeviceInfo
|
||||
// Reads the relevant entries from "file" into DeviceInfo struct
|
||||
// Any already-set entries will be overwriten if they are present
|
||||
// in "file"
|
||||
func (d *DeviceInfo) ReadDeviceinfo(file string) error {
|
||||
if exists, err := misc.Exists(file); !exists {
|
||||
return fmt.Errorf("%q not found, required by mkinitfs", file)
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("unexpected error getting status for %q: %s", file, err)
|
||||
}
|
||||
|
||||
fd, err := os.Open(file)
|
||||
if err != nil {
|
||||
return deviceinfo, err
|
||||
return err
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
if err := unmarshal(fd, &deviceinfo); err != nil {
|
||||
return deviceinfo, err
|
||||
if err := d.unmarshal(fd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return deviceinfo, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unmarshals a deviceinfo into a DeviceInfo struct
|
||||
func unmarshal(r io.Reader, devinfo *DeviceInfo) error {
|
||||
func (d *DeviceInfo) unmarshal(r io.Reader) error {
|
||||
s := bufio.NewScanner(r)
|
||||
for s.Scan() {
|
||||
line := s.Text()
|
||||
@@ -74,7 +83,7 @@ func unmarshal(r io.Reader, devinfo *DeviceInfo) error {
|
||||
return fmt.Errorf("error parsing deviceinfo line, invalid format: %s", line)
|
||||
}
|
||||
|
||||
field := reflect.ValueOf(devinfo).Elem().FieldByName(fieldName)
|
||||
field := reflect.ValueOf(d).Elem().FieldByName(fieldName)
|
||||
if !field.IsValid() {
|
||||
// an option that meets the deviceinfo "specification", but isn't
|
||||
// one we care about in this module
|
||||
|
@@ -58,7 +58,7 @@ func TestUnmarshal(t *testing.T) {
|
||||
var d DeviceInfo
|
||||
for _, table := range tables {
|
||||
testName := fmt.Sprintf("unmarshal::'%s':", strings.ReplaceAll(table.in, "\n", "\\n"))
|
||||
if err := unmarshal(strings.NewReader(table.in), &d); err != nil {
|
||||
if err := d.unmarshal(strings.NewReader(table.in)); err != nil {
|
||||
t.Errorf("%s received an unexpected err: ", err)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user