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
15package path
16
17// OS must be a valid [runtime.GOOS] value or "unix".
18type OS string
19
20const (
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
29type os struct {
30 osInfo
31 Separator byte
32 ListSeparator byte
33}
34
35func (o os) isWindows() bool {
36 return o.Separator == '\\'
37}
38
39type osInfo interface {
40 IsPathSeparator(b byte) bool
41 splitList(path string) []string
42 volumeNameLen(path string) int
43 IsAbs(path string) (b bool)
44 join(elem []string) string
45 sameWord(a, b string) bool
46}
47
48func getOS(o OS) os {
49 switch o {
50 case Windows:
51 return windows
52 case Plan9:
53 return plan9
54 default:
55 return unix
56 }
57}
58
59var (
60 plan9 = os{
61 osInfo: &plan9Info{},
62 Separator: plan9Separator,
63 ListSeparator: plan9ListSeparator,
64 }
65 unix = os{
66 osInfo: &unixInfo{},
67 Separator: unixSeparator,
68 ListSeparator: unixListSeparator,
69 }
70 windows = os{
71 osInfo: &windowsInfo{},
72 Separator: windowsSeparator,
73 ListSeparator: windowsListSeparator,
74 }
75)