Created
November 19, 2014 21:37
-
-
Save bradly/2f963e7c347e7ddbf48a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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