-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfs_create.go
More file actions
45 lines (38 loc) · 921 Bytes
/
fs_create.go
File metadata and controls
45 lines (38 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package command
import (
"context"
"io"
"lesiw.io/fs"
"lesiw.io/fs/path"
)
var _ fs.CreateFS = (*cmdFS)(nil)
func (cfs *cmdFS) Create(
ctx context.Context, name string,
) (wc io.WriteCloser, err error) {
if err = cfs.init(ctx); err != nil {
return nil, err
}
if err = fs.MkdirAll(ctx, cfs, path.Dir(name)); err != nil {
return nil, err
}
switch cfs.kind {
case kindGNU, kindBSD:
wc = NewWriter(ctx, cfs, "tee", name)
case kindWindows:
wc = psWriter(ctx, cfs, psScript(
`$f = [System.IO.File]::Create('%s')`,
`[Console]::OpenStandardInput().CopyTo($f)`,
`$f.Close()`,
), name)
case kindDOS:
wc = dosWriter(ctx, cfs, "cmd", "/c", "more", ">", name)
default:
return nil, errUnsupportedOS
}
// Perform a no-op write to force file creation or truncation
// even if the stream is immediately Closed.
if _, err = wc.Write(nil); err != nil {
return nil, err
}
return wc, nil
}