2019-09-14 05:02:53 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-18 19:08:25 +03:00
|
|
|
"sort"
|
2019-10-18 13:29:30 +03:00
|
|
|
"strings"
|
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"
|
|
|
|
)
|
|
|
|
|
2019-10-18 09:59:27 +03:00
|
|
|
var (
|
|
|
|
cmdGist = &Command{
|
|
|
|
Run: printGistHelp,
|
|
|
|
Usage: `
|
2019-10-18 14:28:24 +03:00
|
|
|
gist create [-oc] [--public] [<FILES>...]
|
2019-10-18 13:29:30 +03:00
|
|
|
gist show <ID> [<FILENAME>]
|
2019-09-14 05:02:53 +03:00
|
|
|
`,
|
2019-10-18 12:59:09 +03:00
|
|
|
Long: `Create and print GitHub Gists
|
2019-09-14 05:02:53 +03:00
|
|
|
|
2019-10-18 09:59:27 +03:00
|
|
|
## Commands:
|
|
|
|
|
2019-10-21 23:22:09 +03:00
|
|
|
* _create_:
|
2019-10-18 12:59:09 +03:00
|
|
|
Create a new gist. If no <FILES> are specified, the content is read from
|
|
|
|
standard input.
|
2019-10-18 09:59:27 +03:00
|
|
|
|
2019-10-21 23:22:09 +03:00
|
|
|
* _show_:
|
2019-10-18 12:59:09 +03:00
|
|
|
Print the contents of a gist. If the gist contains multiple files, the
|
2019-10-18 13:29:30 +03:00
|
|
|
operation will error out unless <FILENAME> is specified.
|
2019-09-14 05:02:53 +03:00
|
|
|
|
|
|
|
## Options:
|
2019-10-18 09:59:27 +03:00
|
|
|
|
2019-10-21 23:22:09 +03:00
|
|
|
--public
|
|
|
|
Make the new gist public (default: false).
|
2019-09-14 05:02:53 +03:00
|
|
|
|
2019-10-21 23:22:09 +03:00
|
|
|
-o, --browse
|
2019-10-18 14:19:49 +03:00
|
|
|
Open the new gist in a web browser.
|
|
|
|
|
2019-10-21 23:22:09 +03:00
|
|
|
-c, --copy
|
2019-10-18 14:19:49 +03:00
|
|
|
Put the URL of the new gist to clipboard instead of printing it.
|
|
|
|
|
2019-09-14 05:02:53 +03:00
|
|
|
## Examples:
|
|
|
|
|
2019-10-18 19:08:25 +03:00
|
|
|
$ echo hello | hub gist create --public
|
2019-09-14 05:02:53 +03:00
|
|
|
|
2019-10-21 23:23:14 +03:00
|
|
|
$ hub gist create file1.txt file2.txt
|
2019-10-18 09:59:27 +03:00
|
|
|
|
2019-10-18 12:59:09 +03:00
|
|
|
# print a specific file within a gist:
|
2019-10-21 23:23:14 +03:00
|
|
|
$ hub gist show ID testfile1.txt
|
2019-10-18 19:08:25 +03:00
|
|
|
|
2019-09-14 05:02:53 +03:00
|
|
|
## See also:
|
|
|
|
|
|
|
|
hub(1), hub-api(1)
|
|
|
|
`,
|
2019-10-18 09:59:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmdShowGist = &Command{
|
2019-10-18 14:19:49 +03:00
|
|
|
Key: "show",
|
|
|
|
Run: showGist,
|
2019-10-18 09:59:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmdCreateGist = &Command{
|
2019-10-18 14:19:49 +03:00
|
|
|
Key: "create",
|
|
|
|
Run: createGist,
|
|
|
|
KnownFlags: `
|
|
|
|
--public
|
|
|
|
-o, --browse
|
|
|
|
-c, --copy
|
|
|
|
`,
|
2019-10-18 09:59:27 +03:00
|
|
|
}
|
|
|
|
)
|
2019-09-14 05:02:53 +03:00
|
|
|
|
|
|
|
func init() {
|
2019-10-18 09:59:27 +03:00
|
|
|
cmdGist.Use(cmdShowGist)
|
|
|
|
cmdGist.Use(cmdCreateGist)
|
2019-09-14 05:02:53 +03:00
|
|
|
CmdRunner.Use(cmdGist)
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:29:30 +03:00
|
|
|
func getGist(gh *github.Client, id string, filename string) error {
|
2019-10-07 14:19:25 +03:00
|
|
|
gist, err := gh.FetchGist(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:29:30 +03:00
|
|
|
if len(gist.Files) > 1 && filename == "" {
|
|
|
|
filenames := []string{}
|
|
|
|
for name := range gist.Files {
|
|
|
|
filenames = append(filenames, name)
|
|
|
|
}
|
2019-10-18 19:08:25 +03:00
|
|
|
sort.Strings(filenames)
|
|
|
|
return fmt.Errorf("This gist contains multiple files, you must specify one:\n %s", strings.Join(filenames, "\n "))
|
2019-10-18 09:59:27 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 13:29:30 +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 {
|
2019-10-07 14:19:25 +03:00
|
|
|
for name := range gist.Files {
|
|
|
|
file := gist.Files[name]
|
|
|
|
ui.Println(file.Content)
|
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
|
|
|
}
|
|
|
|
|
2019-10-18 09:59:27 +03:00
|
|
|
func printGistHelp(command *Command, args *Args) {
|
|
|
|
utils.Check(command.UsageError(""))
|
|
|
|
}
|
|
|
|
|
|
|
|
func createGist(cmd *Command, args *Args) {
|
2019-09-14 05:02:53 +03:00
|
|
|
args.NoForward()
|
|
|
|
|
2019-10-18 13:16:28 +03:00
|
|
|
host, err := github.CurrentConfig().DefaultHostNoPrompt()
|
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
|
|
|
|
2019-10-18 09:59:27 +03:00
|
|
|
filenames := []string{}
|
|
|
|
if args.IsParamsEmpty() {
|
|
|
|
filenames = append(filenames, "-")
|
2019-09-14 05:02:53 +03:00
|
|
|
} else {
|
2019-10-18 09:59:27 +03:00
|
|
|
filenames = args.Params
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|
2019-10-18 14:19:49 +03:00
|
|
|
|
|
|
|
var gist *github.Gist
|
|
|
|
if args.Noop {
|
|
|
|
ui.Println("Would create gist")
|
|
|
|
gist = &github.Gist{
|
2020-04-13 19:33:16 +03:00
|
|
|
HTMLURL: fmt.Sprintf("https://gist.%s/%s", gh.Host.Host, "ID"),
|
2019-10-18 14:19:49 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gist, err = gh.CreateGist(filenames, args.Flag.Bool("--public"))
|
|
|
|
utils.Check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
flagIssueBrowse := args.Flag.Bool("--browse")
|
|
|
|
flagIssueCopy := args.Flag.Bool("--copy")
|
2020-04-13 19:33:16 +03:00
|
|
|
printBrowseOrCopy(args, gist.HTMLURL, flagIssueBrowse, flagIssueCopy)
|
2019-10-18 09:59:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func showGist(cmd *Command, args *Args) {
|
|
|
|
args.NoForward()
|
2019-10-18 19:08:25 +03:00
|
|
|
|
2019-10-21 20:47:57 +03:00
|
|
|
if args.ParamsSize() < 1 {
|
|
|
|
utils.Check(cmd.UsageError("you must specify a gist ID"))
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:08:25 +03:00
|
|
|
host, err := github.CurrentConfig().DefaultHostNoPrompt()
|
|
|
|
utils.Check(err)
|
|
|
|
gh := github.NewClient(host.Host)
|
|
|
|
|
2019-10-18 09:59:27 +03:00
|
|
|
id := args.GetParam(0)
|
|
|
|
filename := ""
|
|
|
|
if args.ParamsSize() > 1 {
|
|
|
|
filename = args.GetParam(1)
|
|
|
|
}
|
2019-10-18 13:29:30 +03:00
|
|
|
|
|
|
|
err = getGist(gh, id, filename)
|
2019-10-18 09:59:27 +03:00
|
|
|
utils.Check(err)
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|