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.

454 lines
10 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"
"github.com/GPMGo/gopm/doc"
"github.com/GPMGo/gopm/utils"
12 years ago
)
var (
config tomlConfig
appPath string // Application path.
)
var (
localNodes []*doc.Node
localBundles []*doc.Bundle
12 years ago
)
// Use for i18n, key is prompt code, value is corresponding message.
var promptMsg map[string]string
12 years ago
type tomlConfig struct {
12 years ago
Title, Version string
Lang string `toml:"user_language"`
AutoBackup bool `toml:"auto_backup"`
12 years ago
Account account
AutoEnable flagEnable `toml:"auto_enable"`
}
type flagEnable struct {
Build, Install, Search, Check []string
12 years ago
}
type account struct {
Username, Password string
Github_Access_Token string `toml:"github_access_token"`
12 years ago
}
// A Command is an implementation of a go command
// like go build or go fix.
type Command struct {
// Run runs the command.
// The args are the arguments after the command name.
Run func(cmd *Command, args []string)
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the command name.
UsageLine string
// Short is the short description shown in the 'go help' output.
Short string
// Long is the long message shown in the 'go help <this-command>' output.
Long string
// Flag is a set of flags specific to this command.
Flags map[string]bool
12 years ago
}
// Name returns the command's name: the first word in the usage line.
func (c *Command) Name() string {
name := c.UsageLine
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}
func (c *Command) Usage() {
fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine)
fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(c.Long))
os.Exit(2)
}
// Runnable reports whether the command can be run; otherwise
// it is a documentation pseudo-command such as importpath.
func (c *Command) Runnable() bool {
return c.Run != nil
}
// Commands lists the available commands and help topics.
// The order here is the order in which they are printed by 'gpm help'.
var commands = []*Command{
cmdBuild,
cmdSearch,
cmdInstall,
cmdRemove,
cmdCheck,
}
12 years ago
// getAppPath returns application execute path for current process.
func getAppPath() bool {
// Look up executable in PATH variable.
appPath, _ = exec.LookPath(path.Base(os.Args[0]))
// Check if run under $GOPATH/bin
if !utils.IsExist(appPath + "conf/") {
paths := utils.GetGOPATH()
for _, p := range paths {
if utils.IsExist(p + "/src/github.com/GPMGo/gopm/") {
appPath = p + "/src/github.com/GPMGo/gopm/"
break
}
}
}
12 years ago
if len(appPath) == 0 {
fmt.Printf("ERROR: getAppPath -> Unable to indicate current execute path.\n")
return false
}
12 years ago
appPath = filepath.Dir(appPath) + "/"
if runtime.GOOS == "windows" {
// Replace all '\' to '/'.
appPath = strings.Replace(appPath, "\\", "/", -1)
12 years ago
}
doc.SetAppConfig(appPath, config.AutoBackup)
return true
12 years ago
}
// loadPromptMsg loads prompt messages according to user language.
func loadPromptMsg(lang string) bool {
promptMsg = make(map[string]string)
// Load prompt messages.
f, err := os.Open(appPath + "i18n/" + lang + "/prompt.txt")
if err != nil {
fmt.Printf("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 {
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.
f, err := os.Open(appPath + "i18n/" + lang + "/usage.tpl")
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR: loadUsage -> %s\n", 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.
for _, cmd := range commands {
f, err := os.Open(appPath + "i18n/" + lang + "/usage_" + cmd.Name() + ".txt")
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR: loadUsage -> %s\n", promptMsg["LoadCommandUsage"]), cmd.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 {
fmt.Printf(
fmt.Sprintf("ERROR: loadUsage -> %s\n", promptMsg["ReadCoammndUsage"]), cmd.Name())
return false
}
cmd.Short = usages[0]
cmd.Long = usages[1]
}
return true
}
12 years ago
// loadLocalNodes loads nodes information from local file system.
func loadLocalNodes() bool {
if !utils.IsExist(appPath + "data/nodes.json") {
os.MkdirAll(appPath+"data/", os.ModePerm)
} else {
fr, err := os.Open(appPath + "data/nodes.json")
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR: loadLocalNodes -> %s\n", promptMsg["LoadLocalData"]), err)
12 years ago
return false
}
defer fr.Close()
err = json.NewDecoder(fr).Decode(&localNodes)
if err != nil && err != io.EOF {
fmt.Printf(fmt.Sprintf("ERROR: loadLocalNodes -> %s\n", promptMsg["ParseJSON"]), err)
12 years ago
return false
}
}
return true
}
// loadLocalBundles loads bundles from local file system.
func loadLocalBundles() bool {
// Find all bundles.
dir, err := os.Open(appPath + "repo/bundles/")
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR: loadLocalBundles -> %s\n", promptMsg["OpenFile"]), err)
return false
}
defer dir.Close()
fis, err := dir.Readdir(0)
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR: loadLocalBundles -> %s\n", promptMsg["OpenFile"]), err)
return false
}
for _, fi := range fis {
// In case this folder contains unexpected directories.
if !fi.IsDir() && strings.HasSuffix(fi.Name(), ".json") {
fr, err := os.Open(appPath + "repo/bundles/" + fi.Name())
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR: loadLocalBundles -> %s\n", promptMsg["OpenFile"]), err)
return false
}
bundle := new(doc.Bundle)
err = json.NewDecoder(fr).Decode(bundle)
fr.Close()
if err != nil && err != io.EOF {
fmt.Printf(fmt.Sprintf("ERROR: loadLocalBundles -> %s\n", 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(), ".")]
}
localBundles = append(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.
if _, err := toml.DecodeFile(appPath+"conf/gopm.toml", &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.
doc.SetGithubCredentials(config.Account.Github_Access_Token)
// Load usages by language.
12 years ago
if !loadUsage(config.Lang) {
return false
}
// Create bundle and snapshot directories.
12 years ago
os.MkdirAll(appPath+"repo/bundles/", os.ModePerm)
os.MkdirAll(appPath+"repo/snapshots/", os.ModePerm)
// Create local tarball directories.
os.MkdirAll(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.
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", 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)
}
// executeCommand executes commands in command line.
func executeCommand(cmd string, args []string) {
cmdExec := exec.Command(cmd, args...)
stdout, err := cmdExec.StdoutPipe()
if err != nil {
fmt.Println(err)
}
stderr, err := cmdExec.StderrPipe()
if err != nil {
fmt.Println(err)
}
err = cmdExec.Start()
if err != nil {
fmt.Println(err)
}
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
cmdExec.Wait()
}