github.com/bosssauce/ponzu@v0.11.1-0.20200102001432-9bc41b703131/cmd/ponzu/options.go (about) 1 package main 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 "os" 8 "path/filepath" 9 "strings" 10 ) 11 12 var ponzuRepo = []string{"github.com", "ponzu-cms", "ponzu"} 13 14 func getAnswer() (string, error) { 15 var answer string 16 _, err := fmt.Scanf("%s\n", &answer) 17 if err != nil { 18 if err.Error() == "unexpected newline" { 19 answer = "" 20 } else { 21 return "", err 22 } 23 } 24 25 answer = strings.ToLower(answer) 26 27 return answer, nil 28 } 29 30 func vendorCorePackages(path string) error { 31 vendorPath := filepath.Join(path, "cmd", "ponzu", "vendor", "github.com", "ponzu-cms", "ponzu") 32 err := os.MkdirAll(vendorPath, os.ModeDir|os.ModePerm) 33 if err != nil { 34 return err 35 } 36 37 dirs := []string{"content", "management", "system"} 38 for _, dir := range dirs { 39 err = os.Rename(filepath.Join(path, dir), filepath.Join(vendorPath, dir)) 40 if err != nil { 41 return err 42 } 43 } 44 45 // create a user content directory at project root 46 contentPath := filepath.Join(path, "content") 47 err = os.Mkdir(contentPath, os.ModeDir|os.ModePerm) 48 if err != nil { 49 return err 50 } 51 52 return nil 53 } 54 55 func copyFileNoRoot(src, dst string) error { 56 noRoot := strings.Split(src, string(filepath.Separator))[1:] 57 path := filepath.Join(noRoot...) 58 dstFile, err := os.Create(filepath.Join(dst, path)) 59 defer dstFile.Close() 60 if err != nil { 61 return err 62 } 63 64 srcFile, err := os.Open(src) 65 defer srcFile.Close() 66 if err != nil { 67 return err 68 } 69 70 _, err = io.Copy(dstFile, srcFile) 71 if err != nil { 72 return err 73 } 74 75 return nil 76 } 77 78 func copyFilesWarnConflicts(srcDir, dstDir string, conflicts []string) error { 79 err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error { 80 if err != nil { 81 return err 82 } 83 84 for _, conflict := range conflicts { 85 if info.Name() == conflict { 86 fmt.Println("Ponzu couldn't fully build your project:") 87 fmt.Println("You must rename the following file, as it conflicts with Ponzu core:") 88 fmt.Println(path) 89 fmt.Println("") 90 fmt.Println("Once the files above have been renamed, run '$ ponzu build' to retry.") 91 return errors.New("Ponzu has very few internal conflicts, sorry for the inconvenience.") 92 } 93 } 94 95 if info.IsDir() { 96 // don't copy root directory 97 if path == srcDir { 98 return nil 99 } 100 101 if len(path) > len(srcDir) { 102 path = path[len(srcDir)+1:] 103 } 104 dir := filepath.Join(dstDir, path) 105 err := os.MkdirAll(dir, os.ModeDir|os.ModePerm) 106 if err != nil { 107 return err 108 } 109 110 return nil 111 } 112 113 err = copyFileNoRoot(path, dstDir) 114 if err != nil { 115 return err 116 } 117 118 return nil 119 }) 120 if err != nil { 121 return err 122 } 123 124 return nil 125 } 126 127 func emptyDir(path string) error { 128 d, err := os.Open(path) 129 if err != nil { 130 return err 131 } 132 defer d.Close() 133 134 names, err := d.Readdirnames(-1) 135 if err != nil { 136 return err 137 } 138 for _, name := range names { 139 err = os.RemoveAll(filepath.Join(path, name)) 140 if err != nil { 141 return err 142 } 143 } 144 145 return nil 146 } 147 148 func copyAll(src, dst string) error { 149 err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error { 150 if err != nil { 151 return err 152 } 153 154 sep := string(filepath.Separator) 155 156 // base == the ponzu project dir + string(filepath.Separator) 157 parts := strings.Split(src, sep) 158 base := strings.Join(parts[:len(parts)-1], sep) 159 base += sep 160 161 target := filepath.Join(dst, path[len(base):]) 162 163 // if its a directory, make dir in dst 164 if info.IsDir() { 165 err := os.MkdirAll(target, os.ModeDir|os.ModePerm) 166 if err != nil { 167 return err 168 } 169 } else { 170 // if its a file, move file to dir of dst 171 err = os.Rename(path, target) 172 if err != nil { 173 return err 174 } 175 } 176 177 return nil 178 }) 179 if err != nil { 180 return err 181 } 182 183 return nil 184 }