This Commented CUE
demonstrates how to use the built-in functions
path.Base
,
path.Dir
, and
path.Ext
to access components of a file’s name and its path.
file.cue
package example
import "path"
[X=string]: {
_OS: *path.Unix | path.Windows | path.Plan9
Dir: path.Dir(X, _OS)
Base: path.Base(X, _OS)
Ext: path.Ext(X, _OS)
}
// Examples of absolute paths.
"/foo/bar/baz.js": _
#"C:\foo\bar\baz.js"#: _OS: path.Windows
// Examples of directory traversal.
"foo/bar/../quux/a.js": _
#"C:\foo\bar\..\quux\a.js"#: _OS: path.Windows
// Examples of empty path components.
"/foo///bar////baz.js": _
#"C:\foo\\bar\\\baz.js"#: _OS: path.Windows
TERMINAL
$ cue eval
"/foo/bar/baz.js": {
Dir: "/foo/bar"
Base: "baz.js"
Ext: ".js"
}
"C:\\foo\\bar\\baz.js": {
Dir: "C:\\foo\\bar"
Base: "baz.js"
Ext: ".js"
}
"foo/bar/../quux/a.js": {
Dir: "foo/quux"
Base: "a.js"
Ext: ".js"
}
"C:\\foo\\bar\\..\\quux\\a.js": {
Dir: "C:\\foo\\quux"
Base: "a.js"
Ext: ".js"
}
"/foo///bar////baz.js": {
Dir: "/foo/bar"
Base: "baz.js"
Ext: ".js"
}
"C:\\foo\\\\bar\\\\\\baz.js": {
Dir: "C:\\foo\\bar"
Base: "baz.js"
Ext: ".js"
}
Related content
- The
path
built-in package documentation details the rules that each of the functionspath.Base
,path.Dir
, andpath.Ext
follow as they process their input - Using CUE’s “raw” strings is
convenient when writing literal Windows paths. They avoid having to escape
every backslash (
\\
), as is demonstrated in the Windows-related examples above.