diff options
Diffstat (limited to 'sd_login/util.go')
-rw-r--r-- | sd_login/util.go | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/sd_login/util.go b/sd_login/util.go new file mode 100644 index 0000000..45df0e0 --- /dev/null +++ b/sd_login/util.go @@ -0,0 +1,130 @@ +// Copyright (C) 2016 Luke Shumaker <lukeshu@sbcglobal.net> +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package sd_login + +import ( + "io/ioutil" + "os" + "path" + "strings" +) + +const ( + whitespace = " \t\n\r" + newline = "\n\r" + digits = "0123456789" + lowercase_letters = "abcdefghijklmnopqrstuvwxyz" + uppercase_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + letters = lowercase_letters + uppercase_letters +) + +func trimOneSuffix(s string, suffixes ...string) string { + for _, suffix := range suffixes { + if strings.HasSuffix(s, suffix) { + return strings.TrimSuffix(s, suffix) + } + } + return s +} + +func trimPrefixSuffix(s, prefix, suffix string) (string, bool) { + if strings.HasPrefix(s, prefix) && strings.HasSuffix(s, suffix) { + s = strings.TrimPrefix(s, prefix) + s = strings.TrimSuffix(s, suffix) + return s, true + } + return "", false +} + +func split2(s string, b byte) (string, string) { + n := strings.IndexByte(s, b) + if n < 0 { + n = len(s) + } + return s[:n], s[n:] +} + +func path_startswith(apath string, aprefix string) (string, bool) { + if path.IsAbs(apath) != path.IsAbs(aprefix) { + return "", false + } + for { + apath = strings.TrimLeft(apath, "/") + aprefix = strings.TrimLeft(aprefix, "/") + + if aprefix == "" { + return apath, true + } + if apath == "" { + return "", false + } + + pathPart, pathRest := split2(apath, '/') + prefixPart, prefixRest := split2(aprefix, '/') + + if pathPart != prefixPart { + return "", false + } + + apath = pathRest + aprefix = prefixRest + } +} + +func get_files_in_directory(apath string) ([]string, error) { + files, err := ioutil.ReadDir(apath) + if err != nil { + return nil, err + } + var ret []string + for _, file := range files { + if file.Mode()&os.ModeType&^os.ModeSymlink == 0 { + ret = append(ret, file.Name()) + } + } + return ret, nil +} + +func parse_env_file(filename string) (map[string]string, error) + +/* + enum { + PRE_KEY, + KEY, + PRE_VALUE, + VALUE, + VALUE_ESCAPE, + SINGLE_QUOTE_VALUE, + SINGLE_QUOTE_VALUE_ESCAPE, + DOUBLE_QUOTE_VALUE, + DOUBLE_QUOTE_VALUE_ESCAPE, + COMMENT, + COMMENT_ESCAPE + } state = PRE_KEY; + +func parse_env_file(filename string) (map[string]string, error) { + contents, err := ioutil.ReadFile(filename) + + for contents != "" { + switch state { + case PRE_KEY: + if strings.ContainsRune("#;", contents[0]) { + sate = COMMENT + } else if strings.ContainsRune(whitespace, contents[0]) { + state = KEY + last_key_whitespace + } +} +*/ |