-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add settings related labels and annotations (#247)
* chore: add settings related labels and annotations * chore: add some methods used by command Co-authored-by: fm <[email protected]>
- Loading branch information
Showing
21 changed files
with
667 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io | ||
|
||
// Config configuration for copy operation | ||
type Config struct { | ||
FileFilter []func(srcFile, dstFile string) bool | ||
} | ||
|
||
// CopyOption function for setting CopyConfig | ||
type CopyOption func(config *Config) | ||
|
||
// FileFilterOption construct file filter `CopyOption` | ||
func FileFilterOption(f func(srcFile, dstFile string) bool) CopyOption { | ||
return func(config *Config) { | ||
config.FileFilter = append(config.FileFilter, f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
) | ||
|
||
// copyFile copies a files from src to dst. | ||
func copyFile(src, dst string, srcInfo os.FileInfo, config *Config) error { | ||
for _, filter := range config.FileFilter { | ||
if filter(src, dst) { | ||
return nil | ||
} | ||
} | ||
s, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer s.Close() | ||
|
||
if IsDir(dst) { | ||
dst = path.Join(dst, path.Base(src)) | ||
} else { | ||
if err = os.MkdirAll(path.Dir(dst), os.ModePerm); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
d, err := os.OpenFile(dst, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, srcInfo.Mode()&os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
defer d.Close() | ||
|
||
_, err = io.Copy(d, s) | ||
return err | ||
} | ||
|
||
// copyDir copies a dir from src to dst | ||
func copyDir(src, dst string, srcInfo os.FileInfo, config *Config) error { | ||
var err error | ||
var fds []os.FileInfo | ||
|
||
if err = os.MkdirAll(dst, srcInfo.Mode()&os.ModePerm); err != nil { | ||
return err | ||
} | ||
|
||
if fds, err = ioutil.ReadDir(src); err != nil { | ||
return err | ||
} | ||
for _, fd := range fds { | ||
srcFp := path.Join(src, fd.Name()) | ||
dstFp := path.Join(dst, fd.Name()) | ||
|
||
if err = switchboard(srcFp, dstFp, fd, config); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Copy support copy file to file or dir to dir | ||
func Copy(src, dst string, opts ...CopyOption) error { | ||
config := &Config{} | ||
for _, option := range opts { | ||
option(config) | ||
} | ||
info, err := os.Lstat(src) | ||
if err != nil { | ||
return err | ||
} | ||
return switchboard(src, dst, info, config) | ||
} | ||
|
||
func copyLink(src, dest string, config *Config) (err error) { | ||
var orig string | ||
orig, err = os.Readlink(src) | ||
if err != nil { | ||
return err | ||
} | ||
if !path.IsAbs(orig) { | ||
orig = path.Join(path.Dir(src), orig) | ||
} | ||
var info os.FileInfo | ||
info, err = os.Lstat(orig) | ||
if err != nil { | ||
return err | ||
} | ||
return switchboard(orig, dest, info, config) | ||
} | ||
|
||
func switchboard(src, dest string, info os.FileInfo, config *Config) (err error) { | ||
switch { | ||
case info.Mode()&os.ModeSymlink != 0: | ||
err = copyLink(src, dest, config) | ||
case info.IsDir(): | ||
err = copyDir(src, dest, info, config) | ||
default: | ||
err = copyFile(src, dest, info, config) | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCopy(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
|
||
cleanup := func() { | ||
err := os.RemoveAll("./testdata/dest") | ||
g.Expect(err).Should(Succeed()) | ||
|
||
err = os.MkdirAll("./testdata/dest", 0777) | ||
g.Expect(err).Should(Succeed()) | ||
} | ||
|
||
checkContent := func(file string, expectContent string) { | ||
fileContent, err := ioutil.ReadFile(file) | ||
g.Expect(err).Should(Succeed()) | ||
g.Expect(bytes.Contains(fileContent, []byte(expectContent))).To(BeTrue()) | ||
} | ||
|
||
checkDstFileContent := func(fileName string) { | ||
dstFile := path.Join("./testdata/dest", fileName) | ||
g.Expect(IsFile(dstFile)).To(BeTrue()) | ||
checkContent(dstFile, "hello") | ||
} | ||
|
||
cleanup() | ||
defer cleanup() | ||
|
||
// copy dir to dir | ||
err := Copy("./testdata/src", "./testdata/dest") | ||
g.Expect(err).Should(Succeed()) | ||
checkDstFileContent("source-file") | ||
checkDstFileContent("..source-file-link1") | ||
checkDstFileContent("..source-file-link2") | ||
checkDstFileContent("..source-file-link3") | ||
|
||
// copy file to file | ||
dstFile4 := path.Join("./testdata/dest", "dst-file-1") | ||
err = Copy("./testdata/src/source-file", dstFile4) | ||
g.Expect(err).Should(Succeed()) | ||
checkDstFileContent("dst-file-1") | ||
|
||
dstFile5 := path.Join("./testdata/dest", "..dst-file-2") | ||
err = Copy("./testdata/src/source-file", dstFile5, FileFilterOption(func(srcFile, dstFile string) bool { | ||
if strings.HasPrefix(path.Base(dstFile), "..") { | ||
return true | ||
} | ||
return false | ||
})) | ||
g.Expect(err).Should(Succeed()) | ||
g.Expect(IsExist(dstFile5)).To(BeFalse()) | ||
|
||
dstFile6 := path.Join("./testdata/dest", "..dst-file-3") | ||
err = Copy("./testdata/src/source-file", dstFile6, FileFilterOption(func(srcFile, dstFile string) bool { | ||
if strings.HasPrefix(path.Base(dstFile), "xx") { | ||
return true | ||
} | ||
return false | ||
})) | ||
g.Expect(err).Should(Succeed()) | ||
checkDstFileContent("..dst-file-3") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package io some io operation functions | ||
package io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io | ||
|
||
import ( | ||
"os" | ||
"path" | ||
) | ||
|
||
// WriteFile write the content to the desired file | ||
func WriteFile(dstFile string, content []byte, perm os.FileMode) error { | ||
if err := os.MkdirAll(path.Dir(dstFile), os.ModePerm); err != nil { | ||
return err | ||
} | ||
f, err := os.OpenFile(dstFile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, perm&os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.Write(content) | ||
return err | ||
} | ||
|
||
// IsFile check if the given path is a file | ||
func IsFile(file string) bool { | ||
s, err := os.Stat(file) | ||
if err != nil { | ||
return false | ||
} | ||
return !s.IsDir() | ||
} | ||
|
||
// IsExist check if the given path is existed | ||
func IsExist(path string) bool { | ||
_, err := os.Stat(path) | ||
return err == nil | ||
} | ||
|
||
// IsDir check if the given path is a dir | ||
func IsDir(dir string) bool { | ||
s, err := os.Stat(dir) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return s.IsDir() | ||
} |
Oops, something went wrong.