You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
4.3 KiB

12 years ago
package cmd
12 years ago
import (
"archive/zip"
//"errors"
12 years ago
"fmt"
"io"
"net/http"
"os"
"os/user"
"path"
"path/filepath"
"strings"
)
12 years ago
var CmdGet = &Command{
12 years ago
UsageLine: "get [-u] [packages]",
Short: "download and install packages and dependencies",
Long: `
Get downloads and installs the packages named by the import paths,
along with their dependencies.
The -u flag instructs get to use the network to update the named packages
and their dependencies. By default, get uses the network to check out
missing packages but does not use it to look for updates to existing packages.
Get also accepts all the flags in the 'go build' and 'go install' commands,
to control the installation. See 'go help build'.
When checking out or updating a package, get looks for a branch or tag
that matches the locally installed version of Go. The most important
rule is that if the local installation is running version "go1", get
searches for a branch or tag named "go1". If no such version exists it
retrieves the most recent version of the package.
For more about specifying packages, see 'go help packages'.
For more about how 'gopm get' finds source code to
download, see 'gopm help'.
12 years ago
See also: gopm build, gopm install, gopm clean.
12 years ago
`,
}
12 years ago
var getD = CmdGet.Flag.Bool("f", false, "")
var getU = CmdGet.Flag.Bool("u", false, "")
12 years ago
func init() {
12 years ago
CmdGet.Run = runGet
12 years ago
}
func isStandalone() bool {
return true
}
12 years ago
func runGet(cmd *Command, args []string) {
if len(args) > 0 {
var ver string = TRUNK
if len(args) == 2 {
ver = args[1]
}
pkg := NewPkg(args[0], ver)
if isStandalone() {
getDirect(pkg)
} else {
fmt.Println("Not implemented.")
//getSource(pkgName)
}
12 years ago
}
}
func dirExists(dir string) bool {
d, e := os.Stat(dir)
switch {
case e != nil:
return false
case !d.IsDir():
return false
}
return true
}
func fileExists(dir string) bool {
info, err := os.Stat(dir)
if err != nil {
return false
}
return !info.IsDir()
}
func download(url string, localfile string) error {
fmt.Println("Downloading", url, "...")
12 years ago
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
localdir := filepath.Dir(localfile)
if !dirExists(localdir) {
err = os.MkdirAll(localdir, 0777)
if err != nil {
return err
}
}
if !fileExists(localfile) {
f, err := os.Create(localfile)
if err == nil {
_, err = io.Copy(f, resp.Body)
}
if err != nil {
return err
}
}
return nil
}
/*func extractPkg(pkg *Pkg, update bool) error {
gopath := os.Getenv("GOPATH")
var childDirs []string = strings.Split(pkg.Name, "/")
if pkg.Ver != TRUNK {
childDirs[len(childDirs)-1] = fmt.Sprintf("%v_%v_%v", childDirs[len(childDirs)-1], pkg.Ver, pkg.VerId)
}
srcDir = path.Join(gopath, childDir...)
if !update {
if dirExists(srcDir) {
return nil
}
err = os.MkdirAll(localdir, 0777)
if err != nil {
return err
}
} else {
if dirExists(srcDir) {
os.Remove(localdir)
} else {
err = os.MkdirAll(localdir, 0777)
if err != nil {
return err
}
}
}
// Iterate through the files in the archive,
// printing some of their contents.
for _, f := range r.File {
fmt.Printf("Contents of %s:\n", f.Name)
rc, err := f.Open()
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, rc)
if err != nil {
return err
}
rc.Close()
}
return nil
}*/
func getPackage(pkg *Pkg, url string) error {
12 years ago
curUser, err := user.Current()
if err != nil {
return err
}
reposDir = strings.Replace(reposDir, "~", curUser.HomeDir, -1)
localdir := path.Join(reposDir, pkg.Name)
12 years ago
localdir, err = filepath.Abs(localdir)
if err != nil {
return err
}
urls := strings.Split(url, ".")
12 years ago
localfile := path.Join(localdir, fmt.Sprintf("%v.%v", pkg.VerSimpleString(), urls[len(urls)-1]))
12 years ago
err = download(url, localfile)
if err != nil {
return err
}
12 years ago
r, err := zip.OpenReader(localfile)
if err != nil {
return err
}
defer r.Close()
if pkg.Ver != TRUNK {
return nil
}
//return extractPkg(pkg)
return nil
12 years ago
}
func getDirect(pkg *Pkg) error {
return getPackage(pkg, pkg.Source.PkgUrl(pkg.Name, pkg.VerString()))
}
/*func getFromSource(pkgName string, ver string, source string) error {
12 years ago
urlTempl := "https://%v/%v"
//urlTempl := "https://%v/archive/master.zip"
url := fmt.Sprintf(urlTempl, source, pkgName)
return getPackage(pkgName, ver, url)
}*/