Skip to content

Commit

Permalink
simplify reading file
Browse files Browse the repository at this point in the history
  • Loading branch information
chilagrow committed Feb 19, 2025
1 parent 64c74f5 commit f062372
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
35 changes: 11 additions & 24 deletions ferretdb_packaging/defineversion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -55,7 +54,7 @@ func main() {

// controlDefaultVer matches major, minor and patch from default_version field in control file,
// see pg_documentdb_core/documentdb_core.control.
var controlDefaultVer = regexp.MustCompile(`^default_version\s=\s'(?P<major>[0-9]+)\.(?P<minor>[0-9]+)-(?P<patch>[0-9]+)'$`)
var controlDefaultVer = regexp.MustCompile(`(?m)^default_version\s=\s'(?P<major>[0-9]+)\.(?P<minor>[0-9]+)-(?P<patch>[0-9]+)'$`)

// tagVer is the syntax used by tags such as `v0.100.0`.
// It may contain additional string such as `v0.100.0-ferretdb` and `v0.100.0-ferretdb-2.0.1`.
Expand Down Expand Up @@ -91,33 +90,21 @@ func debugEnv(action *githubactions.Action) {
// and returns the control default version in SemVer format,
// see pg_documentdb_core/documentdb_core.control.
func readControlDefaultVersion(f string) (string, error) {
file, err := os.Open(f)
b, err := os.ReadFile(f)
if err != nil {
return "", fmt.Errorf("failed to read control file: %w", err)
return "", err
}

defer file.Close()

r := bufio.NewReader(file)

for {
var line []byte
line, _, err = r.ReadLine()
if err != nil {
return "", fmt.Errorf("control file did not find default_version: %w", err)
}

match := controlDefaultVer.FindSubmatch(line)
if match == nil || len(match) != controlDefaultVer.NumSubexp()+1 {
continue
}
match := controlDefaultVer.FindSubmatch(b)
if match == nil || len(match) != controlDefaultVer.NumSubexp()+1 {
return "", fmt.Errorf("control file did not find default_version: %s", f)
}

major := match[tagVer.SubexpIndex("major")]
minor := match[tagVer.SubexpIndex("minor")]
patch := match[tagVer.SubexpIndex("patch")]
major := match[tagVer.SubexpIndex("major")]
minor := match[tagVer.SubexpIndex("minor")]
patch := match[tagVer.SubexpIndex("patch")]

return fmt.Sprintf("%s.%s.%s", major, minor, patch), nil
}
return fmt.Sprintf("%s.%s.%s", major, minor, patch), nil
}

// define returns the upstream version using the environment variables of GitHub Actions.
Expand Down
8 changes: 4 additions & 4 deletions ferretdb_packaging/defineversion/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDefine(t *testing.T) {
env: map[string]string{
"GITHUB_EVENT_NAME": "pull_request_target",
"GITHUB_HEAD_REF": "define-docker-tag",
"GITHUB_REF_NAME": "main",
"GITHUB_REF_NAME": "ferretdb",
"GITHUB_REF_TYPE": "branch",
},
controlDefaultVersion: "0.100.0",
Expand Down Expand Up @@ -196,11 +196,11 @@ func TestResults(t *testing.T) {
})
action := githubactions.New(githubactions.WithGetenv(getenv), githubactions.WithWriter(&stdout))

version := "0.100.0~main"
version := "0.100.0~ferretdb"

setResults(action, version)

expected := "version: 0.100.0~main\n"
expected := "version: 0.100.0~ferretdb\n"
assert.Equal(t, expected, stdout.String(), "stdout does not match")

b, err := io.ReadAll(summaryF)
Expand All @@ -209,7 +209,7 @@ func TestResults(t *testing.T) {

expectedOutput := `
version<<_GitHubActionsFileCommandDelimeter_
0.100.0~main
0.100.0~ferretdb
_GitHubActionsFileCommandDelimeter_
`[1:]
b, err = io.ReadAll(outputF)
Expand Down

0 comments on commit f062372

Please sign in to comment.