refactor: move fs-related functions to submodule
This commit is contained in:
		
							
								
								
									
										46
									
								
								fs/fs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								fs/fs.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| package fs | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| ) | ||||
|  | ||||
| // CaseInsensitive check | ||||
| func CaseInsensitive(dir string) (yes bool, err error) { | ||||
| 	pathLowercase := filepath.Join(dir, "file") | ||||
| 	fLowercase, err := os.Create(pathLowercase) | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 	defer fLowercase.Close() | ||||
| 	defer os.Remove(pathLowercase) | ||||
|  | ||||
| 	pathUppercase := filepath.Join(dir, "FILE") | ||||
| 	fUppercase, err := os.Create(pathUppercase) | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 	defer fUppercase.Close() | ||||
| 	defer os.Remove(pathUppercase) | ||||
|  | ||||
| 	statLowercase, err := fLowercase.Stat() | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	statUppercase, err := fUppercase.Stat() | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	yes = os.SameFile(statLowercase, statUppercase) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| // PathExists check | ||||
| func PathExists(path string) bool { | ||||
| 	if _, err := os.Stat(path); err != nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	return true | ||||
| } | ||||
| @@ -17,6 +17,7 @@ import ( | ||||
| 	"time" | ||||
|  | ||||
| 	"code.dumpstack.io/tools/out-of-tree/config" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/fs" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/qemu" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| ) | ||||
| @@ -58,7 +59,7 @@ func (cmd *ImageEditCmd) Run(g *Globals) (err error) { | ||||
| 	} | ||||
|  | ||||
| 	image := usr.HomeDir + "/.out-of-tree/images/" + cmd.Name | ||||
| 	if !exists(image) { | ||||
| 	if !fs.PathExists(image) { | ||||
| 		fmt.Println("image does not exist") | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -23,6 +23,7 @@ import ( | ||||
| 	"github.com/rs/zerolog/log" | ||||
|  | ||||
| 	"code.dumpstack.io/tools/out-of-tree/config" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/fs" | ||||
| ) | ||||
|  | ||||
| type KernelCmd struct { | ||||
| @@ -352,7 +353,7 @@ func generateBaseDockerImage(registry string, commands []config.DockerCommand, | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	if exists(dockerPath) && string(rawOutput) != "" { | ||||
| 	if fs.PathExists(dockerPath) && string(rawOutput) != "" { | ||||
| 		log.Info().Msgf("Base image for %s:%s found", | ||||
| 			sk.DistroType.String(), sk.DistroRelease) | ||||
| 		if !forceUpdate { | ||||
| @@ -650,7 +651,7 @@ func genRootfsImage(d containerImageInfo, download bool) (rootfs string, err err | ||||
| 	os.MkdirAll(imagesPath, os.ModePerm) | ||||
|  | ||||
| 	rootfs = imagesPath + imageFile | ||||
| 	if !exists(rootfs) { | ||||
| 	if !fs.PathExists(rootfs) { | ||||
| 		if download { | ||||
| 			log.Info().Msgf("%v not available, start download", imageFile) | ||||
| 			err = downloadImage(imagesPath, imageFile) | ||||
|   | ||||
| @@ -16,6 +16,7 @@ import ( | ||||
| 	"github.com/zcalusic/sysinfo" | ||||
|  | ||||
| 	"code.dumpstack.io/tools/out-of-tree/config" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/fs" | ||||
| ) | ||||
|  | ||||
| func genHostKernels(download bool) (kcfg config.KernelConfig, err error) { | ||||
| @@ -85,7 +86,7 @@ func genHostKernels(download bool) (kcfg config.KernelConfig, err error) { | ||||
|  | ||||
| 		vmlinux := "/usr/lib/debug/boot/vmlinux-" + krel | ||||
| 		log.Print("vmlinux", vmlinux) | ||||
| 		if exists(vmlinux) { | ||||
| 		if fs.PathExists(vmlinux) { | ||||
| 			ki.VmlinuxPath = vmlinux | ||||
| 		} | ||||
|  | ||||
|   | ||||
							
								
								
									
										34
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								main.go
									
									
									
									
									
								
							| @@ -23,6 +23,7 @@ import ( | ||||
| 	"github.com/alecthomas/kong" | ||||
|  | ||||
| 	"code.dumpstack.io/tools/out-of-tree/config" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/fs" | ||||
| ) | ||||
|  | ||||
| type Globals struct { | ||||
| @@ -94,37 +95,6 @@ func (lw *LevelWriter) WriteLevel(l zerolog.Level, p []byte) (n int, err error) | ||||
| 	return len(p), nil | ||||
| } | ||||
|  | ||||
| func isFsCaseInsensitive(dir string) (yes bool, err error) { | ||||
| 	pathLowercase := filepath.Join(dir, "file") | ||||
| 	fLowercase, err := os.Create(pathLowercase) | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 	defer fLowercase.Close() | ||||
| 	defer os.Remove(pathLowercase) | ||||
|  | ||||
| 	pathUppercase := filepath.Join(dir, "FILE") | ||||
| 	fUppercase, err := os.Create(pathUppercase) | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 	defer fUppercase.Close() | ||||
| 	defer os.Remove(pathUppercase) | ||||
|  | ||||
| 	statLowercase, err := fLowercase.Stat() | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	statUppercase, err := fUppercase.Stat() | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	yes = os.SameFile(statLowercase, statUppercase) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| var tempDirBase string | ||||
|  | ||||
| var consoleWriter, fileWriter LevelWriter | ||||
| @@ -197,7 +167,7 @@ func main() { | ||||
| 	} | ||||
|  | ||||
| 	path := filepath.Join(usr.HomeDir, ".out-of-tree") | ||||
| 	yes, err := isFsCaseInsensitive(path) | ||||
| 	yes, err := fs.CaseInsensitive(path) | ||||
| 	if err != nil { | ||||
| 		log.Fatal().Err(err).Msg(path) | ||||
| 	} | ||||
|   | ||||
							
								
								
									
										4
									
								
								pack.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								pack.go
									
									
									
									
									
								
							| @@ -9,6 +9,8 @@ import ( | ||||
| 	"io/ioutil" | ||||
| 	"time" | ||||
|  | ||||
| 	"code.dumpstack.io/tools/out-of-tree/fs" | ||||
|  | ||||
| 	"github.com/rs/zerolog/log" | ||||
| ) | ||||
|  | ||||
| @@ -41,7 +43,7 @@ func (cmd *PackCmd) Run(g *Globals) (err error) { | ||||
| 	for _, f := range files { | ||||
| 		workPath := g.WorkDir + "/" + f.Name() | ||||
|  | ||||
| 		if !exists(workPath + "/.out-of-tree.toml") { | ||||
| 		if !fs.PathExists(workPath + "/.out-of-tree.toml") { | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
|   | ||||
							
								
								
									
										10
									
								
								pew.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								pew.go
									
									
									
									
									
								
							| @@ -24,6 +24,7 @@ import ( | ||||
| 	"gopkg.in/logrusorgru/aurora.v2" | ||||
|  | ||||
| 	"code.dumpstack.io/tools/out-of-tree/config" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/fs" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/qemu" | ||||
| ) | ||||
|  | ||||
| @@ -691,7 +692,7 @@ func (cmd PewCmd) testArtifact(swg *sizedwaitgroup.SizedWaitGroup, | ||||
|  | ||||
| 	if cmd.Test == "" { | ||||
| 		cmd.Test = result.BuildArtifact + "_test" | ||||
| 		if !exists(cmd.Test) { | ||||
| 		if !fs.PathExists(cmd.Test) { | ||||
| 			slog.Debug().Msgf("%s does not exist", cmd.Test) | ||||
| 			cmd.Test = tmp + "/source/" + "test.sh" | ||||
| 		} else { | ||||
| @@ -824,13 +825,6 @@ func (cmd PewCmd) performCI(ka config.Artifact) (err error) { | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func exists(path string) bool { | ||||
| 	if _, err := os.Stat(path); err != nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func kernelMask(kernel string) (km config.KernelMask, err error) { | ||||
| 	parts := strings.Split(kernel, ":") | ||||
| 	if len(parts) != 2 { | ||||
|   | ||||
| @@ -18,6 +18,7 @@ import ( | ||||
| 	"github.com/rs/zerolog/log" | ||||
|  | ||||
| 	"code.dumpstack.io/tools/out-of-tree/config" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/fs" | ||||
| 	"code.dumpstack.io/tools/out-of-tree/qemu" | ||||
| ) | ||||
|  | ||||
| @@ -68,7 +69,7 @@ func buildAndInsmod(workPath string, q *qemu.System, ki config.KernelInfo, | ||||
| 	defer os.RemoveAll(tmp) | ||||
|  | ||||
| 	var artifact string | ||||
| 	if exists(cache) { | ||||
| 	if fs.PathExists(cache) { | ||||
| 		artifact = cache | ||||
| 	} else { | ||||
| 		artifact, err = buildPreload(workPath, tmp, ki, dockerTimeout) | ||||
| @@ -124,7 +125,7 @@ func cloneOrPull(repo string, ki config.KernelInfo) (workPath, cache string, err | ||||
| 	workPath = filepath.Join(base, "/repos/", sha1sum(repo)) | ||||
|  | ||||
| 	var r *git.Repository | ||||
| 	if exists(workPath) { | ||||
| 	if fs.PathExists(workPath) { | ||||
| 		r, err = git.PlainOpen(workPath) | ||||
| 		if err != nil { | ||||
| 			return | ||||
|   | ||||
		Reference in New Issue
	
	Block a user