cuelang.org/go@v0.10.1/pkg/path/os.go (about) 1 // Copyright 2020 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package path 16 17 // OS must be a valid runtime.GOOS value or "unix". 18 type OS string 19 20 const ( 21 Unix OS = "unix" 22 Windows OS = "windows" 23 Plan9 OS = "plan9" 24 ) 25 26 // These types have been designed to minimize the diffs with the original Go 27 // code, thereby minimizing potential toil in keeping them up to date. 28 29 type os struct { 30 osInfo 31 Separator byte 32 ListSeparator byte 33 } 34 35 func (o os) isWindows() bool { 36 return o.Separator == '\\' 37 } 38 39 type osInfo interface { 40 IsPathSeparator(b byte) bool 41 splitList(path string) []string 42 volumeNameLen(path string) int 43 IsAbs(path string) (b bool) 44 HasPrefix(p, prefix string) bool 45 join(elem []string) string 46 sameWord(a, b string) bool 47 } 48 49 func getOS(o OS) os { 50 switch o { 51 case Windows: 52 return windows 53 case Plan9: 54 return plan9 55 default: 56 return unix 57 } 58 } 59 60 var ( 61 plan9 = os{ 62 osInfo: &plan9Info{}, 63 Separator: plan9Separator, 64 ListSeparator: plan9ListSeparator, 65 } 66 unix = os{ 67 osInfo: &unixInfo{}, 68 Separator: unixSeparator, 69 ListSeparator: unixListSeparator, 70 } 71 windows = os{ 72 osInfo: &windowsInfo{}, 73 Separator: windowsSeparator, 74 ListSeparator: windowsListSeparator, 75 } 76 )