Skip to content

Commit

Permalink
Add support for brew-specific build ids
Browse files Browse the repository at this point in the history
Among other stuff, homebrew docs say:

> We don’t like tools that upgrade themselves

So we'll use a distinctive build id when building for brew (similar to
the one we use for debian distos), and mos will refuse to update itself
then.

PUBLISHED_FROM=13447e23f105ab3c232cdea390469679e124bb83
  • Loading branch information
dimonomid authored and cesantabot committed Dec 19, 2017
1 parent e1f3cb4 commit c8d51e0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
17 changes: 17 additions & 0 deletions common/go/ourutil/ourutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"

"github.com/cesanta/errors"
Expand Down Expand Up @@ -75,3 +76,19 @@ func RunCmd(outMode CmdOutMode, args ...string) error {

return nil
}

// Returns a map from regexp capture group name to the corresponding matched
// string.
// A return value of nil indicates no match.
func FindNamedSubmatches(r *regexp.Regexp, s string) map[string]string {
matches := r.FindStringSubmatch(s)
if matches == nil {
return nil
}

result := make(map[string]string)
for i, name := range r.SubexpNames()[1:] {
result[name] = matches[i+1]
}
return result
}
3 changes: 3 additions & 0 deletions mos/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func Update(ctx context.Context, devConn *dev.DevConn) error {
return errors.Trace(err)
}

return nil
} else if version.LooksLikeBrewBuildId(version.BuildId) {
ourutil.Reportf("Please use brew to update mos.")
return nil
}

Expand Down
34 changes: 27 additions & 7 deletions mos/version/version_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package version

import (
"regexp"
"strings"

"cesanta.com/common/go/ourutil"
moscommon "cesanta.com/mos/common"
)

Expand All @@ -14,14 +16,20 @@ type VersionJson struct {
BuildVersion string `json:"build_version"`
}

const (
brewDistrName = "brew"
)

var (
regexpVersionNumber = regexp.MustCompile(`^\d+\.[0-9.]*$`)
regexpBuildId = regexp.MustCompile(
`^(?P<datetime>[^/]+)\/(?P<symbolic>[^@]+)\@(?P<hash>.+)$`,
)
regexpBuildIdDebian = regexp.MustCompile(
regexpBuildIdDistr = regexp.MustCompile(
`^(?P<version>[^+]+)\+(?P<hash>[^~]+)\~(?P<distr>.+)$`,
)

debianDistrNames = []string{"xenial", "zesty", "artful"}
)

// GetMosVersion returns this binary's version, or "latest" if it's not a release build.
Expand Down Expand Up @@ -55,19 +63,31 @@ func LooksLikeDebianBuildId(s string) bool {
return GetDebianPackageName(s) != ""
}

func LooksLikeBrewBuildId(s string) bool {
matches := ourutil.FindNamedSubmatches(regexpBuildIdDistr, s)
return matches != nil && matches["distr"] == brewDistrName
}

// GetDebianPackageName parses given build id string, and if it looks like a
// debian build id, returns either "mos-latest" or "mos". Otherwise, returns
// an empty string.
func GetDebianPackageName(buildId string) string {
matches := regexpBuildIdDebian.FindStringSubmatch(buildId)
matches := ourutil.FindNamedSubmatches(regexpBuildIdDistr, buildId)
if matches != nil {
if LooksLikeVersionNumber(matches[1]) {
return "mos"
} else {
return "mos-latest"
for _, v := range debianDistrNames {
if strings.HasPrefix(matches["distr"], v) {
if LooksLikeVersionNumber(matches["version"]) {
return "mos"
} else {
return "mos-latest"
}
}
}

// Some non-debian distro name
return ""
} else {
// Doesn't look like debian build id
// Doesn't look like distro build id
return ""
}
}

0 comments on commit c8d51e0

Please sign in to comment.