// CopyDir copies the contents of an embedded FS in your binary// to some directory on disk.// f is the //go:embed attribute// origin is the name of the directory in the embed call// target is the target directory to write intofuncCopyDir(fembed.FS,origin,targetstring)error{if_,err:=os.Stat(target);os.IsNotExist(err){iferr:=os.MkdirAll(target,os.ModePerm);err!=nil{err=fmt.Errorf("error creating directory: %v",err)returnerr}}files,err:=f.ReadDir(origin)iferr!=nil{err=fmt.Errorf("error reading directory: %v",err)returnerr}for_,file:=rangefiles{sourceFileName:=filepath.Join(origin,file.Name())destFileName:=filepath.Join(target,file.Name())iffile.IsDir(){iferr:=copyDir(f,sourceFileName,destFileName);err!=nil{err=fmt.Errorf("error copying subdirectory: %v",err)returnerr}continue}fileContent,err:=f.ReadFile(sourceFileName)iferr!=nil{err=fmt.Errorf("error reading file: %v",err)returnerr}iferr:=os.WriteFile(destFileName,fileContent,0644);err!=nil{// nolint: goseclog.Printf("error os.WriteFile error: %v",err)err=fmt.Errorf("error writing file: %w",err)returnerr}}returnnil}
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)