2013-09-03 17:23:58 +00:00
|
|
|
// Copyright 2013 bee authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"compress/gzip"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
path "path/filepath"
|
2013-06-27 07:29:27 +00:00
|
|
|
"regexp"
|
2013-06-27 02:15:54 +00:00
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdPack = &Command{
|
2013-06-27 07:29:27 +00:00
|
|
|
CustomFlags: true,
|
|
|
|
UsageLine: "pack",
|
2016-12-03 10:54:41 +00:00
|
|
|
Short: "Compresses a Beego application into a single file",
|
|
|
|
Long: `Pack is used to compress Beego applications into a tarball/zip file.
|
2016-12-05 13:40:15 +00:00
|
|
|
This eases the deployment by directly extracting the file to a server.
|
2016-12-03 10:54:41 +00:00
|
|
|
|
|
|
|
{{"Example:"|bold}}
|
|
|
|
$ bee pack -v -ba="-ldflags '-s -w'"
|
2013-06-27 02:15:54 +00:00
|
|
|
`,
|
2016-12-03 10:54:41 +00:00
|
|
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
|
|
|
Run: packApp,
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2013-12-23 08:45:20 +00:00
|
|
|
appPath string
|
|
|
|
excludeP string
|
|
|
|
excludeS string
|
|
|
|
outputP string
|
2013-12-31 11:28:36 +00:00
|
|
|
excludeR ListOpts
|
2013-12-23 08:45:20 +00:00
|
|
|
fsym bool
|
|
|
|
ssym bool
|
|
|
|
build bool
|
|
|
|
buildArgs string
|
2013-12-31 11:28:36 +00:00
|
|
|
buildEnvs ListOpts
|
2013-12-23 08:45:20 +00:00
|
|
|
verbose bool
|
|
|
|
format string
|
2013-06-27 02:15:54 +00:00
|
|
|
)
|
|
|
|
|
2013-12-31 11:28:36 +00:00
|
|
|
type ListOpts []string
|
|
|
|
|
|
|
|
func (opts *ListOpts) String() string {
|
|
|
|
return fmt.Sprint(*opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *ListOpts) Set(value string) error {
|
|
|
|
*opts = append(*opts, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
func init() {
|
2013-06-27 07:29:27 +00:00
|
|
|
fs := flag.NewFlagSet("pack", flag.ContinueOnError)
|
2016-12-03 10:54:41 +00:00
|
|
|
fs.StringVar(&appPath, "p", "", "Set the application path. Defaults to the current path.")
|
|
|
|
fs.BoolVar(&build, "b", true, "Tell the command to do a build for the current platform. Defaults to true.")
|
|
|
|
fs.StringVar(&buildArgs, "ba", "", "Specify additional args for Go build.")
|
|
|
|
fs.Var(&buildEnvs, "be", "Specify additional env variables for Go build. e.g. GOARCH=arm.")
|
|
|
|
fs.StringVar(&outputP, "o", "", "Set the compressed file output path. Defaults to the current path.")
|
|
|
|
fs.StringVar(&format, "f", "tar.gz", "Set file format. Either tar.gz or zip. Defaults to tar.gz.")
|
|
|
|
fs.StringVar(&excludeP, "exp", ".", "Set prefixes of paths to be excluded. Uses a column (:) as separator.")
|
|
|
|
fs.StringVar(&excludeS, "exs", ".go:.DS_Store:.tmp", "Set suffixes of paths to be excluded. Uses a column (:) as separator.")
|
|
|
|
fs.Var(&excludeR, "exr", "Set a regular expression of files to be excluded.")
|
|
|
|
fs.BoolVar(&fsym, "fs", false, "Tell the command to follow symlinks. Defaults to false.")
|
|
|
|
fs.BoolVar(&ssym, "ss", false, "Tell the command to skip symlinks. Defaults to false.")
|
|
|
|
fs.BoolVar(&verbose, "v", false, "Be more verbose during the operation. Defaults to false.")
|
2013-06-27 02:15:54 +00:00
|
|
|
cmdPack.Flag = *fs
|
|
|
|
}
|
|
|
|
|
|
|
|
type walker interface {
|
|
|
|
isExclude(string) bool
|
|
|
|
isEmpty(string) bool
|
|
|
|
relName(string) string
|
|
|
|
virPath(string) string
|
|
|
|
compress(string, string, os.FileInfo) (bool, error)
|
|
|
|
walkRoot(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type byName []os.FileInfo
|
|
|
|
|
|
|
|
func (f byName) Len() int { return len(f) }
|
|
|
|
func (f byName) Less(i, j int) bool { return f[i].Name() < f[j].Name() }
|
|
|
|
func (f byName) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
|
|
|
|
|
|
|
|
type walkFileTree struct {
|
|
|
|
wak walker
|
|
|
|
prefix string
|
|
|
|
excludePrefix []string
|
2013-12-31 11:28:36 +00:00
|
|
|
excludeRegexp []*regexp.Regexp
|
2013-06-27 02:15:54 +00:00
|
|
|
excludeSuffix []string
|
2013-06-28 10:05:00 +00:00
|
|
|
allfiles map[string]bool
|
2016-12-03 10:54:41 +00:00
|
|
|
output *io.Writer
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) setPrefix(prefix string) {
|
|
|
|
wft.prefix = prefix
|
|
|
|
}
|
|
|
|
|
2013-12-31 11:28:36 +00:00
|
|
|
func (wft *walkFileTree) isExclude(fPath string) bool {
|
|
|
|
if fPath == "" {
|
2013-06-27 02:15:54 +00:00
|
|
|
return true
|
|
|
|
}
|
2013-12-31 11:28:36 +00:00
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
for _, prefix := range wft.excludePrefix {
|
2013-12-31 11:28:36 +00:00
|
|
|
if strings.HasPrefix(fPath, prefix) {
|
2013-06-27 02:15:54 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, suffix := range wft.excludeSuffix {
|
2013-12-31 11:28:36 +00:00
|
|
|
if strings.HasSuffix(fPath, suffix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) isExcludeName(name string) bool {
|
|
|
|
for _, r := range wft.excludeRegexp {
|
|
|
|
if r.MatchString(name) {
|
2013-06-27 02:15:54 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 11:28:36 +00:00
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) isEmpty(fpath string) bool {
|
|
|
|
fh, _ := os.Open(fpath)
|
|
|
|
defer fh.Close()
|
|
|
|
infos, _ := fh.Readdir(-1)
|
|
|
|
for _, fi := range infos {
|
2013-12-31 11:28:36 +00:00
|
|
|
fn := fi.Name()
|
|
|
|
fp := path.Join(fpath, fn)
|
2013-06-27 02:15:54 +00:00
|
|
|
if wft.isExclude(wft.virPath(fp)) {
|
|
|
|
continue
|
|
|
|
}
|
2013-12-31 11:28:36 +00:00
|
|
|
if wft.isExcludeName(fn) {
|
|
|
|
continue
|
|
|
|
}
|
2013-06-27 02:15:54 +00:00
|
|
|
if fi.Mode()&os.ModeSymlink > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fi.IsDir() && wft.isEmpty(fp) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) relName(fpath string) string {
|
|
|
|
name, _ := path.Rel(wft.prefix, fpath)
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) virPath(fpath string) string {
|
|
|
|
name := fpath[len(wft.prefix):]
|
|
|
|
if name == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
name = name[1:]
|
2016-01-06 14:09:12 +00:00
|
|
|
name = path.ToSlash(name)
|
2013-06-27 02:15:54 +00:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) readDir(dirname string) ([]os.FileInfo, error) {
|
|
|
|
f, err := os.Open(dirname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list, err := f.Readdir(-1)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Sort(byName(list))
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) walkLeaf(fpath string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-06-27 07:29:27 +00:00
|
|
|
if fpath == outputP {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
if fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if ssym && fi.Mode()&os.ModeSymlink > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-31 11:28:36 +00:00
|
|
|
name := wft.virPath(fpath)
|
|
|
|
|
2013-06-28 10:05:00 +00:00
|
|
|
if wft.allfiles[name] {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
if added, err := wft.wak.compress(name, fpath, fi); added {
|
|
|
|
if verbose {
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(*wft.output, "\t%s%scompressed%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", name, "\x1b[0m")
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
2013-06-28 10:05:00 +00:00
|
|
|
wft.allfiles[name] = true
|
2013-06-27 02:15:54 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-07-22 23:05:01 +00:00
|
|
|
return err
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) iterDirectory(fpath string, fi os.FileInfo) error {
|
|
|
|
doFSym := fsym && fi.Mode()&os.ModeSymlink > 0
|
|
|
|
if doFSym {
|
|
|
|
nfi, err := os.Stat(fpath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fi = nfi
|
|
|
|
}
|
|
|
|
|
2013-12-31 11:28:36 +00:00
|
|
|
relPath := wft.virPath(fpath)
|
|
|
|
|
|
|
|
if len(relPath) > 0 {
|
|
|
|
if wft.isExcludeName(fi.Name()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if wft.isExclude(relPath) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
err := wft.walkLeaf(fpath, fi, nil)
|
|
|
|
if err != nil {
|
|
|
|
if fi.IsDir() && err == path.SkipDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := wft.readDir(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return wft.walkLeaf(fpath, fi, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileInfo := range list {
|
|
|
|
err = wft.iterDirectory(path.Join(fpath, fileInfo.Name()), fileInfo)
|
|
|
|
if err != nil {
|
|
|
|
if !fileInfo.IsDir() || err != path.SkipDir {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *walkFileTree) walkRoot(root string) error {
|
|
|
|
wft.prefix = root
|
|
|
|
fi, err := os.Stat(root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wft.iterDirectory(root, fi)
|
|
|
|
}
|
|
|
|
|
|
|
|
type tarWalk struct {
|
|
|
|
walkFileTree
|
|
|
|
tw *tar.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *tarWalk) compress(name, fpath string, fi os.FileInfo) (bool, error) {
|
|
|
|
isSym := fi.Mode()&os.ModeSymlink > 0
|
|
|
|
link := ""
|
|
|
|
if isSym {
|
|
|
|
link, _ = os.Readlink(fpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr, err := tar.FileInfoHeader(fi, link)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
hdr.Name = name
|
|
|
|
|
|
|
|
tw := wft.tw
|
|
|
|
err = tw.WriteHeader(hdr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isSym == false {
|
|
|
|
fr, err := os.Open(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-07-31 21:30:35 +00:00
|
|
|
defer CloseFile(fr)
|
2013-06-27 02:15:54 +00:00
|
|
|
_, err = io.Copy(tw, fr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
tw.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type zipWalk struct {
|
|
|
|
walkFileTree
|
|
|
|
zw *zip.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wft *zipWalk) compress(name, fpath string, fi os.FileInfo) (bool, error) {
|
|
|
|
isSym := fi.Mode()&os.ModeSymlink > 0
|
|
|
|
|
|
|
|
hdr, err := zip.FileInfoHeader(fi)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
hdr.Name = name
|
|
|
|
|
|
|
|
zw := wft.zw
|
|
|
|
w, err := zw.CreateHeader(hdr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isSym == false {
|
|
|
|
fr, err := os.Open(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-07-31 21:30:35 +00:00
|
|
|
defer CloseFile(fr)
|
2013-06-27 02:15:54 +00:00
|
|
|
_, err = io.Copy(w, fr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2014-04-12 05:12:39 +00:00
|
|
|
} else {
|
|
|
|
var link string
|
|
|
|
if link, err = os.Readlink(fpath); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
_, err = w.Write([]byte(link))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
func packDirectory(output io.Writer, excludePrefix []string, excludeSuffix []string,
|
2013-12-31 11:28:36 +00:00
|
|
|
excludeRegexp []*regexp.Regexp, includePath ...string) (err error) {
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Infof("Excluding relpath prefix: %s", strings.Join(excludePrefix, ":"))
|
|
|
|
logger.Infof("Excluding relpath suffix: %s", strings.Join(excludeSuffix, ":"))
|
2013-12-31 11:28:36 +00:00
|
|
|
if len(excludeRegexp) > 0 {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Infof("Excluding filename regex: `%s`", strings.Join(excludeR, "`, `"))
|
2013-12-31 11:28:36 +00:00
|
|
|
}
|
2013-06-27 02:15:54 +00:00
|
|
|
|
|
|
|
w, err := os.OpenFile(outputP, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var wft walker
|
|
|
|
|
|
|
|
if format == "zip" {
|
|
|
|
walk := new(zipWalk)
|
2016-12-03 10:54:41 +00:00
|
|
|
walk.output = &output
|
2013-06-27 02:15:54 +00:00
|
|
|
zw := zip.NewWriter(w)
|
|
|
|
defer func() {
|
|
|
|
zw.Close()
|
|
|
|
}()
|
2013-06-28 10:05:00 +00:00
|
|
|
walk.allfiles = make(map[string]bool)
|
2013-06-27 02:15:54 +00:00
|
|
|
walk.zw = zw
|
|
|
|
walk.wak = walk
|
|
|
|
walk.excludePrefix = excludePrefix
|
|
|
|
walk.excludeSuffix = excludeSuffix
|
2013-12-31 11:28:36 +00:00
|
|
|
walk.excludeRegexp = excludeRegexp
|
2013-06-27 02:15:54 +00:00
|
|
|
wft = walk
|
|
|
|
} else {
|
|
|
|
walk := new(tarWalk)
|
2016-12-03 10:54:41 +00:00
|
|
|
walk.output = &output
|
2013-06-27 02:15:54 +00:00
|
|
|
cw := gzip.NewWriter(w)
|
|
|
|
tw := tar.NewWriter(cw)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
tw.Flush()
|
|
|
|
cw.Flush()
|
|
|
|
tw.Close()
|
|
|
|
cw.Close()
|
|
|
|
}()
|
2013-06-28 10:05:00 +00:00
|
|
|
walk.allfiles = make(map[string]bool)
|
2013-06-27 02:15:54 +00:00
|
|
|
walk.tw = tw
|
|
|
|
walk.wak = walk
|
|
|
|
walk.excludePrefix = excludePrefix
|
|
|
|
walk.excludeSuffix = excludeSuffix
|
2013-12-31 11:28:36 +00:00
|
|
|
walk.excludeRegexp = excludeRegexp
|
2013-06-27 02:15:54 +00:00
|
|
|
wft = walk
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range includePath {
|
|
|
|
err = wft.walkRoot(p)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-15 09:38:51 +00:00
|
|
|
func packApp(cmd *Command, args []string) int {
|
2016-12-03 10:54:41 +00:00
|
|
|
output := cmd.Out()
|
2013-06-27 02:15:54 +00:00
|
|
|
curPath, _ := os.Getwd()
|
|
|
|
thePath := ""
|
2013-06-27 07:29:27 +00:00
|
|
|
|
|
|
|
nArgs := []string{}
|
|
|
|
has := false
|
|
|
|
for _, a := range args {
|
|
|
|
if a != "" && a[0] == '-' {
|
|
|
|
has = true
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
nArgs = append(nArgs, a)
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 10:54:41 +00:00
|
|
|
cmd.Flag.Parse(nArgs)
|
2013-06-27 02:15:54 +00:00
|
|
|
|
|
|
|
if path.IsAbs(appPath) == false {
|
2013-06-27 07:29:27 +00:00
|
|
|
appPath = path.Join(curPath, appPath)
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
2013-06-27 07:29:27 +00:00
|
|
|
|
|
|
|
thePath, err := path.Abs(appPath)
|
2013-06-27 02:15:54 +00:00
|
|
|
if err != nil {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatalf("Wrong application path: %s", thePath)
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
if stat, err := os.Stat(thePath); os.IsNotExist(err) || stat.IsDir() == false {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatalf("Application path does not exist: %s", thePath)
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Infof("Packaging application on '%s'...", thePath)
|
2013-06-27 07:29:27 +00:00
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
appName := path.Base(thePath)
|
|
|
|
|
|
|
|
goos := runtime.GOOS
|
|
|
|
if v, found := syscall.Getenv("GOOS"); found {
|
|
|
|
goos = v
|
|
|
|
}
|
|
|
|
goarch := runtime.GOARCH
|
|
|
|
if v, found := syscall.Getenv("GOARCH"); found {
|
|
|
|
goarch = v
|
|
|
|
}
|
|
|
|
|
|
|
|
str := strconv.FormatInt(time.Now().UnixNano(), 10)[9:]
|
|
|
|
|
|
|
|
tmpdir := path.Join(os.TempDir(), "beePack-"+str)
|
|
|
|
|
|
|
|
os.Mkdir(tmpdir, 0700)
|
2016-12-04 13:48:42 +00:00
|
|
|
defer func() {
|
|
|
|
// Remove the tmpdir once bee pack is done
|
|
|
|
err := os.RemoveAll(tmpdir)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to remove the generated temp dir")
|
|
|
|
}
|
|
|
|
}()
|
2013-06-27 02:15:54 +00:00
|
|
|
|
|
|
|
if build {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Info("Building application...")
|
2013-12-31 11:28:36 +00:00
|
|
|
var envs []string
|
|
|
|
for _, env := range buildEnvs {
|
|
|
|
parts := strings.SplitN(env, "=", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
k, v := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
|
|
|
|
if len(k) > 0 && len(v) > 0 {
|
|
|
|
switch k {
|
|
|
|
case "GOOS":
|
|
|
|
goos = v
|
|
|
|
case "GOARCH":
|
|
|
|
goarch = v
|
|
|
|
default:
|
|
|
|
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
os.Setenv("GOOS", goos)
|
|
|
|
os.Setenv("GOARCH", goarch)
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Infof("Using: GOOS=%s GOARCH=%s", goos, goarch)
|
2013-12-31 11:28:36 +00:00
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
binPath := path.Join(tmpdir, appName)
|
2013-06-28 10:05:00 +00:00
|
|
|
if goos == "windows" {
|
|
|
|
binPath += ".exe"
|
|
|
|
}
|
|
|
|
|
2013-12-23 08:45:20 +00:00
|
|
|
args := []string{"build", "-o", binPath}
|
|
|
|
if len(buildArgs) > 0 {
|
|
|
|
args = append(args, strings.Fields(buildArgs)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%s+ go %s%s%s\n", "\x1b[32m", "\x1b[1m", strings.Join(args, " "), "\x1b[21m", "\x1b[0m")
|
2013-12-23 08:45:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 19:30:53 +00:00
|
|
|
execmd := exec.Command("go", args...)
|
2013-12-31 11:28:36 +00:00
|
|
|
execmd.Env = append(os.Environ(), envs...)
|
2013-06-27 02:15:54 +00:00
|
|
|
execmd.Stdout = os.Stdout
|
|
|
|
execmd.Stderr = os.Stderr
|
2013-06-28 07:17:12 +00:00
|
|
|
execmd.Dir = thePath
|
2013-06-27 02:15:54 +00:00
|
|
|
err = execmd.Run()
|
|
|
|
if err != nil {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal(err.Error())
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
logger.Success("Build Successful!")
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case "zip":
|
|
|
|
default:
|
|
|
|
format = "tar.gz"
|
|
|
|
}
|
|
|
|
|
2013-06-27 07:29:27 +00:00
|
|
|
outputN := appName + "." + format
|
|
|
|
|
|
|
|
if outputP == "" || path.IsAbs(outputP) == false {
|
|
|
|
outputP = path.Join(curPath, outputP)
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 07:29:27 +00:00
|
|
|
if _, err := os.Stat(outputP); err != nil {
|
|
|
|
err = os.MkdirAll(outputP, 0755)
|
|
|
|
if err != nil {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal(err.Error())
|
2013-06-27 07:29:27 +00:00
|
|
|
}
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 07:29:27 +00:00
|
|
|
outputP = path.Join(outputP, outputN)
|
|
|
|
|
2013-06-27 02:15:54 +00:00
|
|
|
var exp, exs []string
|
|
|
|
for _, p := range strings.Split(excludeP, ":") {
|
|
|
|
if len(p) > 0 {
|
|
|
|
exp = append(exp, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, p := range strings.Split(excludeS, ":") {
|
|
|
|
if len(p) > 0 {
|
|
|
|
exs = append(exs, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 11:28:36 +00:00
|
|
|
var exr []*regexp.Regexp
|
|
|
|
for _, r := range excludeR {
|
|
|
|
if len(r) > 0 {
|
|
|
|
if re, err := regexp.Compile(r); err != nil {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal(err.Error())
|
2013-12-31 11:28:36 +00:00
|
|
|
} else {
|
|
|
|
exr = append(exr, re)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 21:28:52 +00:00
|
|
|
logger.Infof("Writing to output: %s", outputP)
|
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
err = packDirectory(output, exp, exs, exr, tmpdir, thePath)
|
2013-06-27 02:15:54 +00:00
|
|
|
if err != nil {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal(err.Error())
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 21:28:52 +00:00
|
|
|
logger.Success("Application packed!")
|
2014-08-15 09:38:51 +00:00
|
|
|
return 0
|
2013-06-27 02:15:54 +00:00
|
|
|
}
|