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)
|
log.Default().SetFlags(log.Lmicroseconds)
|
||||||
|
|
||||||
deviceinfoFile := "/etc/deviceinfo"
|
var devinfo deviceinfo.DeviceInfo
|
||||||
if exists, err := misc.Exists(deviceinfoFile); !exists {
|
if err := devinfo.ReadDeviceinfo("/etc/deviceinfo"); err != nil {
|
||||||
log.Printf("NOTE: %q not found, this file is required by mkinitfs.\n", deviceinfoFile)
|
log.Println("Error reading deviceinfo:", err)
|
||||||
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)
|
|
||||||
retCode = 1
|
retCode = 1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@@ -11,6 +11,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeviceInfo struct {
|
type DeviceInfo struct {
|
||||||
@@ -21,24 +23,31 @@ type DeviceInfo struct {
|
|||||||
UbootBoardname string
|
UbootBoardname string
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadDeviceinfo(file string) (DeviceInfo, error) {
|
// Reads the relevant entries from "file" into DeviceInfo struct
|
||||||
var deviceinfo DeviceInfo
|
// 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)
|
fd, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return deviceinfo, err
|
return err
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
|
|
||||||
if err := unmarshal(fd, &deviceinfo); err != nil {
|
if err := d.unmarshal(fd); err != nil {
|
||||||
return deviceinfo, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return deviceinfo, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshals a deviceinfo into a DeviceInfo struct
|
// 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)
|
s := bufio.NewScanner(r)
|
||||||
for s.Scan() {
|
for s.Scan() {
|
||||||
line := s.Text()
|
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)
|
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() {
|
if !field.IsValid() {
|
||||||
// an option that meets the deviceinfo "specification", but isn't
|
// an option that meets the deviceinfo "specification", but isn't
|
||||||
// one we care about in this module
|
// one we care about in this module
|
||||||
|
@@ -58,7 +58,7 @@ func TestUnmarshal(t *testing.T) {
|
|||||||
var d DeviceInfo
|
var d DeviceInfo
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
testName := fmt.Sprintf("unmarshal::'%s':", strings.ReplaceAll(table.in, "\n", "\\n"))
|
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)
|
t.Errorf("%s received an unexpected err: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user