提交 194be2ae 编写于 作者: P Phil Dibowitz

code review bits

上级 9e7f3920
package commands
import (
"encoding/json"
"fmt"
"sort"
"github.com/github/hub/github"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
)
var cmdGist = &Command{
Run: gist,
Usage: `
gist <GistID> [--no-headers] [<filename>]
gist [--public] --file <file>
cat <file> | gist [--public]
var (
cmdGist = &Command{
Run: printGistHelp,
Usage: `
gist show <GistID> [--json] [<filename>]
gist create [--public] <file1> [<file2> ..]
cat <file> | gist create [--public]
`,
Long: `Create a GitHub Gist
Long: `Create a GitHub Gist
Doesn't take any options. With no arguments, it assumes a file on stdin. With an arguement, it tries to display that gist ID. If there is a second arguement it'll print that filename (if it exists) from the gist.
Can both create and retrieve gists. With no arguements, it takes a file on
stdin and creates a gist. With multiple '--file' arguments, will create a
mult-file gist.
If gistid is passed in, if there is only one file in the gist, it will be
printed, otherwise will error if a specific file is not requested. However, if
'--json' is used, then multiple files can be retreived.
## Commands:
* _show_:
Show the gist <GistID>. If the gist has more than one file in it,
either a file must be specified, or --json must be used. When --json
is specified, filenames are ignored.
* _create_:
Create a gist. If no files are specified, the file is content
is read from stdin. You may specify as many files as you want.
## Options:
--public
The gist should be marked as public.
--file <file>
The file to use. If not specified, or if the filename is "-", then
contents will be read from STDIN.
--no-headers
If there is more than one file in a gist, don't separate them by
headers, simply print them all out one after another.
--json
Print all files in the gist and emit them in JSON.
## Examples:
# Retrieve the contents of a gist with a single file
$ hub gist 87560fa4ebcc8683f68ec04d9100ab1c
$ hub gist show 87560fa4ebcc8683f68ec04d9100ab1c
this is test content in testfile.txt in test gist
# Retrieve the contents of a gist with multiple files
$ hub gist 6188fb16b1a7df0f51a51e03b3a2b4e8
GIST: test gist 2 (6188fb16b1a7df0f51a51e03b3a2b4e8)
==== BEGIN testfile1.txt ====>
test content in testfile1.txt inside of test gist 2
<=== END testfile1.txt =======
==== BEGIN testfile2.txt ====>
more test content in testfile2.txt inside of test gist 2
<=== END testfile2.txt =======
# Retrieve same gist, but specify a single file
$ hub gist 6188fb16b1a7df0f51a51e03b3a2b4e8 testfile1.txt
$ hub gist show 6188fb16b1a7df0f51a51e03b3a2b4e8 testfile1.txt
test content in testfile1.txt inside of test gist 2
# Retrieve same gist, with all files, but no headers
$ hub gist --no-headers 6188fb16b1a7df0f51a51e03b3a2b4e8
# Retrieve same gist, with all files, using JSON
$ hub gist show --json 6188fb16b1a7df0f51a51e03b3a2b4e8
test content in testfile1.txt inside of test gist 2
more test content in testfile2.txt inside of test gist 2
# Create a gist:
$ cat /tmp/testfile | hub gist
$ cat /tmp/testfile | hub gist create
https://gist.github.com/bdf551042f77bb8431b99f13c1105168
# Or a public one:
$ cat /tmp/testfile | hub gist --public
$ cat /tmp/testfile | hub gist create --public
https://gist.github.com/6c925133a295f0c5ad61eafcf05fee30
# You can also specify a file directly
$ hub gist --file /tmp/testfile
$ hub gist create /tmp/testfile
https://gist.github.com/bdf551042f77bb8431b99f13c1105168
# Or with multiple files
$ hub gist create /tmp/testfile /tmp/testfile2
https://gist.github.com/bdf551042f77bb8431b99f13c1105168
## See also:
hub(1), hub-api(1)
`,
}
KnownFlags: "\n",
}
cmdShowGist = &Command{
Key: "show",
Run: showGist,
KnownFlags: "--json",
}
cmdCreateGist = &Command{
Key: "create",
Run: createGist,
KnownFlags: "--public",
}
)
func init() {
cmdGist.Use(cmdShowGist)
cmdGist.Use(cmdCreateGist)
CmdRunner.Use(cmdGist)
}
func getGist(gh *github.Client, id string, filename string, includeHeaders bool) error {
func getGist(gh *github.Client, id string, filename string, emitJson bool) error {
gist, err := gh.FetchGist(id)
if err != nil {
return err
}
if filename != "" {
if len(gist.Files) > 1 && !emitJson && filename == "" {
return fmt.Errorf("There are multiple files, you must specify one, or use --json")
}
if emitJson {
data, err := json.Marshal(gist.Files)
if err != nil {
return err
}
fmt.Printf("%s\n", data)
} else if filename != "" {
if val, ok := gist.Files[filename]; ok {
ui.Println(val.Content)
} else {
return fmt.Errorf("no such file in gist")
}
} else {
includeHeaders := includeHeaders && len(gist.Files) > 1
if includeHeaders {
ui.Printf("GIST: %s (%s)\n\n", gist.Description, gist.Id)
}
filenames := []string{}
/*
* There's only one, but we don't know the name so a
* loop us fine.
*/
for name := range gist.Files {
filenames = append(filenames, name)
}
sort.Strings(filenames)
for _, name := range filenames {
file := gist.Files[name]
if includeHeaders {
ui.Printf("==== BEGIN %s ====>\n", name)
}
ui.Println(file.Content)
if includeHeaders {
ui.Printf("<=== END %s =======\n", name)
}
}
}
return nil
}
func gist(cmd *Command, args *Args) {
func printGistHelp(command *Command, args *Args) {
utils.Check(command.UsageError(""))
}
func createGist(cmd *Command, args *Args) {
args.NoForward()
host, err := github.CurrentConfig().DefaultHost()
utils.Check(err)
gh := github.NewClient(host.Host)
if !args.IsParamsEmpty() {
id := args.GetParam(0)
filename := ""
if args.ParamsSize() > 1 {
filename = args.GetParam(1)
}
getGist(gh, id, filename, !args.Flag.Bool("--no-headers"))
filenames := []string{}
if args.IsParamsEmpty() {
filenames = append(filenames, "-")
} else {
file := "-"
if args.Flag.HasReceived("--file") {
file = args.Flag.Value("--file")
}
g, err := gh.CreateGist(file, args.Flag.Bool("--public"))
utils.Check(err)
ui.Println(g.HtmlUrl)
filenames = args.Params
}
g, err := gh.CreateGist(filenames, args.Flag.Bool("--public"))
utils.Check(err)
ui.Println(g.HtmlUrl)
}
func showGist(cmd *Command, args *Args) {
args.NoForward()
host, err := github.CurrentConfig().DefaultHost()
utils.Check(err)
gh := github.NewClient(host.Host)
id := args.GetParam(0)
ui.Println("id is %s", id)
filename := ""
if args.ParamsSize() > 1 {
filename = args.GetParam(1)
}
err = getGist(gh, id, filename, args.Flag.Bool("--json"))
utils.Check(err)
}
......@@ -1008,27 +1008,31 @@ func (client *Client) FetchGist(id string) (gist *Gist, err error) {
return
}
func (client *Client) CreateGist(file string, public bool) (gist *Gist, err error) {
func (client *Client) CreateGist(filenames []string, public bool) (gist *Gist, err error) {
api, err := client.simpleApi()
if err != nil {
return
}
files := map[string]GistFile{}
var basename string
var content []byte
basename := "gistfile1.txt"
if file == "-" {
content, err = ioutil.ReadAll(os.Stdin)
} else {
content, err = ioutil.ReadFile(file)
basename = path.Base(file)
}
if err != nil {
return
var gf GistFile
for _, file := range filenames {
if file == "-" {
content, err = ioutil.ReadAll(os.Stdin)
basename = "gistfile1.txt"
} else {
content, err = ioutil.ReadFile(file)
basename = path.Base(file)
}
if err != nil {
return
}
gf = GistFile{Content: string(content)}
files[basename] = gf
}
gf := GistFile{Content: string(content)}
files := map[string]GistFile{}
files[basename] = gf
g := Gist{
Files: files,
Public: public,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册