github.com/mlmmr/revel-cmd@v0.21.2-0.20191112133115-68d8795776dd/revel/clean.go (about) 1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved. 2 // Revel Framework source code and usage is governed by a MIT style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "github.com/mlmmr/revel-cmd/model" 10 "github.com/mlmmr/revel-cmd/utils" 11 "go/build" 12 "os" 13 "path/filepath" 14 ) 15 16 var cmdClean = &Command{ 17 UsageLine: "clean [import path]", 18 Short: "clean a Revel application's temp files", 19 Long: ` 20 Clean the Revel web application named by the given import path. 21 22 For example: 23 24 revel clean github.com/revel/examples/chat 25 26 It removes the app/tmp and app/routes directory. 27 28 29 `, 30 } 31 32 func init() { 33 cmdClean.UpdateConfig = updateCleanConfig 34 cmdClean.RunWith = cleanApp 35 } 36 37 // Update the clean command configuration, using old method 38 func updateCleanConfig(c *model.CommandConfig, args []string) bool { 39 c.Index = model.CLEAN 40 if len(args) == 0 { 41 fmt.Fprintf(os.Stderr, cmdClean.Long) 42 return false 43 } 44 c.Clean.ImportPath = args[0] 45 return true 46 } 47 48 // Clean the source directory of generated files 49 func cleanApp(c *model.CommandConfig) (err error) { 50 appPkg, err := build.Import(c.ImportPath, "", build.FindOnly) 51 if err != nil { 52 utils.Logger.Fatal("Abort: Failed to find import path:", "error", err) 53 } 54 55 purgeDirs := []string{ 56 filepath.Join(appPkg.Dir, "app", "tmp"), 57 filepath.Join(appPkg.Dir, "app", "routes"), 58 } 59 60 for _, dir := range purgeDirs { 61 fmt.Println("Removing:", dir) 62 err = os.RemoveAll(dir) 63 if err != nil { 64 utils.Logger.Error("Failed to clean dir", "error", err) 65 return 66 } 67 } 68 return err 69 }