File: util.go

package info (click to toggle)
addchain 0.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: sh: 428; makefile: 10
file content (30 lines) | stat: -rw-r--r-- 781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main

import (
	"errors"
	"path/filepath"
	"regexp"
	"runtime"
)

// ValidateVersion checks if version is of the form "MAJOR.MINOR.PATCH". That
// is, a simple semver format without "v" prefix, pre-release or build suffixes.
func ValidateVersion(version string) error {
	if !versionrx.MatchString(version) {
		return errors.New("version must be of the form MAJOR.MINOR.PATCH")
	}
	return nil
}

var versionrx = regexp.MustCompile(`^\d+\.\d+\.\d+$`)

// RepoPath returns a full file path given a path relative to the repository
// root. Returns empty string if it cannot be determined.
func RepoPath(rel string) string {
	_, self, _, ok := runtime.Caller(0)
	if !ok {
		return ""
	}
	path := filepath.Join(filepath.Dir(self), "../../..", rel)
	return filepath.Clean(path)
}