Browse Source

get add extract;get add gitosc support

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

109
cmd/get.go

@ -16,7 +16,7 @@ package cmd
import (
"archive/zip"
//"errors"
"errors"
"fmt"
"io"
"net/http"
@ -123,7 +123,7 @@ func runGet(cmd *Command, args []string) {
// Check length of arguments.
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
}
@ -133,8 +133,18 @@ func runGet(cmd *Command, args []string) {
ver = args[1]
}
pkg := NewPkg(args[0], ver)
if pkg == nil {
doc.ColorLog("[ERROR] Unrecognized package %v.\n", args[0])
return
}
if isStandalone() {
getDirect(pkg)
err := getDirect(pkg)
if err != nil {
doc.ColorLog("[ERROR] %v\n", err)
} else {
fmt.Println("done.")
}
} else {
fmt.Println("Not implemented.")
//getSource(pkgName)
@ -163,6 +173,17 @@ func fileExists(dir string) bool {
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 {
fmt.Println("Downloading", url, "...")
resp, err := http.Get(url)
@ -192,51 +213,78 @@ func download(url string, localfile string) error {
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")
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...)
dstDir := joinPath(gopath, "src", joinPath(childDirs...))
//fmt.Println(dstDir)
var err error
if !update {
if dirExists(srcDir) {
if dirExists(dstDir) {
return nil
}
err = os.MkdirAll(localdir, 0777)
if err != nil {
return err
}
err = os.MkdirAll(dstDir, 0777)
} else {
if dirExists(srcDir) {
os.Remove(localdir)
if dirExists(dstDir) {
err = os.Remove(dstDir)
} else {
err = os.MkdirAll(localdir, 0777)
err = os.MkdirAll(dstDir, 0777)
}
}
if err != nil {
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()
for _, f := range r.File {
//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
}
}
// 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)
newF, err := os.Create(path.Join(dstDir, joinPath(paths...)))
if err == nil {
_, err = io.Copy(newF, rc)
}
if err != nil {
return err
}
rc.Close()
}
return nil
}*/
}
func getPackage(pkg *Pkg, url string) error {
curUser, err := user.Current()
@ -251,31 +299,18 @@ func getPackage(pkg *Pkg, url string) error {
return err
}
urls := strings.Split(url, ".")
localfile := path.Join(localdir, fmt.Sprintf("%v.%v", pkg.VerSimpleString(), urls[len(urls)-1]))
localfile := path.Join(localdir, pkg.FileName())
err = download(url, localfile)
if err != nil {
return err
}
r, err := zip.OpenReader(localfile)
if err != nil {
return err
}
defer r.Close()
if pkg.Ver != TRUNK {
return nil
}
//return extractPkg(pkg)
return nil
return extractPkg(pkg, localfile, false)
}
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 {

67
cmd/source.go

@ -31,6 +31,7 @@ var (
downloadCache map[string]bool // Saves packages that have been downloaded.
sources []Source = []Source{
&GithubSource{},
&GitOscSource{},
}
)
@ -44,8 +45,9 @@ func getSource(pkgName string) Source {
}
type Source interface {
PkgUrl(pkgName string, ver string) string
PkgUrl(pkg *Pkg) string
HasPkg(pkgName string) bool
PkgExt() string
}
type Pkg struct {
@ -69,6 +71,14 @@ func (p *Pkg) VerString() string {
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 {
vers := strings.Split(ver, ":")
if len(vers) > 2 {
@ -80,34 +90,61 @@ func NewPkg(pkgName string, ver string) *Pkg {
verId = vers[1]
}
return &Pkg{
getSource(pkgName), pkgName, vers[0], verId,
source := getSource(pkgName)
if source == nil {
return nil
}
return &Pkg{source, pkgName, vers[0], verId}
}
// github repository
type GithubSource struct {
}
func (s *GithubSource) PkgUrl(pkgName string, ver string) string {
vers := strings.Split(ver, ":")
func (s *GithubSource) PkgUrl(pkg *Pkg) string {
var verPath string
switch strings.ToLower(vers[0]) {
case TRUNK:
if pkg.Ver == TRUNK {
verPath = "master"
case TAG, COMMIT, BRANCH:
if len(vers) != 2 {
return ""
}
verPath = vers[1]
default:
return ""
} else {
verPath = pkg.VerId
}
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 {
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 {
IP string
Username string
Passwd string
PrivateKey string
}

Loading…
Cancel
Save