Browse Source

clean code: install.go

pull/103/head
Unknown 12 years ago
parent
commit
63a27f647d
  1. 172
      doc/github.go
  2. 6
      install.go

172
doc/github.go

@ -28,108 +28,110 @@ func SetGithubCredentials(id, secret string) {
githubCred = "client_id=" + id + "&client_secret=" + secret githubCred = "client_id=" + id + "&client_secret=" + secret
} }
// GetGithubDoc downloads tarball from github.com.
func GetGithubDoc(client *http.Client, match map[string]string, commit string) (*Package, []string, error) { func GetGithubDoc(client *http.Client, match map[string]string, commit string) (*Package, []string, error) {
/*SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f") SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f")
match["cred"] = githubCred match["cred"] = githubCred
var refs []*struct { /*
Object struct { var refs []*struct {
Type string Object struct {
Sha string Type string
Url string Sha string
} Url string
Ref string }
Url string Ref string
} Url string
// Check if has specific commit.
if len(commit) == 0 {
// Get up-to-date version.
err := httpGetJSON(client, expand("https://api.github.com/repos/{owner}/{repo}/git/refs?{cred}", match), &refs)
if err != nil {
return nil, err
} }
tags := make(map[string]string) // Check if has specific commit.
for _, ref := range refs { if len(commit) == 0 {
switch { // Get up-to-date version.
case strings.HasPrefix(ref.Ref, "refs/heads/"): err := httpGetJSON(client, expand("https://api.github.com/repos/{owner}/{repo}/git/refs?{cred}", match), &refs)
tags[ref.Ref[len("refs/heads/"):]] = ref.Object.Sha if err != nil {
case strings.HasPrefix(ref.Ref, "refs/tags/"): return nil, err
tags[ref.Ref[len("refs/tags/"):]] = ref.Object.Sha
} }
}
// Check revision tag. tags := make(map[string]string)
match["tag"], commit, err = bestTag(tags, "master") for _, ref := range refs {
if err != nil { switch {
return nil, err case strings.HasPrefix(ref.Ref, "refs/heads/"):
} tags[ref.Ref[len("refs/heads/"):]] = ref.Object.Sha
} case strings.HasPrefix(ref.Ref, "refs/tags/"):
tags[ref.Ref[len("refs/tags/"):]] = ref.Object.Sha
match["sha"] = commit }
// Download zip. }
p, err := httpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil)
if err != nil {
return nil, err
}
r, err := zip.NewReader(bytes.NewReader(p), int64(len(p)))
if err != nil {
return nil, err
}
//defer r.Close()
shaName := expand("{repo}-{sha}", match)
paths := utils.GetGOPATH()
importPath := "github.com/" + expand("{owner}/{repo}", match)
installPath := paths[0] + "/src/" + importPath
// Create destination directory
os.Mkdir(installPath, os.ModePerm)
dirs := make([]string, 0, 5)
for _, f := range r.File {
absPath := strings.Replace(f.FileInfo().Name(), shaName, installPath, 1)
fmt.Printf("Unzipping %s...", absPath)
// Check if it is directory or not.
if strings.HasSuffix(absPath, "/") {
// Directory.
dirs = append(dirs, absPath)
continue
}
// Get files from archive // Check revision tag.
rc, err := f.Open() match["tag"], commit, err = bestTag(tags, "master")
if err != nil { if err != nil {
return nil, err return nil, err
}
} }
// Create diretory before create file match["sha"] = commit
os.MkdirAll(path.Dir(absPath), os.ModePerm) // Download zip.
// Write data to file p, err := httpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil)
fw, _ := os.Create(absPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
n, err := io.Copy(fw, rc) r, err := zip.NewReader(bytes.NewReader(p), int64(len(p)))
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Println(n) //defer r.Close()
/*localF, _ := os.Open(absPath) shaName := expand("{repo}-{sha}", match)
fbytes := make([]byte, f.FileInfo().Size()) paths := utils.GetGOPATH()
n, _ := localF.Read(fbytes) importPath := "github.com/" + expand("{owner}/{repo}", match)
installPath := paths[0] + "/src/" + importPath
// Check if Go source file. // Create destination directory
if n > 0 && strings.HasSuffix(absPath, ".go") { os.Mkdir(installPath, os.ModePerm)
files = append(files, &source{
name: srcName, dirs := make([]string, 0, 5)
data: fbytes, for _, f := range r.File {
}) absPath := strings.Replace(f.FileInfo().Name(), shaName, installPath, 1)
}*/ fmt.Printf("Unzipping %s...", absPath)
// Check if it is directory or not.
if strings.HasSuffix(absPath, "/") {
// Directory.
dirs = append(dirs, absPath)
continue
}
// Get files from archive
rc, err := f.Open()
if err != nil {
return nil, err
}
// Create diretory before create file
os.MkdirAll(path.Dir(absPath), os.ModePerm)
// Write data to file
fw, _ := os.Create(absPath)
if err != nil {
return nil, err
}
n, err := io.Copy(fw, rc)
if err != nil {
return nil, err
}
fmt.Println(n)
/*localF, _ := os.Open(absPath)
fbytes := make([]byte, f.FileInfo().Size())
n, _ := localF.Read(fbytes)
// Check if Go source file.
if n > 0 && strings.HasSuffix(absPath, ".go") {
files = append(files, &source{
name: srcName,
data: fbytes,
})
}*/
/* } /* }
pkg := &Package{ pkg := &Package{

6
install.go

@ -105,7 +105,11 @@ func runInstall(cmd *Command, args []string) {
commits := make([]string, len(args)) commits := make([]string, len(args))
downloadPackages(args, commits) downloadPackages(args, commits)
// Install packages all together. if !cmdInstall.Flags["d"] {
// Install packages all together.
fmt.Printf("Installing package: %s.\n")
}
fmt.Println("Well done.") fmt.Println("Well done.")
} }

Loading…
Cancel
Save