mirror of https://github.com/gogits/gogs.git
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.
246 lines
5.5 KiB
246 lines
5.5 KiB
// 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 ( |
|
"flag" |
|
"fmt" |
|
"go/build" |
|
"io" |
|
"log" |
|
"os" |
|
"os/exec" |
|
"path/filepath" |
|
"runtime" |
|
"strings" |
|
"sync" |
|
"text/template" |
|
"unicode" |
|
"unicode/utf8" |
|
|
|
"github.com/BurntSushi/toml" |
|
) |
|
|
|
var ( |
|
config tomlConfig |
|
appPath string // Application path. |
|
) |
|
|
|
type tomlConfig struct { |
|
Title, Version string |
|
Lang string `toml:"user_language"` |
|
} |
|
|
|
// 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. |
|
Flag flag.FlagSet |
|
|
|
// CustomFlags indicates that the command will do its own |
|
// flag parsing. |
|
CustomFlags bool |
|
} |
|
|
|
// 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 'go help'. |
|
var commands = []*Command{ |
|
cmdBuild, |
|
} |
|
|
|
var exitStatus = 0 |
|
var exitMu sync.Mutex |
|
|
|
func setExitStatus(n int) { |
|
exitMu.Lock() |
|
if exitStatus < n { |
|
exitStatus = n |
|
} |
|
exitMu.Unlock() |
|
} |
|
|
|
func main() { |
|
// Get application path. |
|
appPath, _ := exec.LookPath("gpm") |
|
appPath = strings.Replace(filepath.Dir(appPath), "\\", "/", -1) + "/" |
|
|
|
// Load configuration. |
|
if _, err := toml.DecodeFile(appPath+"i18n/gpm.toml", &config); err != nil { |
|
fmt.Println(err) |
|
return |
|
} |
|
|
|
// Load usage template by language. |
|
f, err := os.Open(appPath + "i18n/usage_" + config.Lang + ".tpl") |
|
if err != nil { |
|
fmt.Println(err) |
|
return |
|
} |
|
defer f.Close() |
|
// Read usage. |
|
fi, _ := f.Stat() |
|
usageBytes := make([]byte, fi.Size()) |
|
f.Read(usageBytes) |
|
usageTemplate = string(usageBytes) |
|
|
|
// Initialization. |
|
flag.Usage = usage |
|
flag.Parse() |
|
log.SetFlags(0) |
|
|
|
args := flag.Args() |
|
if len(args) < 1 { |
|
usage() |
|
} |
|
|
|
if args[0] == "help" { |
|
help(args[1:]) |
|
return |
|
} |
|
|
|
// Diagnose common mistake: GOPATH==GOROOT. |
|
// This setting is equivalent to not setting GOPATH at all, |
|
// which is not what most people want when they do it. |
|
if gopath := os.Getenv("GOPATH"); gopath == runtime.GOROOT() { |
|
fmt.Fprintf(os.Stderr, "warning: GOPATH set to GOROOT (%s) has no effect\n", gopath) |
|
} else { |
|
for _, p := range filepath.SplitList(gopath) { |
|
// Note: using HasPrefix instead of Contains because a ~ can appear |
|
// in the middle of directory elements, such as /tmp/git-1.8.2~rc3 |
|
// or C:\PROGRA~1. Only ~ as a path prefix has meaning to the shell. |
|
if strings.HasPrefix(p, "~") { |
|
fmt.Fprintf(os.Stderr, "gpm: GOPATH entry cannot start with shell metacharacter '~': %q\n", p) |
|
os.Exit(2) |
|
} |
|
if build.IsLocalImport(p) { |
|
fmt.Fprintf(os.Stderr, "gpm: GOPATH entry is relative; must be absolute path: %q.\nRun 'gpm help gopath' for usage.\n", p) |
|
os.Exit(2) |
|
} |
|
} |
|
} |
|
|
|
for _, cmd := range commands { |
|
if cmd.Name() == args[0] && cmd.Run != nil { |
|
cmd.Flag.Usage = func() { cmd.Usage() } |
|
cmd.Run(cmd, args[1:]) |
|
exit() |
|
return |
|
} |
|
} |
|
|
|
fmt.Fprintf(os.Stderr, "gpm: unknown subcommand %q\nRun 'gpm help' for usage.\n", args[0]) |
|
setExitStatus(2) |
|
exit() |
|
} |
|
|
|
var usageTemplate string |
|
var helpTemplate = `{{if .Runnable}}usage: gpm {{.UsageLine}} |
|
|
|
{{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: gpm help command\n\nToo many arguments given.\n") |
|
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 'gpm help'.\n", arg) |
|
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) |
|
}
|
|
|