2019-09-14 05:02:53 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-07 14:19:25 +03:00
|
|
|
"sort"
|
2019-10-07 13:31:43 +03:00
|
|
|
|
2019-09-14 05:02:53 +03:00
|
|
|
"github.com/github/hub/github"
|
2019-10-07 13:31:43 +03:00
|
|
|
"github.com/github/hub/ui"
|
2019-09-14 05:02:53 +03:00
|
|
|
"github.com/github/hub/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdGist = &Command{
|
|
|
|
Run: gist,
|
|
|
|
Usage: `
|
|
|
|
gist <GistID> [--no-headers] [<filename>]
|
|
|
|
gist [--public] --file <file>
|
|
|
|
cat <file> | gist [--public]
|
|
|
|
`,
|
|
|
|
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.
|
|
|
|
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
## Examples:
|
|
|
|
|
|
|
|
# Retrieve the contents of a gist with a single file
|
|
|
|
$ hub gist 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
|
|
|
|
test content in testfile1.txt inside of test gist 2
|
|
|
|
|
|
|
|
# Retrieve same gist, with all files, but no headers
|
|
|
|
$ hub gist --no-headers 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
|
|
|
|
https://gist.github.com/bdf551042f77bb8431b99f13c1105168
|
|
|
|
|
|
|
|
# Or a public one:
|
|
|
|
$ cat /tmp/testfile | hub gist --public
|
|
|
|
https://gist.github.com/6c925133a295f0c5ad61eafcf05fee30
|
|
|
|
|
|
|
|
# You can also specify a file directly
|
|
|
|
$ hub gist --file /tmp/testfile
|
|
|
|
https://gist.github.com/bdf551042f77bb8431b99f13c1105168
|
|
|
|
|
|
|
|
## See also:
|
|
|
|
|
|
|
|
hub(1), hub-api(1)
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
CmdRunner.Use(cmdGist)
|
|
|
|
}
|
|
|
|
|
2019-10-07 14:19:25 +03:00
|
|
|
func getGist(gh *github.Client, id string, filename string, no_headers bool) error {
|
|
|
|
gist, err := gh.FetchGist(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-14 05:02:53 +03:00
|
|
|
if filename != "" {
|
2019-10-07 14:19:25 +03:00
|
|
|
if val, ok := gist.Files[filename]; ok {
|
|
|
|
ui.Println(val.Content)
|
2019-09-14 05:02:53 +03:00
|
|
|
} else {
|
2019-10-07 14:19:25 +03:00
|
|
|
return fmt.Errorf("no such file in gist")
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print_hdrs := len(gist.Files) != 1 && !no_headers
|
|
|
|
if print_hdrs {
|
2019-10-07 14:19:25 +03:00
|
|
|
ui.Printf("GIST: %s (%s)\n\n", gist.Description, gist.Id)
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|
2019-10-07 14:19:25 +03:00
|
|
|
|
|
|
|
filenames := []string{}
|
|
|
|
for name := range gist.Files {
|
|
|
|
filenames = append(filenames, name)
|
|
|
|
}
|
|
|
|
sort.Strings(filenames)
|
|
|
|
|
|
|
|
for _, name := range filenames {
|
|
|
|
file := gist.Files[name]
|
2019-09-14 05:02:53 +03:00
|
|
|
if print_hdrs {
|
2019-10-07 14:19:25 +03:00
|
|
|
ui.Printf("==== BEGIN %s ====>\n", name)
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|
2019-10-07 14:19:25 +03:00
|
|
|
ui.Println(file.Content)
|
2019-09-14 05:02:53 +03:00
|
|
|
if print_hdrs {
|
2019-10-07 14:19:25 +03:00
|
|
|
ui.Printf("<=== END %s =======\n", name)
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 14:19:25 +03:00
|
|
|
return nil
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func gist(cmd *Command, args *Args) {
|
|
|
|
args.NoForward()
|
|
|
|
|
2019-10-07 13:49:09 +03:00
|
|
|
host, err := github.CurrentConfig().DefaultHost()
|
2019-09-14 05:02:53 +03:00
|
|
|
utils.Check(err)
|
2019-10-07 13:49:09 +03:00
|
|
|
gh := github.NewClient(host.Host)
|
2019-09-14 05:02:53 +03:00
|
|
|
|
|
|
|
if !args.IsParamsEmpty() {
|
|
|
|
id := args.GetParam(0)
|
|
|
|
filename := ""
|
|
|
|
if args.ParamsSize() > 1 {
|
|
|
|
filename = args.GetParam(1)
|
|
|
|
}
|
2019-10-07 13:31:43 +03:00
|
|
|
getGist(gh, id, filename, args.Flag.Bool("--no-headers"))
|
2019-09-14 05:02:53 +03:00
|
|
|
} else {
|
|
|
|
file := "-"
|
|
|
|
if args.Flag.HasReceived("--file") {
|
|
|
|
file = args.Flag.Value("--file")
|
|
|
|
}
|
2019-10-07 13:31:43 +03:00
|
|
|
g, err := gh.CreateGist(file, args.Flag.Bool("--public"))
|
|
|
|
utils.Check(err)
|
|
|
|
ui.Println(g.HtmlUrl)
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|
|
|
|
}
|