dispatch cmd/main into files.go

This commit is contained in:
Adrien Marquès 2018-07-09 00:29:25 +02:00
parent 5e14394966
commit d04bafa0eb
2 changed files with 34 additions and 20 deletions

25
cmd/aicra/files.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"os"
"path/filepath"
)
// Returns an absolute path from the @path variable if already absolute
// if @path is relative, it is processed relative to the @base directory
func getAbsPath(base string, path string) (string, error) {
// already absolute
if filepath.IsAbs(path) {
return path, nil
}
// relative: join from @base dir
return filepath.Abs(filepath.Join(base, path))
}
// Returns whether a directory exists for the path @path
func dirExists(path string) bool {
stat, err := os.Stat(path)
return err != nil || !stat.IsDir()
}

View File

@ -46,30 +46,21 @@ func main() {
return return
} }
/* (2) Get absolute controllers' path */ /* (2) Get absolute controllers' path */
if !filepath.IsAbs(*ctlPathFlag) { cPath, err := getAbsPath(projectPath, *ctlPathFlag)
*ctlPathFlag = filepath.Join(projectPath, *ctlPathFlag)
}
cPath, err := filepath.Abs(*ctlPathFlag)
if err != nil { if err != nil {
fmt.Printf("invalid argument: controllers' path\n") fmt.Printf("invalid argument: controllers' path\n")
return return
} }
/* (3) Get absolute types' path */ /* (3) Get absolute types' path */
if !filepath.IsAbs(*typPathFlag) { tPath, err := getAbsPath(projectPath, *typPathFlag)
*typPathFlag = filepath.Join(projectPath, *typPathFlag)
}
tPath, err := filepath.Abs(*typPathFlag)
if err != nil { if err != nil {
fmt.Printf("invalid argument: types' path\n") fmt.Printf("invalid argument: types' path\n")
return return
} }
/* (4) Get absolute middlewares' path */ /* (4) Get absolute middlewares' path */
if !filepath.IsAbs(*midPathFlag) { mPath, err := getAbsPath(projectPath, *midPathFlag)
*midPathFlag = filepath.Join(projectPath, *midPathFlag)
}
mPath, err := filepath.Abs(*midPathFlag)
if err != nil { if err != nil {
fmt.Printf("invalid argument: middlwares' path\n") fmt.Printf("invalid argument: middlwares' path\n")
return return
@ -84,18 +75,17 @@ func main() {
/* (1) Project path */ /* (1) Project path */
clifmt.Align(" . project root") clifmt.Align(" . project root")
if stat, err := os.Stat(projectPath); err != nil || !stat.IsDir() { if !dirExists(projectPath) {
fmt.Printf("invalid\n\n") fmt.Printf("invalid\n\n")
fmt.Printf("%s invalid project folder - %s\n\n", clifmt.Warn(), clifmt.Color(36, projectPath)) fmt.Printf("%s invalid project folder - %s\n\n", clifmt.Warn(), clifmt.Color(36, projectPath))
fmt.Printf("You must specify an existing directory path\n") fmt.Printf("You must specify an existing directory path\n")
return return
} else {
fmt.Printf("ok\n")
} }
fmt.Printf("ok\n")
/* (2) Controllers path */ /* (2) Controllers path */
clifmt.Align(" . controllers") clifmt.Align(" . controllers")
if stat, err := os.Stat(cPath); err != nil || !stat.IsDir() { if !dirExists(cPath) {
compileControllers = false compileControllers = false
fmt.Printf("missing\n") fmt.Printf("missing\n")
} else { } else {
@ -104,7 +94,7 @@ func main() {
/* (3) Middlewares path */ /* (3) Middlewares path */
clifmt.Align(" . middlewares") clifmt.Align(" . middlewares")
if stat, err := os.Stat(cPath); err != nil || !stat.IsDir() { if !dirExists(mPath) {
compileMiddlewares = false compileMiddlewares = false
fmt.Printf("missing\n") fmt.Printf("missing\n")
} else { } else {
@ -113,7 +103,7 @@ func main() {
/* (4) Default types path */ /* (4) Default types path */
clifmt.Align(" . default types") clifmt.Align(" . default types")
if stat, err := os.Stat(dtPath); err != nil || !stat.IsDir() { if !dirExists(dtPath) {
fmt.Printf("missing\n") fmt.Printf("missing\n")
compileTypes = false compileTypes = false
@ -123,10 +113,9 @@ func main() {
/* (5) Types path */ /* (5) Types path */
clifmt.Align(" . custom types") clifmt.Align(" . custom types")
if stat, err := os.Stat(tPath); err != nil || !stat.IsDir() { if !dirExists(tPath) {
fmt.Printf("missing\n") fmt.Printf("missing\n")
compileTypes = false compileTypes = false
} else { } else {
fmt.Printf("ok\n") fmt.Printf("ok\n")
} }