// Copyright (C) 2016-2017 Luke Shumaker // // 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" "golang.org/x/sys/unix" ) 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_boolean(v string) (bool, error) { switch { case v == "1", strings.EqualFold(v, "yes"), strings.EqualFold(v, "y"), strings.EqualFold(v, "true"), strings.EqualFold(v, "t"), strings.EqualFold(v, "on"): return true, nil case v == "0", strings.EqualFold(v, "no"), strings.EqualFold(v, "n"), strings.EqualFold(v, "false"), strings.EqualFold(v, "f"), strings.EqualFold(v, "off"): return false, nil default: return false, unix.EINVAL } } 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 } } */