Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better LSB release #18

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 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,16 +79,27 @@ 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()

for scanner.Scan() {
line := scanner.Text()
handled := false
for _, key := range keys {
keyString := key.String()
data := parseLine(line, keyString)
if data != "" {
parseStruct[keyString](data)
handled = true
break
}
}
if validate && !handled {
log.Fatalf("Cannot parse line '%s'", line)
}
data := parseLine(scanner.Text(), k)
callback(data)
}
}

Expand All @@ -92,7 +110,7 @@ func parseLine(line string, regexpStr string) string {
}
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
4 changes: 4 additions & 0 deletions tools/lsb_release/test_data/lsb_release_1804.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: 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
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.1
Loading