Browse Source

clean code: github.go

pull/103/head
Unknown 12 years ago
parent
commit
864b1d79b1
  1. 71
      doc/github.go

71
doc/github.go

@ -5,17 +5,16 @@
package doc package doc
import ( import (
/*"archive/zip" "archive/zip"
"bytes" "bytes"
"fmt" "io"
"io"*/
"net/http" "net/http"
/*"os" "os"
"path"*/ "path"
"regexp" "regexp"
//"strings" "strings"
//"github.com/GPMGo/gpm/utils" "github.com/GPMGo/gpm/utils"
) )
var ( var (
@ -33,23 +32,27 @@ func GetGithubDoc(client *http.Client, match map[string]string, commit string) (
SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f") SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f")
match["cred"] = githubCred match["cred"] = githubCred
/* // JSON struct for github.com.
var refs []*struct { var refs []*struct {
Ref string
Url string
Object struct { Object struct {
Type string
Sha string Sha string
Type string
Url string Url string
} }
Ref string
Url string
} }
// Check if has specific commit. // bundle and snapshot will have commit 'B' and 'S',
if len(commit) == 0 { // but does not need to download dependencies.
isCheckImport := len(commit) == 0
// Check if download with specific revision.
if isCheckImport || len(commit) == 1 {
// Get up-to-date version. // Get up-to-date version.
err := httpGetJSON(client, expand("https://api.github.com/repos/{owner}/{repo}/git/refs?{cred}", match), &refs) err := httpGetJSON(client, expand("https://api.github.com/repos/{owner}/{repo}/git/refs?{cred}", match), &refs)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
tags := make(map[string]string) tags := make(map[string]string)
@ -65,34 +68,39 @@ func GetGithubDoc(client *http.Client, match map[string]string, commit string) (
// Check revision tag. // Check revision tag.
match["tag"], commit, err = bestTag(tags, "master") match["tag"], commit, err = bestTag(tags, "master")
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
} }
// We use .zip here.
// zip : https://github.com/{owner}/{repo}/archive/{sha}.zip
// tarball : https://github.com/{owner}/{repo}/tarball/{sha}
match["sha"] = commit match["sha"] = commit
// Download zip. // Downlaod archive.
p, err := httpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil) p, err := httpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
r, err := zip.NewReader(bytes.NewReader(p), int64(len(p))) r, err := zip.NewReader(bytes.NewReader(p), int64(len(p)))
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
//defer r.Close()
shaName := expand("{repo}-{sha}", match) shaName := expand("{repo}-{sha}", match)
paths := utils.GetGOPATH() paths := utils.GetGOPATH()
importPath := "github.com/" + expand("{owner}/{repo}", match) importPath := "github.com/" + expand("{owner}/{repo}", match)
installPath := paths[0] + "/src/" + importPath installPath := paths[0] + "/src/" + importPath
// Create destination directory
// Remove old files.
os.RemoveAll(installPath)
// Create destination directory.
os.Mkdir(installPath, os.ModePerm) os.Mkdir(installPath, os.ModePerm)
dirs := make([]string, 0, 5) dirs := make([]string, 0, 5)
for _, f := range r.File { for _, f := range r.File {
absPath := strings.Replace(f.FileInfo().Name(), shaName, installPath, 1) absPath := strings.Replace(f.FileInfo().Name(), shaName, installPath, 1)
fmt.Printf("Unzipping %s...", absPath)
// Check if it is directory or not. // Check if it is directory or not.
if strings.HasSuffix(absPath, "/") { if strings.HasSuffix(absPath, "/") {
@ -101,10 +109,10 @@ func GetGithubDoc(client *http.Client, match map[string]string, commit string) (
continue continue
} }
// Get files from archive // Get files from archive.
rc, err := f.Open() rc, err := f.Open()
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
// Create diretory before create file // Create diretory before create file
@ -112,14 +120,13 @@ func GetGithubDoc(client *http.Client, match map[string]string, commit string) (
// Write data to file // Write data to file
fw, _ := os.Create(absPath) fw, _ := os.Create(absPath)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
n, err := io.Copy(fw, rc) _, err = io.Copy(fw, rc)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
fmt.Println(n)
/*localF, _ := os.Open(absPath) /*localF, _ := os.Open(absPath)
fbytes := make([]byte, f.FileInfo().Size()) fbytes := make([]byte, f.FileInfo().Size())
@ -132,9 +139,15 @@ func GetGithubDoc(client *http.Client, match map[string]string, commit string) (
data: fbytes, data: fbytes,
}) })
}*/ }*/
/* } }
pkg := &Package{ // Check if need to check imports.
if isCheckImport {
} else {
}
/*pkg := &Package{
ImportPath: importPath, ImportPath: importPath,
AbsPath: installPath, AbsPath: installPath,
Commit: commit, Commit: commit,

Loading…
Cancel
Save