File: filesystem.go

package info (click to toggle)
golang-k8s-sigs-kustomize-kyaml 0.20.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,180 kB
  • sloc: makefile: 220; sh: 68
file content (154 lines) | stat: -rw-r--r-- 4,339 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package filesys

import (
	"fmt"
	"path/filepath"

	"sigs.k8s.io/kustomize/kyaml/errors"
)

const (
	Separator = string(filepath.Separator)
	SelfDir   = "."
	ParentDir = ".."
)

// FileSystem groups basic os filesystem methods.
// It's supposed be functional subset of https://golang.org/pkg/os
type FileSystem interface {

	// Create a file.
	Create(path string) (File, error)

	// MkDir makes a directory.
	Mkdir(path string) error

	// MkDirAll makes a directory path, creating intervening directories.
	MkdirAll(path string) error

	// RemoveAll removes path and any children it contains.
	RemoveAll(path string) error

	// Open opens the named file for reading.
	Open(path string) (File, error)

	// IsDir returns true if the path is a directory.
	IsDir(path string) bool

	// ReadDir returns a list of files and directories within a directory.
	ReadDir(path string) ([]string, error)

	// CleanedAbs converts the given path into a
	// directory and a file name, where the directory
	// is represented as a ConfirmedDir and all that implies.
	// If the entire path is a directory, the file component
	// is an empty string.
	CleanedAbs(path string) (ConfirmedDir, string, error)

	// Exists is true if the path exists in the file system.
	Exists(path string) bool

	// Glob returns the list of matching files,
	// emulating https://golang.org/pkg/path/filepath/#Glob
	Glob(pattern string) ([]string, error)

	// ReadFile returns the contents of the file at the given path.
	ReadFile(path string) ([]byte, error)

	// WriteFile writes the data to a file at the given path,
	// overwriting anything that's already there.
	WriteFile(path string, data []byte) error

	// Walk walks the file system with the given WalkFunc.
	Walk(path string, walkFn filepath.WalkFunc) error
}

// ConfirmDir returns an error if the user-specified path is not an existing directory on fSys.
// Otherwise, ConfirmDir returns path, which can be relative, as a ConfirmedDir and all that implies.
func ConfirmDir(fSys FileSystem, path string) (ConfirmedDir, error) {
	if path == "" {
		return "", errors.Errorf("directory path cannot be empty")
	}

	d, f, err := fSys.CleanedAbs(path)
	if err != nil {
		return "", errors.WrapPrefixf(err, "not a valid directory")
	}
	if f != "" {
		//nolint:govet
		return "", errors.WrapPrefixf(errors.Errorf("file is not directory"), fmt.Sprintf("'%s'", path))
	}
	return d, nil
}

// FileSystemOrOnDisk satisfies the FileSystem interface by forwarding
// all of its method calls to the given FileSystem whenever it's not nil.
// If it's nil, the call is forwarded to the OS's underlying file system.
type FileSystemOrOnDisk struct {
	FileSystem FileSystem
}

// Set sets the given FileSystem as the target for all the FileSystem method calls.
func (fs *FileSystemOrOnDisk) Set(f FileSystem) { fs.FileSystem = f }

func (fs FileSystemOrOnDisk) fs() FileSystem {
	if fs.FileSystem != nil {
		return fs.FileSystem
	}
	return MakeFsOnDisk()
}

func (fs FileSystemOrOnDisk) Create(path string) (File, error) {
	return fs.fs().Create(path)
}

func (fs FileSystemOrOnDisk) Mkdir(path string) error {
	return fs.fs().Mkdir(path)
}

func (fs FileSystemOrOnDisk) MkdirAll(path string) error {
	return fs.fs().MkdirAll(path)
}

func (fs FileSystemOrOnDisk) RemoveAll(path string) error {
	return fs.fs().RemoveAll(path)
}

func (fs FileSystemOrOnDisk) Open(path string) (File, error) {
	return fs.fs().Open(path)
}

func (fs FileSystemOrOnDisk) IsDir(path string) bool {
	return fs.fs().IsDir(path)
}

func (fs FileSystemOrOnDisk) ReadDir(path string) ([]string, error) {
	return fs.fs().ReadDir(path)
}

func (fs FileSystemOrOnDisk) CleanedAbs(path string) (ConfirmedDir, string, error) {
	return fs.fs().CleanedAbs(path)
}

func (fs FileSystemOrOnDisk) Exists(path string) bool {
	return fs.fs().Exists(path)
}

func (fs FileSystemOrOnDisk) Glob(pattern string) ([]string, error) {
	return fs.fs().Glob(pattern)
}

func (fs FileSystemOrOnDisk) ReadFile(path string) ([]byte, error) {
	return fs.fs().ReadFile(path)
}

func (fs FileSystemOrOnDisk) WriteFile(path string, data []byte) error {
	return fs.fs().WriteFile(path, data)
}

func (fs FileSystemOrOnDisk) Walk(path string, walkFn filepath.WalkFunc) error {
	return fs.fs().Walk(path, walkFn)
}