hub/commands/gist.go

159 строки
3.1 KiB
Go
Исходник Обычный вид История

package commands
import (
"fmt"
"strings"
"github.com/github/hub/github"
"github.com/github/hub/ui"
"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 <ID> [<FILENAME>]
`,
2019-10-18 12:59:09 +03:00
Long: `Create and print GitHub Gists
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 <FILENAME> is specified.
## Options:
2019-10-18 09:59:27 +03:00
--public
2019-10-18 12:59:09 +03:00
Make the new gist public (default: false).
-o, --browse
Open the new gist in a web browser.
-c, --copy
Put the URL of the new gist to clipboard instead of printing it.
## Examples:
2019-10-18 12:59:09 +03:00
$ echo hello | hub gist create --public
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
## See also:
hub(1), hub-api(1)
`,
2019-10-18 09:59:27 +03:00
}
cmdShowGist = &Command{
Key: "show",
Run: showGist,
2019-10-18 09:59:27 +03:00
}
cmdCreateGist = &Command{
Key: "create",
Run: createGist,
KnownFlags: `
--public
-o, --browse
-c, --copy
`,
2019-10-18 09:59:27 +03:00
}
)
func init() {
2019-10-18 09:59:27 +03:00
cmdGist.Use(cmdShowGist)
cmdGist.Use(cmdCreateGist)
CmdRunner.Use(cmdGist)
}
func getGist(gh *github.Client, id string, filename string) error {
gist, err := gh.FetchGist(id)
if err != nil {
return err
}
if len(gist.Files) > 1 && filename == "" {
filenames := []string{}
for name := range gist.Files {
filenames = append(filenames, name)
}
return fmt.Errorf("the gist contains multiple files, you must specify one:\n%s", strings.Join(filenames, "\n"))
2019-10-18 09:59:27 +03:00
}
if filename != "" {
if val, ok := gist.Files[filename]; ok {
ui.Println(val.Content)
} else {
return fmt.Errorf("no such file in gist")
}
} else {
for name := range gist.Files {
file := gist.Files[name]
ui.Println(file.Content)
}
}
return nil
}
2019-10-18 09:59:27 +03:00
func printGistHelp(command *Command, args *Args) {
utils.Check(command.UsageError(""))
}
func createGist(cmd *Command, args *Args) {
args.NoForward()
host, err := github.CurrentConfig().DefaultHostNoPrompt()
utils.Check(err)
gh := github.NewClient(host.Host)
2019-10-18 09:59:27 +03:00
filenames := []string{}
if args.IsParamsEmpty() {
filenames = append(filenames, "-")
} else {
2019-10-18 09:59:27 +03:00
filenames = args.Params
}
var gist *github.Gist
if args.Noop {
ui.Println("Would create gist")
gist = &github.Gist{
HtmlUrl: fmt.Sprintf("https://gist.%s/%s", gh.Host.Host, "ID"),
}
} else {
gist, err = gh.CreateGist(filenames, args.Flag.Bool("--public"))
utils.Check(err)
}
flagIssueBrowse := args.Flag.Bool("--browse")
flagIssueCopy := args.Flag.Bool("--copy")
printBrowseOrCopy(args, gist.HtmlUrl, flagIssueBrowse, flagIssueCopy)
2019-10-18 09:59:27 +03:00
}
func showGist(cmd *Command, args *Args) {
args.NoForward()
if args.ParamsSize() < 1 {
utils.Check(cmd.UsageError("you must specify a gist ID"))
}
2019-10-18 09:59:27 +03:00
id := args.GetParam(0)
filename := ""
if args.ParamsSize() > 1 {
filename = args.GetParam(1)
}
host, err := github.CurrentConfig().DefaultHostNoPrompt()
utils.Check(err)
gh := github.NewClient(host.Host)
err = getGist(gh, id, filename)
2019-10-18 09:59:27 +03:00
utils.Check(err)
}