-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
140 lines (125 loc) · 2.86 KB
/
file.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
// taken from knot8.io/cmd/knot8
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"github.com/mattn/go-isatty"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
)
// A shadowFile is in-memory copy of a file that can be committed back to disk.
type shadowFile struct {
name string
buf []byte
}
func newShadowFile(filename string) (*shadowFile, error) {
var r io.Reader
if filename == "" {
filename = "-"
}
if filename == "-" {
if isatty.IsTerminal(os.Stdin.Fd()) {
fmt.Fprintf(os.Stderr, "(reading manifests from standard input; hit ctrl-c if this is not what you wanted)\n")
}
r = os.Stdin
} else {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
r = f
}
buf, err := readAllTranscode(r)
if err != nil {
return nil, err
}
return &shadowFile{name: filename, buf: buf}, nil
}
func (f *shadowFile) Commit() error {
b := []byte(string(f.buf))
var w io.Writer
if f.name == "-" {
w = os.Stdout
} else {
file, err := os.OpenFile(f.name, os.O_WRONLY|os.O_TRUNC, 0)
if err != nil {
return err
}
defer file.Close()
w = file
}
_, err := w.Write(b)
return err
}
// expandPaths will expand all path entries and return a slice of file paths.
// If an input path points to a directory it will return all *.yaml files contained in it.
// Shell globs are resolved.
func expandPaths(paths []string) ([]string, error) {
var (
res []string
errs []error
)
glob := func(p string) ([]string, bool, error) {
g, err := filepath.Glob(p)
if err != nil {
return nil, false, err
}
res, err := onlyFiles(g)
return res, len(g) > 0, err
}
add := func(p string) bool {
g, found, err := glob(p)
if err != nil {
errs = append(errs, err)
} else {
res = append(res, g...)
}
return found
}
for _, p := range paths {
// special case for stdin pseudo path
if p == "-" {
res = append(res, p)
continue
}
if found := add(p); !found {
errs = append(errs, fmt.Errorf("%q matched no files", p))
}
_ = add(p + "/*.yaml")
_ = add(p + "/*.yml")
}
if errs != nil {
return nil, errors.Join(errs...)
}
sort.Strings(res)
return res, nil
}
// onlyFiles filter the paths and excludes directories.
// This function assumes all paths exist.
func onlyFiles(paths []string) ([]string, error) {
var res []string
for _, p := range paths {
st, err := os.Stat(p)
if err != nil {
return nil, nil
}
if !st.IsDir() {
res = append(res, p)
}
}
return res, nil
}
// readAllTranscode reads a text input encoded as either UTF-8 or UTF-16, both LE and BE
// (which are the supported encodings of YAML),
func readAllTranscode(r io.Reader) ([]byte, error) {
t := unicode.BOMOverride(runes.ReplaceIllFormed())
return io.ReadAll(transform.NewReader(r, t))
}