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.

362 lines
8.6 KiB

12 years ago
// Copyright (c) 2013 GPMGo Members. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// gpm(Go Package Manager) is a Go package manage tool for search, install, update and share packages in Go.
package main
import (
12 years ago
"encoding/json"
12 years ago
"fmt"
"io"
"os"
"os/exec"
"path"
12 years ago
"path/filepath"
"runtime"
"strings"
"sync"
"text/template"
"unicode"
"unicode/utf8"
"github.com/BurntSushi/toml"
12 years ago
"github.com/GPMGo/gopm/cmd"
"github.com/GPMGo/gopm/doc"
"github.com/GPMGo/gopm/utils"
12 years ago
)
// Commands lists the available commands and help topics.
// The order here is the order in which they are printed by 'gpm help'.
12 years ago
var commands = []*cmd.Command{
cmd.CmdBuild,
cmd.CmdSearch,
cmd.CmdInstall,
cmd.CmdRemove,
cmd.CmdCheck,
}
12 years ago
// getAppPath returns application execute path for current process.
func getAppPath() bool {
// Look up executable in PATH variable.
12 years ago
cmd.AppPath, _ = exec.LookPath(path.Base(os.Args[0]))
// Check if run under $GOPATH/bin
12 years ago
if !utils.IsExist(cmd.AppPath + "conf/") {
paths := utils.GetGOPATH()
for _, p := range paths {
if utils.IsExist(p + "/src/github.com/GPMGo/gopm/") {
12 years ago
cmd.AppPath = p + "/src/github.com/GPMGo/gopm/"
break
}
}
}
12 years ago
12 years ago
if len(cmd.AppPath) == 0 {
utils.ColorPrint("[ERROR] getAppPath ->[ Unable to indicate current execute path. ]\n")
return false
}
12 years ago
12 years ago
cmd.AppPath = filepath.Dir(cmd.AppPath) + "/"
if runtime.GOOS == "windows" {
// Replace all '\' to '/'.
12 years ago
cmd.AppPath = strings.Replace(cmd.AppPath, "\\", "/", -1)
12 years ago
}
12 years ago
doc.SetAppConfig(cmd.AppPath, cmd.Config.AutoBackup)
return true
12 years ago
}
// loadPromptMsg loads prompt messages according to user language.
func loadPromptMsg(lang string) bool {
12 years ago
cmd.PromptMsg = make(map[string]string)
// Load prompt messages.
12 years ago
f, err := os.Open(cmd.AppPath + "i18n/" + lang + "/prompt.txt")
if err != nil {
utils.ColorPrint(fmt.Sprintf("[ERROR] loadUsage -> Fail to load prompt messages[ %s ]\n", err))
return false
}
defer f.Close()
// Read prompt messages.
fi, _ := f.Stat()
promptBytes := make([]byte, fi.Size())
f.Read(promptBytes)
promptStrs := strings.Split(string(promptBytes), "\n")
for _, p := range promptStrs {
i := strings.Index(p, "=")
if i > -1 {
12 years ago
cmd.PromptMsg[p[:i]] = p[i+1:]
}
}
return true
}
// loadUsage loads usage according to user language.
12 years ago
func loadUsage(lang string) bool {
if !loadPromptMsg(lang) {
return false
}
// Load main usage.
12 years ago
f, err := os.Open(cmd.AppPath + "i18n/" + lang + "/usage.tpl")
if err != nil {
12 years ago
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] loadUsage -> %s\n", cmd.PromptMsg["LoadCommandUsage"]), "main", err))
return false
}
defer f.Close()
// Read main usages.
fi, _ := f.Stat()
usageBytes := make([]byte, fi.Size())
f.Read(usageBytes)
usageTemplate = string(usageBytes)
// Load command usage.
12 years ago
for _, command := range commands {
f, err := os.Open(cmd.AppPath + "i18n/" + lang + "/usage_" + command.Name() + ".txt")
if err != nil {
12 years ago
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] loadUsage -> %s\n", cmd.PromptMsg["LoadCommandUsage"]), command.Name(), err))
return false
}
defer f.Close()
// Read usage.
fi, _ := f.Stat()
usageBytes := make([]byte, fi.Size())
f.Read(usageBytes)
usages := strings.Split(string(usageBytes), "|||")
if len(usages) < 2 {
utils.ColorPrint(fmt.Sprintf(
12 years ago
fmt.Sprintf("[ERROR] loadUsage -> %s\n", cmd.PromptMsg["ReadCoammndUsage"]), command.Name()))
return false
}
12 years ago
command.Short = usages[0]
command.Long = usages[1]
}
return true
}
12 years ago
// loadLocalNodes loads nodes information from local file system.
func loadLocalNodes() bool {
12 years ago
if !utils.IsExist(cmd.AppPath + "data/nodes.json") {
os.MkdirAll(cmd.AppPath+"data/", os.ModePerm)
12 years ago
} else {
12 years ago
fr, err := os.Open(cmd.AppPath + "data/nodes.json")
12 years ago
if err != nil {
12 years ago
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] loadLocalNodes -> %s\n", cmd.PromptMsg["LoadLocalData"]), err))
12 years ago
return false
}
defer fr.Close()
12 years ago
err = json.NewDecoder(fr).Decode(&cmd.LocalNodes)
12 years ago
if err != nil && err != io.EOF {
12 years ago
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] loadLocalNodes -> %s\n", cmd.PromptMsg["ParseJSON"]), err))
12 years ago
return false
}
}
return true
}
// loadLocalBundles loads bundles from local file system.
func loadLocalBundles() bool {
// Find all bundles.
12 years ago
dir, err := os.Open(cmd.AppPath + "repo/bundles/")
if err != nil {
12 years ago
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] loadLocalBundles -> %s\n", cmd.PromptMsg["OpenFile"]), err))
return false
}
defer dir.Close()
fis, err := dir.Readdir(0)
if err != nil {
12 years ago
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] loadLocalBundles -> %s\n", cmd.PromptMsg["OpenFile"]), err))
return false
}
for _, fi := range fis {
// In case this folder contains unexpected directories.
if !fi.IsDir() && strings.HasSuffix(fi.Name(), ".json") {
12 years ago
fr, err := os.Open(cmd.AppPath + "repo/bundles/" + fi.Name())
if err != nil {
12 years ago
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] loadLocalBundles -> %s\n", cmd.PromptMsg["OpenFile"]), err))
return false
}
bundle := new(doc.Bundle)
err = json.NewDecoder(fr).Decode(bundle)
fr.Close()
if err != nil && err != io.EOF {
12 years ago
utils.ColorPrint(fmt.Sprintf(fmt.Sprintf("[ERROR] loadLocalBundles -> %s\n", cmd.PromptMsg["ParseJSON"]), err))
return false
}
// Make sure bundle name is not empty.
if len(bundle.Name) == 0 {
bundle.Name = fi.Name()[:strings.Index(fi.Name(), ".")]
}
12 years ago
cmd.LocalBundles = append(cmd.LocalBundles, bundle)
}
}
return true
}
// We don't use init() to initialize
// bacause we need to get execute path in runtime.
func initialize() bool {
12 years ago
// Try to have highest performance.
runtime.GOMAXPROCS(runtime.NumCPU())
// Get application execute path.
if !getAppPath() {
return false
}
12 years ago
// Load configuration.
12 years ago
if _, err := toml.DecodeFile(cmd.AppPath+"conf/gopm.toml", &cmd.Config); err != nil {
fmt.Printf("initialize -> Fail to load configuration[ %s ]\n", err)
return false
12 years ago
}
12 years ago
// Set github.com access token.
12 years ago
doc.SetGithubCredentials(cmd.Config.Account.Github_Access_Token)
12 years ago
// Load usages by language.
12 years ago
if !loadUsage(cmd.Config.Lang) {
return false
}
// Create bundle and snapshot directories.
12 years ago
os.MkdirAll(cmd.AppPath+"repo/bundles/", os.ModePerm)
os.MkdirAll(cmd.AppPath+"repo/snapshots/", os.ModePerm)
// Create local tarball directories.
12 years ago
os.MkdirAll(cmd.AppPath+"repo/tarballs/", os.ModePerm)
// Initialize local data.
if !loadLocalNodes() || !loadLocalBundles() {
12 years ago
return false
}
return true
}
func main() {
// Initialization.
if !initialize() {
12 years ago
return
}
// Check length of arguments.
args := os.Args[1:]
12 years ago
if len(args) < 1 {
usage()
return
12 years ago
}
// Show help documentation.
12 years ago
if args[0] == "help" {
help(args[1:])
return
}
// Check commands and run.
12 years ago
for _, cmd := range commands {
if cmd.Name() == args[0] && cmd.Run != nil {
cmd.Run(cmd, args[1:])
12 years ago
exit()
return
}
}
// Uknown commands.
12 years ago
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", cmd.PromptMsg["UnknownCommand"]), args[0])
12 years ago
setExitStatus(2)
exit()
}
var exitStatus = 0
var exitMu sync.Mutex
func setExitStatus(n int) {
exitMu.Lock()
if exitStatus < n {
exitStatus = n
}
exitMu.Unlock()
}
12 years ago
var usageTemplate string
var helpTemplate = `{{if .Runnable}}usage: gopm {{.UsageLine}}
12 years ago
{{end}}{{.Long | trim}}
`
// tmpl executes the given template text on data, writing the result to w.
func tmpl(w io.Writer, text string, data interface{}) {
t := template.New("top")
t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
template.Must(t.Parse(text))
if err := t.Execute(w, data); err != nil {
panic(err)
}
}
func capitalize(s string) string {
if s == "" {
return s
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToTitle(r)) + s[n:]
}
func printUsage(w io.Writer) {
tmpl(w, usageTemplate, commands)
}
func usage() {
printUsage(os.Stderr)
os.Exit(2)
}
// help implements the 'help' command.
func help(args []string) {
if len(args) == 0 {
printUsage(os.Stdout)
// not exit 2: succeeded at 'gpm help'.
return
}
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "usage: gopm help command\n\nToo many arguments given.\n")
12 years ago
os.Exit(2) // failed at 'gpm help'
}
arg := args[0]
for _, cmd := range commands {
if cmd.Name() == arg {
tmpl(os.Stdout, helpTemplate, cmd)
// not exit 2: succeeded at 'go help cmd'.
return
}
}
fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'gopm help'.\n", arg)
12 years ago
os.Exit(2) // failed at 'go help cmd'
}
var atexitFuncs []func()
func atexit(f func()) {
atexitFuncs = append(atexitFuncs, f)
}
func exit() {
for _, f := range atexitFuncs {
f()
}
os.Exit(exitStatus)
}