forked from docker/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
names.go
43 lines (37 loc) · 1.04 KB
/
names.go
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 internal
import (
"fmt"
"path/filepath"
"regexp"
"strings"
)
const (
// AppExtension is the extension used by an application
AppExtension = ".dockerapp"
)
var appNameRe, _ = regexp.Compile("^[a-zA-Z][a-zA-Z0-9_-]+$")
// AppNameFromDir takes a path to an app directory and returns
// the application's name
func AppNameFromDir(dirName string) string {
return strings.TrimSuffix(filepath.Base(dirName), AppExtension)
}
// DirNameFromAppName takes an application name and returns the
// corresponding directory name
func DirNameFromAppName(appName string) string {
if strings.HasSuffix(appName, AppExtension) {
return appName
}
return appName + AppExtension
}
// ValidateAppName takes an app name and returns an error if it doesn't
// match the expected format
func ValidateAppName(appName string) error {
if appNameRe.MatchString(appName) {
return nil
}
return fmt.Errorf(
"invalid app name: %s ; app names must start with a letter, and must contain only letters, numbers, '-' and '_' (regexp: %q)",
appName,
appNameRe.String(),
)
}