package got import ( "fmt" "io/ioutil" "os" "os/exec" "path/filepath" "strings" . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common" ) var ( dependenceMethodDeclared = map[string]bool{} ) func Translate(p *Project) (err error) { handlers := map[string]func(*Project) error { "CreateMainFile":CreateMainFile, "CreateMetricFiles":CreateMetricFiles, "createParamsFile":createParamsFile, "CreateEnvironment":CreateEnvironment, "CreateVariables":CreateVariables, "GenSchemas":GenSchemas, "GenResources":GenResources, "GenMiddlewares":GenMiddlewares, } for stage, fn := range handlers { if err = fn(p); err != nil { return fmt.Errorf("[%s] %s",stage,err.Error()) } } return include(p) } func createSymbolicLinks(p *Project) func(string, os.FileInfo, error) error { var ( basedir = p.Paths.Include("/go") fileContent []byte ) return func(sourcePath string, fileInfo os.FileInfo, errX error) (err error) { if errX != nil { fmt.Printf("error 「%v」 at a path 「%q」\n", errX, sourcePath) return errX } relativePath := strings.Replace(sourcePath, basedir, "", 1) if relativePath == "" { return } if fileInfo.IsDir() { os.MkdirAll(p.Paths.Build("/%s/%s", p.Package, relativePath), 0777) } else { if fileContent, err = ioutil.ReadFile(sourcePath); err != nil { return } if err = ioutil.WriteFile( p.Paths.Build("/%s/%s", p.Package, relativePath), fileContent, 0777, ); err != nil { return } } return nil } } func include(p *Project) error { var err error delete := fmt.Sprintf("find %s -type l -delete", p.Paths.Build("")) if err = exec.Command("sh", "-c", delete).Run(); err != nil { fmt.Println("include error ", err) } if err = filepath.Walk(p.Paths.Include("/go"), createSymbolicLinks(p)); err != nil { return err } return err }