github.com/Xenoex/gopm@v0.6.5/cmd/cmd.go (about)

     1  // Copyright 2013-2014 gopm authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"os"
    19  	"strings"
    20  
    21  	"github.com/Unknwon/com"
    22  	"github.com/codegangsta/cli"
    23  
    24  	"github.com/gpmgo/gopm/doc"
    25  	"github.com/gpmgo/gopm/log"
    26  )
    27  
    28  var (
    29  	workDir string // The path of gopm was executed.
    30  )
    31  
    32  // setup initialize common environment for commands.
    33  func setup(ctx *cli.Context) {
    34  	var err error
    35  	workDir, err = os.Getwd()
    36  	if err != nil {
    37  		log.Error("setup", "Fail to get work directory:")
    38  		log.Fatal("", "\t"+err.Error())
    39  	}
    40  
    41  	log.PureMode = ctx.GlobalBool("noterm")
    42  	log.Verbose = ctx.Bool("verbose")
    43  }
    44  
    45  // parseTarget returns "." when target is empty string.
    46  func parseTarget(target string) string {
    47  	if len(target) == 0 {
    48  		target = "."
    49  	}
    50  	return target
    51  }
    52  
    53  // validPath checks if the information of the package is valid.
    54  func validPath(info string) (string, string) {
    55  	infos := strings.Split(info, ":")
    56  
    57  	l := len(infos)
    58  	switch {
    59  	case l == 1:
    60  		// For local imports.
    61  		if com.IsFile(infos[0]) {
    62  			return doc.LOCAL, infos[0]
    63  		}
    64  	case l == 2:
    65  		switch infos[1] {
    66  		case doc.TRUNK, doc.MASTER, doc.DEFAULT:
    67  			infos[1] = ""
    68  		}
    69  		return infos[0], infos[1]
    70  	}
    71  
    72  	log.Error("", "Cannot parse dependency version:")
    73  	log.Error("", "\t"+info)
    74  	log.Help("Try 'gopm help get' to get more information")
    75  	return "", ""
    76  }
    77  
    78  func versionSuffix(value string) string {
    79  	if len(value) > 0 {
    80  		return "." + value
    81  	}
    82  	return ""
    83  }
    84  
    85  func isSubpackage(rootPath, targetPath string) bool {
    86  	return strings.HasSuffix(strings.Replace(workDir, "\\", "/", -1), rootPath) ||
    87  		strings.HasPrefix(rootPath, targetPath)
    88  }