2019-09-14 05:02:53 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-10-18 09:59:27 +03:00
|
|
|
"encoding/json"
|
2019-09-14 05:02:53 +03:00
|
|
|
"fmt"
|
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 12:59:09 +03:00
|
|
|
gist create [--public] [<FILES>...]
|
|
|
|
gist show [--json] <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-18 12:59:09 +03:00
|
|
|
* _create_:
|
|
|
|
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-18 12:59:09 +03:00
|
|
|
* _show_:
|
|
|
|
Print the contents of a gist. If the gist contains multiple files, the
|
|
|
|
operation will error out unless either <FILENAME> is specified or the
|
|
|
|
'--json' flag is used.
|
2019-09-14 05:02:53 +03:00
|
|
|
|
|
|
|
## Options:
|
2019-10-18 09:59:27 +03:00
|
|
|
|
2019-09-14 05:02:53 +03:00
|
|
|
--public
|
2019-10-18 12:59:09 +03:00
|
|
|
Make the new gist public (default: false).
|
2019-09-14 05:02:53 +03:00
|
|
|
|
2019-10-18 09:59:27 +03:00
|
|
|
--json
|
|
|
|
Print all files in the gist and emit them in JSON.
|
2019-09-14 05:02:53 +03:00
|
|
|
|
|
|
|
## Examples:
|
|
|
|
|
2019-10-18 12:59:09 +03:00
|
|
|
$ echo hello | hub gist create --public
|
2019-09-14 05:02:53 +03:00
|
|
|
|
2019-10-18 12:59:09 +03:00
|
|
|
$ hub gist create <file1> <file2>
|
2019-10-18 09:59:27 +03:00
|
|
|
|
2019-10-18 12:59:09 +03:00
|
|
|
# print a specific file within a gist:
|
|
|
|
$ hub gist show <ID> testfile1.txt
|
2019-10-18 09:59:27 +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
|
|
|
KnownFlags: "\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdShowGist = &Command{
|
|
|
|
Key: "show",
|
|
|
|
Run: showGist,
|
|
|
|
KnownFlags: "--json",
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdCreateGist = &Command{
|
|
|
|
Key: "create",
|
|
|
|
Run: createGist,
|
|
|
|
KnownFlags: "--public",
|
|
|
|
}
|
|
|
|
)
|
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 09:59:27 +03:00
|
|
|
func getGist(gh *github.Client, id string, filename string, emitJson bool) error {
|
2019-10-07 14:19:25 +03:00
|
|
|
gist, err := gh.FetchGist(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-18 09:59:27 +03:00
|
|
|
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 != "" {
|
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-18 09:59:27 +03:00
|
|
|
/*
|
|
|
|
* There's only one, but we don't know the name so a
|
|
|
|
* loop us fine.
|
|
|
|
*/
|
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 09:59:27 +03:00
|
|
|
g, err := gh.CreateGist(filenames, args.Flag.Bool("--public"))
|
|
|
|
utils.Check(err)
|
|
|
|
ui.Println(g.HtmlUrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func showGist(cmd *Command, args *Args) {
|
|
|
|
args.NoForward()
|
|
|
|
|
2019-10-18 13:16:28 +03:00
|
|
|
host, err := github.CurrentConfig().DefaultHostNoPrompt()
|
2019-10-18 09:59:27 +03:00
|
|
|
utils.Check(err)
|
|
|
|
gh := github.NewClient(host.Host)
|
|
|
|
|
|
|
|
id := args.GetParam(0)
|
|
|
|
filename := ""
|
|
|
|
if args.ParamsSize() > 1 {
|
|
|
|
filename = args.GetParam(1)
|
|
|
|
}
|
|
|
|
err = getGist(gh, id, filename, args.Flag.Bool("--json"))
|
|
|
|
utils.Check(err)
|
2019-09-14 05:02:53 +03:00
|
|
|
}
|