Created
May 21, 2015 01:54
-
-
Save virifi/1c7e985eb878077de0a0 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" | |
| "os" | |
| "path/filepath" | |
| ) | |
| func main() { | |
| if len(os.Args) != 3 { | |
| fmt.Fprintf(os.Stderr, "Usage: gen_dummy_file <src directory> <dest directory>\n") | |
| os.Exit(1) | |
| } | |
| srcDir := os.Args[1] | |
| destDir := os.Args[2] | |
| if srcFi, err := os.Stat(srcDir); err != nil { | |
| fmt.Fprintf(os.Stderr, "Src dir '%v' does not exist.\n", srcDir) | |
| os.Exit(1) | |
| } else if !srcFi.IsDir() { | |
| fmt.Fprintf(os.Stderr, "The file '%v' is not a directory.\n", srcDir) | |
| os.Exit(1) | |
| } | |
| // Remove dest directory if exist | |
| if _, err := os.Stat(destDir); err == nil { | |
| if err := os.RemoveAll(destDir); err != nil { | |
| fmt.Fprintf(os.Stderr, "Removing dest directory failed : %v\n", err) | |
| os.Exit(1) | |
| } | |
| } | |
| if err := os.MkdirAll(destDir, 0755); err != nil { | |
| fmt.Fprintf(os.Stderr, "Creating dest directory failed : %v\n", err) | |
| os.Exit(1) | |
| } | |
| if err := genDummyFiles(srcDir, destDir); err != nil { | |
| fmt.Fprintf(os.Stderr, "Generating dummy files failed : %v\n", err) | |
| os.Exit(1) | |
| } | |
| } | |
| func genDummyFiles(srcDir, destDir string) error { | |
| return filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error { | |
| if err != nil { | |
| return err | |
| } | |
| rel, err := filepath.Rel(srcDir, path) | |
| if err != nil { | |
| return err | |
| } | |
| destPath := filepath.Join(destDir, rel) | |
| if info.IsDir() { | |
| return os.MkdirAll(destPath, 0755) | |
| } | |
| df, err := os.Create(destPath) | |
| if err != nil { | |
| return err | |
| } | |
| defer df.Close() | |
| _, err = df.Write([]byte("dummy\n")) | |
| return err | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment