summaryrefslogtreecommitdiff
path: root/store/misc.go
blob: dd6ee40d32b0e4b3765cbc8584f1255ceac7b25a (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
package store

import (
	"errors"
	"net/url"
	"regexp"
)

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

var (
	nameRegex   = regexp.MustCompile(`^[a-z0-9()_-]{3,}$`)
	sha256Regex = regexp.MustCompile(`\b[A-Fa-f0-9]{64}\b`)
	secretRegex = regexp.MustCompile(`\b[A-Za-z]{64}\b`)
)

var (
	// AlreadyExists is returned when store already exists.
	AlreadyExists = errors.New("store path already exists")
)

// MatchName determines if str is a valid name.
func MatchName(str string) bool {
	return nameRegex.MatchString(str)
}

// MatchURL determines if str is a valid URL.
func MatchURL(str string) bool {
	u, err := url.Parse(str)
	return err == nil && u.Scheme != "" && u.Host != ""
}

// These two really shouldn't be methods... Maybe change that for v2.

// MatchName determines if str is a valid name. As of v1, this just calls MatchName.
func (s *Store) MatchName(str string) bool {
	return MatchName(str)
}

// MatchURL determines if str is a valid URL. As of v1, this just calls MatchURL.
func (s *Store) MatchURL(str string) bool {
	return MatchURL(str)
}