Skip to content

Commit

Permalink
Merge pull request #12 from crazy-max/fix-copyfile
Browse files Browse the repository at this point in the history
fix desync file copying
  • Loading branch information
thaJeztah committed Nov 4, 2021
2 parents cc5eaf0 + 1ceaf72 commit 7e82c46
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
18 changes: 7 additions & 11 deletions clidocstool.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,17 @@ func fileExists(f string) bool {
return !info.IsDir()
}

func copyFile(src string, dest string) error {
srcFile, err := os.Open(src)
func copyFile(src string, dst string) error {
sf, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()

destFile, err := os.Create(dest)
defer sf.Close()
df, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return err
}
defer destFile.Close()

if _, err = io.Copy(destFile, srcFile); err != nil {
return err
}
return destFile.Sync()
defer df.Close()
_, err = io.Copy(df, sf)
return err
}
3 changes: 3 additions & 0 deletions clidocstool_md_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func TestGenMarkdownTree(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

err = copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
require.NoError(t, err)

c, err := New(Options{
Root: buildxCmd,
SourceDir: tmpdir,
Expand Down
3 changes: 3 additions & 0 deletions clidocstool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func TestGenAllTree(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

err = copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
require.NoError(t, err)

c, err := New(Options{
Root: buildxCmd,
SourceDir: tmpdir,
Expand Down
5 changes: 5 additions & 0 deletions fixtures/buildx_stop.pre.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# docker buildx stop

<!---MARKER_GEN_START-->
<!---MARKER_GEN_END-->

0 comments on commit 7e82c46

Please sign in to comment.