Exemple #1
0
func copyPathToPath(fromPath, toPath string) (err error) {
	srcFileInfo, err := os.Stat(fromPath)
	if err != nil {
		return
	}

	if srcFileInfo.IsDir() {
		err = os.MkdirAll(toPath, srcFileInfo.Mode())
		if err != nil {
			return
		}
	} else {
		var dst *os.File
		dst, err = fileutils.Create(toPath)
		if err != nil {
			return
		}
		defer dst.Close()

		dst.Chmod(srcFileInfo.Mode())

		err = fileutils.CopyPathToWriter(fromPath, dst)
	}
	return err
}
Exemple #2
0
func (appfiles ApplicationFiles) copyFile(srcPath string, dstPath string, fileMode os.FileMode) error {
	dst, err := fileutils.Create(dstPath)
	if err != nil {
		return err
	}
	defer dst.Close()

	if runtime.GOOS != "windows" {
		err = dst.Chmod(fileMode)
		if err != nil {
			return err
		}
	}

	src, err := os.Open(srcPath)
	if err != nil {
		return err
	}
	defer src.Close()

	_, err = io.Copy(dst, src)
	if err != nil {
		return err
	}

	return nil
}
Exemple #3
0
func (appfiles ApplicationFiles) CopyFiles(appFiles []models.AppFileFields, fromDir, toDir string) error {
	for _, file := range appFiles {
		err := func() error {
			fromPath := filepath.Join(fromDir, file.Path)
			srcFileInfo, err := os.Stat(fromPath)
			if err != nil {
				return err
			}

			toPath := filepath.Join(toDir, file.Path)

			if srcFileInfo.IsDir() {
				err = os.MkdirAll(toPath, srcFileInfo.Mode())
				if err != nil {
					return err
				}
				return nil
			}

			var dst *os.File
			dst, err = fileutils.Create(toPath)
			if err != nil {
				return err
			}
			defer dst.Close()

			dst.Chmod(srcFileInfo.Mode())

			src, err := os.Open(fromPath)
			if err != nil {
				return err
			}
			defer src.Close()

			_, err = io.Copy(dst, src)
			if err != nil {
				return err
			}

			return nil
		}()

		if err != nil {
			return err
		}
	}

	return nil
}
Exemple #4
0
			fileBytes, err := ioutil.ReadFile(filePath)
			Expect(err).NotTo(HaveOccurred())
			Expect(string(fileBytes)).To(Equal("Never Gonna Give You Up"))
		})
	})

	Describe("Create", func() {
		It("truncates an existing file", func() {
			tmpFile, err := ioutil.TempFile("", "create_test")
			Expect(err).NotTo(HaveOccurred())
			_, err = tmpFile.WriteString("Never Gonna Give You Up")
			Expect(err).NotTo(HaveOccurred())
			filePath := tmpFile.Name()
			tmpFile.Close()

			fd, err := fileutils.Create(filePath)
			Expect(err).NotTo(HaveOccurred())

			fileBytes, err := ioutil.ReadAll(fd)
			Expect(err).NotTo(HaveOccurred())
			Expect(len(fileBytes)).To(Equal(0))
			fd.Close()
		})

		It("creates a non-existing file and all intermediary directories", func() {
			filePath := fileutils.TempPath("create_test")

			fd, err := fileutils.Create(filePath)
			Expect(err).NotTo(HaveOccurred())

			_, err = fd.WriteString("Never Gonna Let You Down")