github.com/jd-ly/cmd@v1.0.10/revel/package.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  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/jd-ly/cmd/model"
    14  	"github.com/jd-ly/cmd/utils"
    15  )
    16  
    17  var cmdPackage = &Command{
    18  	UsageLine: "package [-r [run mode]] [application] ",
    19  	Short:     "package a Revel application (e.g. for deployment)",
    20  	Long: `
    21  Package the Revel web application named by the given import path.
    22  This allows it to be deployed and run on a machine that lacks a Go installation.
    23  
    24  The run mode is used to select which set of app.conf configuration should
    25  apply and may be used to determine logic in the application itself.
    26  
    27  Run mode defaults to "dev".
    28  
    29  For example:
    30  
    31      revel package github.com/revel/examples/chat
    32  `,
    33  }
    34  
    35  func init() {
    36  	cmdPackage.RunWith = packageApp
    37  	cmdPackage.UpdateConfig = updatePackageConfig
    38  }
    39  
    40  // Called when unable to parse the command line automatically and assumes an old launch
    41  func updatePackageConfig(c *model.CommandConfig, args []string) bool {
    42  	c.Index = model.PACKAGE
    43  	if len(args)==0 && c.Package.ImportPath!="" {
    44  		return true
    45  	}
    46  	c.Package.ImportPath = args[0]
    47  	if len(args) > 1 {
    48  		c.Package.Mode = args[1]
    49  	}
    50  	return true
    51  
    52  }
    53  
    54  // Called to package the app
    55  func packageApp(c *model.CommandConfig) (err error) {
    56  
    57  	// Determine the run mode.
    58  	mode := DefaultRunMode
    59  	if len(c.Package.Mode) >= 0 {
    60  		mode = c.Package.Mode
    61  	}
    62  
    63  	appImportPath := c.ImportPath
    64  	revel_paths, err := model.NewRevelPaths(mode, appImportPath, c.AppPath, model.NewWrappedRevelCallback(nil, c.PackageResolver))
    65  	if err != nil {
    66  		return
    67  	}
    68  
    69  	// Remove the archive if it already exists.
    70  	destFile := filepath.Join(c.AppPath, filepath.Base(revel_paths.BasePath)+".tar.gz")
    71  	if c.Package.TargetPath != "" {
    72  		if filepath.IsAbs(c.Package.TargetPath) {
    73  			destFile = c.Package.TargetPath
    74  		} else {
    75  			destFile = filepath.Join(c.AppPath, c.Package.TargetPath)
    76  		}
    77  	}
    78  	if err := os.Remove(destFile); err != nil && !os.IsNotExist(err) {
    79  		return utils.NewBuildError("Unable to remove target file", "error", err, "file", destFile)
    80  	}
    81  
    82  	// Collect stuff in a temp directory.
    83  	tmpDir, err := ioutil.TempDir("", filepath.Base(revel_paths.BasePath))
    84  	utils.PanicOnError(err, "Failed to get temp dir")
    85  
    86  	// Build expects the command the build to contain the proper data
    87  	if len(c.Package.Mode) >= 0 {
    88  		c.Build.Mode = c.Package.Mode
    89  	}
    90  	c.Build.TargetPath = tmpDir
    91  	c.Build.CopySource = c.Package.CopySource
    92  	if err = buildApp(c); err != nil {
    93  		return
    94  	}
    95  
    96  	// Create the zip file.
    97  
    98  	archiveName, err := utils.TarGzDir(destFile, tmpDir)
    99  	if err != nil {
   100  		return
   101  	}
   102  
   103  	fmt.Println("Your archive is ready:", archiveName)
   104  	return
   105  }