Skip to content

Instantly share code, notes, and snippets.

@bradly
Created November 19, 2014 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradly/2f963e7c347e7ddbf48a to your computer and use it in GitHub Desktop.
Save bradly/2f963e7c347e7ddbf48a to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "os"
func main() {
source_dir_path := "/some/dir"
destination_dir_path := "/some/other/dir"
ensure_source_directory_exists(source_dir_path)
ensure_destination_directory_exists(destination_dir_path)
file.walk(source_dir_path, handle_file)
fmt.Printf("Done.\n")
os.Exit(0)
}
func handle_file(path string, f os.FileInfo, err Error) (error) {
copy_file(path, destination_dir_path)
return nil
}
func ensure_source_directory_exists(source_dir_path string) (bool) {
result, _ := path_exists(source_dir_path)
if !result {
fmt.Printf("Source dir doesn't exist.\n")
os.Exit(1)
}
return result
}
func ensure_destination_directory_exists(destination_dir_path string) (bool) {
result, _ := path_exists(destination_dir_path)
if !result {
fmt.Printf("Destination dir doesn't exist.\n")
os.Exit(1)
}
return result
}
func path_exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return false, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment