abuild-lint

A linting utility for Alpine Linux APKBUILDs

git clone https://git.8pit.net/abuild-lint.git

 1package main
 2
 3import (
 4	"mvdan.cc/sh/syntax"
 5	"os"
 6	"regexp"
 7)
 8
 9var (
10	// IsName checks if the given string could be a part of a name
11	// in the shell command language as defined in section 3.235 of
12	// the POSIX base specification.
13	IsNamePart = regexp.MustCompile("^[_A-Za-z0-9]+$").MatchString
14)
15
16// IsSpace reports whether the rune is an ascii space character. This
17// differs from unicode.IsSpace which reports whether the rune is a
18// space character as defined by Unicode's White Space property.
19func IsSpace(r rune) bool {
20	return r == ' '
21}
22
23// IsParamExp reports whether the given parameter expression can be
24// replaced by a short parameter expression.
25func IsParamExp(paramExp *syntax.ParamExp) bool {
26	return paramExp.Excl || paramExp.Length || paramExp.Width ||
27		paramExp.Index != nil || paramExp.Slice != nil ||
28		paramExp.Repl != nil || paramExp.Exp != nil
29}
30
31// IsPrefixVar reports whether the given string is prefixed with a
32// single ascii underscore character.
33func IsPrefixVar(varname string) bool {
34	if len(varname) < 2 {
35		return false
36	}
37
38	return varname[0] == '_' && varname[1] != '_'
39}
40
41// IsIncluded reports whether the given string is included in the given
42// string slice.
43func IsIncluded(slice []string, str string) bool {
44	for _, s := range slice {
45		if s == str {
46			return true
47		}
48	}
49
50	return false
51}
52
53// IsMetaVar reports whether the given variable is a meta variable.
54func IsMetaVar(varname string) bool {
55	_, ok := metadataVariables[varname]
56	return ok
57}
58
59// IsDir reports whether the given file name is a directory.
60func IsDir(fn string) bool {
61	fi, err := os.Stat(fn)
62	return err == nil && fi.IsDir()
63}
64
65// Exists checks if a file with the given name exists.
66func Exists(fn string) bool {
67	_, err := os.Stat(fn)
68	return !os.IsNotExist(err)
69}