Skip to content

Commit

Permalink
Merge branch 'master' into BAF-805/aarch64
Browse files Browse the repository at this point in the history
  • Loading branch information
Melky-Phoe committed Apr 5, 2024
2 parents bcf6173 + b193c9a commit 504cfbd
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
52 changes: 35 additions & 17 deletions tools/lsb_release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
)

// CmdLineArgs
Expand All @@ -19,10 +19,11 @@ import (
// - build package (package mode)
// Exactly one of these modes can be active in a time.
type CmdLineArgs struct {
FlagR *bool
FlagS *bool
FlagI *bool
parser *argparse.Parser
FlagR *bool
FlagS *bool
FlagI *bool
FlagValidate *bool
parser *argparse.Parser
}

func (cmd *CmdLineArgs) InitFlags() {
Expand All @@ -45,6 +46,12 @@ func (cmd *CmdLineArgs) InitFlags() {
Help: "i",
},
)
cmd.FlagValidate = cmd.parser.Flag("", "validate",
&argparse.Options{
Required: false,
Help: "Validate the input file",
},
)
}

func (cmd *CmdLineArgs) ParseArgs(args []string) error {
Expand All @@ -57,11 +64,11 @@ func (cmd *CmdLineArgs) ParseArgs(args []string) error {
}

type DataStruct struct {
ReleaseNumber int
ReleaseNumber string
DistributorID string
}

func (data *DataStruct) ReadFromFile(filePath string) {
func (data *DataStruct) ReadFromFile(filePath string, validate bool) {
var err error

file, err := os.Open(filePath)
Expand All @@ -72,27 +79,38 @@ func (data *DataStruct) ReadFromFile(filePath string) {

parseStruct := map[string]func(string){
"^Distributor ID:\t([^\t]+)$": func(s string) { data.DistributorID = s },
"^Release:\t([^\t]+)$": func(s string) { data.ReleaseNumber, _ = strconv.Atoi(s) },
"^Release:\t([^\t]+)$": func(s string) { data.ReleaseNumber = s },
}

scanner := bufio.NewScanner(file)
for k, callback := range parseStruct {
if !scanner.Scan() {
log.Fatal("cannot scan next line in the input file")
keys := reflect.ValueOf(parseStruct).MapKeys()

handled := 0
for scanner.Scan() {
line := scanner.Text()
for _, key := range keys {
keyString := key.String()
data := parseLine(line, keyString)
if data != "" {
parseStruct[keyString](data)
handled++
break
}
}
data := parseLine(scanner.Text(), k)
callback(data)
}
if validate && len(keys) != handled {
log.Panicf("Not all needed values were extracted!")
}
}

func parseLine(line string, regexpStr string) string {
regex, err := regexp.Compile(regexpStr)
if err != nil {
log.Fatalf("Cannot compile regex '%s'", regexpStr)
log.Panicf("Cannot compile regex '%s'", regexpStr)
}
subMatch := regex.FindStringSubmatch(line)
if subMatch == nil {
log.Fatalf("Cannot parse '%s' from '%s'", regexpStr, line)
return ""
}
return subMatch[1]
}
Expand All @@ -115,7 +133,7 @@ func main() {
filePath := path.Join(exPath, "lsb_release.txt")

var lsbReleaseData DataStruct
lsbReleaseData.ReadFromFile(filePath)
lsbReleaseData.ReadFromFile(filePath, *args.FlagValidate)

if *args.FlagS == true {
if *args.FlagI {
Expand All @@ -129,7 +147,7 @@ func main() {
fmt.Printf("Distributor ID:\t%s\n", lsbReleaseData.DistributorID)
}
if *args.FlagR {
fmt.Printf("Release:\t%d\n", lsbReleaseData.ReleaseNumber)
fmt.Printf("Release:\t%s\n", lsbReleaseData.ReleaseNumber)
}
}
}
42 changes: 42 additions & 0 deletions tools/lsb_release/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import "testing"

func lsb_release_test(distId string, relNumber string, validate bool, file string, t *testing.T) {
data := DataStruct{}

data.ReadFromFile(file, validate)
if data.DistributorID != distId {
t.Errorf("Distributor ID is not %s", distId)
}
if data.ReleaseNumber != relNumber {
t.Errorf("Release number is not %s", relNumber)
}
}

func Test_lsb_release_multipledigit(t *testing.T) {
lsb_release_test("Ubuntu", "18.04", false, "test_data/lsb_release_ubuntu1804.txt", t)
}

func Test_lsb_release_digit(t *testing.T) {
lsb_release_test("Debian", "11", false, "test_data/lsb_release.txt", t)
}

func Test_lsb_release_validate_ok(t *testing.T) {
lsb_release_test("Debian", "11", true, "test_data/lsb_release.txt", t)
lsb_release_test("Ubuntu", "18.04", true, "test_data/lsb_release_ubuntu1804.txt", t)
}

func Test_lsb_release_validate_notok(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
lsb_release_test("Debian", "11", true, "test_data/lsb_release_broken.txt", t)
}

func Test_lsb_release_validate_notok_nopanic(t *testing.T) {
data := DataStruct{}
data.ReadFromFile("test_data/lsb_release_broken.txt", false)
}
2 changes: 2 additions & 0 deletions tools/lsb_release/test_data/lsb_release.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Distributor ID: Debian
Release: 11
2 changes: 2 additions & 0 deletions tools/lsb_release/test_data/lsb_release_broken.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Distributor ID : Debian
Release: 11
4 changes: 4 additions & 0 deletions tools/lsb_release/test_data/lsb_release_ubuntu1804.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Distributor ID: Ubuntu
Description: Ubuntu GNU/Linux 18.04 (Bionic Beaver)
Release: 18.04
Codename: Bionic Beaver
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.3.0
version=0.3.2

0 comments on commit 504cfbd

Please sign in to comment.