github.com/jd-ly/cmd@v1.0.10/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/jd-ly/cmd/model"
    10  	"github.com/jd-ly/cmd/utils"
    11  
    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 && c.Clean.ImportPath != "" {
    41  		return true
    42  	}
    43  	if len(args) == 0 {
    44  		fmt.Fprintf(os.Stderr, cmdClean.Long)
    45  		return false
    46  	}
    47  	c.Clean.ImportPath = args[0]
    48  	return true
    49  }
    50  
    51  // Clean the source directory of generated files
    52  func cleanApp(c *model.CommandConfig) (err error) {
    53  
    54  	purgeDirs := []string{
    55  		filepath.Join(c.AppPath, "app", "tmp"),
    56  		filepath.Join(c.AppPath, "app", "routes"),
    57  	}
    58  
    59  	for _, dir := range purgeDirs {
    60  		fmt.Println("Removing:", dir)
    61  		err = os.RemoveAll(dir)
    62  		if err != nil {
    63  			utils.Logger.Error("Failed to clean dir", "error", err)
    64  			return
    65  		}
    66  	}
    67  	return err
    68  }