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

Use shlex to parse version.txt #464

Draft
wants to merge 1 commit into
base: flatcar-master
Choose a base branch
from
Draft
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
19 changes: 7 additions & 12 deletions platform/api/azure/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"net/http"
"strings"

"github.com/flatcar/mantle/sdk"

"github.com/Azure/azure-sdk-for-go/services/classic/management"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-03-01/compute"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources"
Expand Down Expand Up @@ -203,17 +205,10 @@ func (a *API) resolveImage() error {
return fmt.Errorf("unable to fetch release bucket %v version: %v", a.Opts.Sku, err)
}

scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := strings.SplitN(scanner.Text(), "=", 2)
if len(line) != 2 {
continue
}
if line[0] == "FLATCAR_VERSION" {
a.Opts.Version = line[1]
return nil
}
versions, err := sdk.ParseFlatcarVersions(resp.Body)
if err != nil {
return fmt.Errorf("couldn't parse version.txt: %v", err)
}

return fmt.Errorf("couldn't find FLATCAR_VERSION in version.txt")
a.Opts.Version = versions.Version
return nil
}
46 changes: 23 additions & 23 deletions sdk/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ package sdk
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/google/go-shlex"
)

const (
Expand All @@ -37,39 +40,32 @@ type Versions struct {
SDKVersion string
}

func unquote(s string) string {
if len(s) < 2 {
return s
}
for _, q := range []byte{'\'', '"'} {
if s[0] == q && s[len(s)-1] == q {
return s[1 : len(s)-1]
func parseVersions(r io.Reader, prefix string) (ver Versions, err error) {
l := shlex.NewLexer(r)
for {
token, err2 := l.Next()
if err2 != nil {
if err2 == io.EOF {
break
}
err = err2
return
}
}
return s
}

func parseVersions(f *os.File, prefix string) (ver Versions, err error) {
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.SplitN(scanner.Text(), "=", 2)
line := strings.SplitN(token, "=", 2)
if len(line) != 2 {
continue
}
switch line[0] {
case prefix + "VERSION":
ver.Version = unquote(line[1])
ver.Version = line[1]
case prefix + "VERSION_ID":
ver.VersionID = unquote(line[1])
ver.VersionID = line[1]
case prefix + "BUILD_ID":
ver.BuildID = unquote(line[1])
ver.BuildID = line[1]
case prefix + "SDK_VERSION":
ver.SDKVersion = unquote(line[1])
ver.SDKVersion = line[1]
}
}
if err = scanner.Err(); err != nil {
return
}

if ver.VersionID == "" {
err = fmt.Errorf("Missing %sVERSION_ID in %s", prefix, f.Name())
Expand All @@ -80,6 +76,10 @@ func parseVersions(f *os.File, prefix string) (ver Versions, err error) {
return
}

func ParseFlatcarVersions(r io.Reader) (Versions, error) {
return parseVersions(r, "FLATCAR_")
}

func OSRelease(root string) (ver Versions, err error) {
f, err := os.Open(filepath.Join(root, "usr/lib/os-release"))
if err != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ func VersionsFromDir(dir string) (ver Versions, err error) {
}
defer f.Close()

ver, err = parseVersions(f, "FLATCAR_")
ver, err = ParseFlacarVersions(f)
if ver.SDKVersion == "" {
err = fmt.Errorf("Missing FLATCAR_SDK_VERSION in %s", f.Name())
}
Expand Down