Browse Source

get add extract;get add gitosc support

pull/103/head
Lunny Xiao 12 years ago
parent
commit
a6f7f2b74b
  1. 105
      cmd/get.go
  2. 67
      cmd/source.go

105
cmd/get.go

@ -16,7 +16,7 @@ package cmd
import ( import (
"archive/zip" "archive/zip"
//"errors" "errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -123,7 +123,7 @@ func runGet(cmd *Command, args []string) {
// Check length of arguments. // Check length of arguments.
if len(args) < 1 { if len(args) < 1 {
doc.ColorLog("[ERRO] Please list the package that you want to install.\n") doc.ColorLog("[ERROR] Please list the package that you want to install.\n")
return return
} }
@ -133,8 +133,18 @@ func runGet(cmd *Command, args []string) {
ver = args[1] ver = args[1]
} }
pkg := NewPkg(args[0], ver) pkg := NewPkg(args[0], ver)
if pkg == nil {
doc.ColorLog("[ERROR] Unrecognized package %v.\n", args[0])
return
}
if isStandalone() { if isStandalone() {
getDirect(pkg) err := getDirect(pkg)
if err != nil {
doc.ColorLog("[ERROR] %v\n", err)
} else {
fmt.Println("done.")
}
} else { } else {
fmt.Println("Not implemented.") fmt.Println("Not implemented.")
//getSource(pkgName) //getSource(pkgName)
@ -163,6 +173,17 @@ func fileExists(dir string) bool {
return !info.IsDir() return !info.IsDir()
} }
func joinPath(paths ...string) string {
if len(paths) < 1 {
return ""
}
res := ""
for _, p := range paths {
res = path.Join(res, p)
}
return res
}
func download(url string, localfile string) error { func download(url string, localfile string) error {
fmt.Println("Downloading", url, "...") fmt.Println("Downloading", url, "...")
resp, err := http.Get(url) resp, err := http.Get(url)
@ -192,51 +213,78 @@ func download(url string, localfile string) error {
return nil return nil
} }
/*func extractPkg(pkg *Pkg, update bool) error { func extractPkg(pkg *Pkg, localfile string, update bool) error {
fmt.Println("Extracting package", pkg.Name, "...")
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")
var childDirs []string = strings.Split(pkg.Name, "/") var childDirs []string = strings.Split(pkg.Name, "/")
if pkg.Ver != TRUNK { if pkg.Ver != TRUNK {
childDirs[len(childDirs)-1] = fmt.Sprintf("%v_%v_%v", childDirs[len(childDirs)-1], pkg.Ver, pkg.VerId) childDirs[len(childDirs)-1] = fmt.Sprintf("%v_%v_%v", childDirs[len(childDirs)-1], pkg.Ver, pkg.VerId)
} }
srcDir = path.Join(gopath, childDir...) dstDir := joinPath(gopath, "src", joinPath(childDirs...))
//fmt.Println(dstDir)
var err error
if !update { if !update {
if dirExists(srcDir) { if dirExists(dstDir) {
return nil return nil
} }
err = os.MkdirAll(localdir, 0777) err = os.MkdirAll(dstDir, 0777)
if err != nil {
return err
}
} else { } else {
if dirExists(srcDir) { if dirExists(dstDir) {
os.Remove(localdir) err = os.Remove(dstDir)
} else { } else {
err = os.MkdirAll(localdir, 0777) err = os.MkdirAll(dstDir, 0777)
}
}
if err != nil { if err != nil {
return err return err
} }
if path.Ext(localfile) != ".zip" {
return errors.New("Not implemented!")
} }
r, err := zip.OpenReader(localfile)
if err != nil {
return err
} }
defer r.Close()
// Iterate through the files in the archive,
// printing some of their contents.
for _, f := range r.File { for _, f := range r.File {
fmt.Printf("Contents of %s:\n", f.Name) //fmt.Printf("Contents of %s:\n", f.Name)
paths := strings.Split(f.Name, "/")[1:]
//fmt.Println(paths)
if len(paths) < 1 {
continue
}
if f.FileInfo().IsDir() {
childDir := joinPath(dstDir, joinPath(paths...))
err = os.MkdirAll(childDir, 0777)
if err != nil {
return err
}
continue
}
rc, err := f.Open() rc, err := f.Open()
if err != nil { if err != nil {
return err return err
} }
_, err = io.Copy(os.Stdout, rc) newF, err := os.Create(path.Join(dstDir, joinPath(paths...)))
if err == nil {
_, err = io.Copy(newF, rc)
}
if err != nil { if err != nil {
return err return err
} }
rc.Close() rc.Close()
} }
return nil return nil
}*/ }
func getPackage(pkg *Pkg, url string) error { func getPackage(pkg *Pkg, url string) error {
curUser, err := user.Current() curUser, err := user.Current()
@ -251,31 +299,18 @@ func getPackage(pkg *Pkg, url string) error {
return err return err
} }
urls := strings.Split(url, ".") localfile := path.Join(localdir, pkg.FileName())
localfile := path.Join(localdir, fmt.Sprintf("%v.%v", pkg.VerSimpleString(), urls[len(urls)-1]))
err = download(url, localfile) err = download(url, localfile)
if err != nil { if err != nil {
return err return err
} }
r, err := zip.OpenReader(localfile) return extractPkg(pkg, localfile, false)
if err != nil {
return err
}
defer r.Close()
if pkg.Ver != TRUNK {
return nil
}
//return extractPkg(pkg)
return nil
} }
func getDirect(pkg *Pkg) error { func getDirect(pkg *Pkg) error {
return getPackage(pkg, pkg.Source.PkgUrl(pkg.Name, pkg.VerString())) return getPackage(pkg, pkg.Url())
} }
/*func getFromSource(pkgName string, ver string, source string) error { /*func getFromSource(pkgName string, ver string, source string) error {

67
cmd/source.go

@ -31,6 +31,7 @@ var (
downloadCache map[string]bool // Saves packages that have been downloaded. downloadCache map[string]bool // Saves packages that have been downloaded.
sources []Source = []Source{ sources []Source = []Source{
&GithubSource{}, &GithubSource{},
&GitOscSource{},
} }
) )
@ -44,8 +45,9 @@ func getSource(pkgName string) Source {
} }
type Source interface { type Source interface {
PkgUrl(pkgName string, ver string) string PkgUrl(pkg *Pkg) string
HasPkg(pkgName string) bool HasPkg(pkgName string) bool
PkgExt() string
} }
type Pkg struct { type Pkg struct {
@ -69,6 +71,14 @@ func (p *Pkg) VerString() string {
return fmt.Sprintf("%v:%v", p.Ver, p.VerId) return fmt.Sprintf("%v:%v", p.Ver, p.VerId)
} }
func (p *Pkg) Url() string {
return p.Source.PkgUrl(p)
}
func (p *Pkg) FileName() string {
return fmt.Sprintf("%v.%v", p.VerSimpleString(), p.Source.PkgExt())
}
func NewPkg(pkgName string, ver string) *Pkg { func NewPkg(pkgName string, ver string) *Pkg {
vers := strings.Split(ver, ":") vers := strings.Split(ver, ":")
if len(vers) > 2 { if len(vers) > 2 {
@ -80,34 +90,61 @@ func NewPkg(pkgName string, ver string) *Pkg {
verId = vers[1] verId = vers[1]
} }
return &Pkg{ source := getSource(pkgName)
getSource(pkgName), pkgName, vers[0], verId, if source == nil {
return nil
} }
return &Pkg{source, pkgName, vers[0], verId}
} }
// github repository
type GithubSource struct { type GithubSource struct {
} }
func (s *GithubSource) PkgUrl(pkgName string, ver string) string { func (s *GithubSource) PkgUrl(pkg *Pkg) string {
vers := strings.Split(ver, ":")
var verPath string var verPath string
switch strings.ToLower(vers[0]) { if pkg.Ver == TRUNK {
case TRUNK:
verPath = "master" verPath = "master"
case TAG, COMMIT, BRANCH: } else {
if len(vers) != 2 { verPath = pkg.VerId
return ""
}
verPath = vers[1]
default:
return ""
} }
return fmt.Sprintf("https://%v/archive/%v.zip", pkgName, verPath) return fmt.Sprintf("https://%v/archive/%v.zip", pkg.Name, verPath)
} }
func (s *GithubSource) HasPkg(pkgName string) bool { func (s *GithubSource) HasPkg(pkgName string) bool {
return strings.HasPrefix(pkgName, "github.com") return strings.HasPrefix(pkgName, "github.com")
} }
func (s *GithubSource) PkgExt() string {
return "zip"
}
// git osc repos
type GitOscSource struct {
}
func (s *GitOscSource) PkgUrl(pkg *Pkg) string {
var verPath string
if pkg.Ver == TRUNK {
verPath = "master"
} else {
verPath = pkg.VerId
}
return fmt.Sprintf("https://%v/repository/archive?ref=%v", pkg.Name, verPath)
}
func (s *GitOscSource) HasPkg(pkgName string) bool {
return strings.HasPrefix(pkgName, "git.oschina.net")
}
func (s *GitOscSource) PkgExt() string {
return "zip"
}
type GitLabSource struct { type GitLabSource struct {
IP string
Username string
Passwd string
PrivateKey string
} }

Loading…
Cancel
Save