Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Add our own copy.exe binary so that we can copy without cmd.exe #65

Merged
merged 1 commit into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added arduino101load/arduino101copy.exe
Binary file not shown.
44 changes: 44 additions & 0 deletions arduino101load/arduino101copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"io"
"log"
"os"
"fmt"
)

func main() {
args := os.Args[1:]

// Make sure there are only two arguments
if len(args) != 2 {
log.Fatalf("%s <source> <destination>", os.Args[0])
}

source_path := args[0]
destination_path := args[1]

// Make sure the source file exists
source_file, err := os.Open(source_path)
exit_if_error(err)
defer source_file.Close()

// Make sure we can create the destination file
destination_file, err := os.Create(destination_path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're creating an empty version of the dest. file in order to check if you can access that path? is there a cleaner way to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It creates to get a file handle, so that io.Copy() can use it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, fair enough

exit_if_error(err)
defer destination_file.Close()

// Copy
_, err = io.Copy(destination_file, source_file)
exit_if_error(err)

// Report
fmt.Printf("Copied %s to %s\n", source_path, destination_path)
}

func exit_if_error(err error) {
if err != nil {
log.Fatal("ERROR:", err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for using log.* functions for errors, instead of just fmt.Printf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.Fatal is fmt.Print followed by os.Exit(1)
https://golang.org/pkg/log/#Fatal

}
}