1package commands 2 3import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 "github.com/github/hub/github" 9 "github.com/github/hub/ui" 10 "github.com/github/hub/utils" 11) 12 13var ( 14 cmdGist = &Command{ 15 Run: printGistHelp, 16 Usage: ` 17gist create [-oc] [--public] [<FILES>...] 18gist show <ID> [<FILENAME>] 19`, 20 Long: `Create and print GitHub Gists 21 22## Commands: 23 24 * _create_: 25 Create a new gist. If no <FILES> are specified, the content is read from 26 standard input. 27 28 * _show_: 29 Print the contents of a gist. If the gist contains multiple files, the 30 operation will error out unless <FILENAME> is specified. 31 32## Options: 33 34 --public 35 Make the new gist public (default: false). 36 37 -o, --browse 38 Open the new gist in a web browser. 39 40 -c, --copy 41 Put the URL of the new gist to clipboard instead of printing it. 42 43## Examples: 44 45 $ echo hello | hub gist create --public 46 47 $ hub gist create file1.txt file2.txt 48 49 # print a specific file within a gist: 50 $ hub gist show ID testfile1.txt 51 52## See also: 53 54hub(1), hub-api(1) 55`, 56 } 57 58 cmdShowGist = &Command{ 59 Key: "show", 60 Run: showGist, 61 } 62 63 cmdCreateGist = &Command{ 64 Key: "create", 65 Run: createGist, 66 KnownFlags: ` 67 --public 68 -o, --browse 69 -c, --copy 70`, 71 } 72) 73 74func init() { 75 cmdGist.Use(cmdShowGist) 76 cmdGist.Use(cmdCreateGist) 77 CmdRunner.Use(cmdGist) 78} 79 80func getGist(gh *github.Client, id string, filename string) error { 81 gist, err := gh.FetchGist(id) 82 if err != nil { 83 return err 84 } 85 86 if len(gist.Files) > 1 && filename == "" { 87 filenames := []string{} 88 for name := range gist.Files { 89 filenames = append(filenames, name) 90 } 91 sort.Strings(filenames) 92 return fmt.Errorf("This gist contains multiple files, you must specify one:\n %s", strings.Join(filenames, "\n ")) 93 } 94 95 if filename != "" { 96 if val, ok := gist.Files[filename]; ok { 97 ui.Println(val.Content) 98 } else { 99 return fmt.Errorf("no such file in gist") 100 } 101 } else { 102 for name := range gist.Files { 103 file := gist.Files[name] 104 ui.Println(file.Content) 105 } 106 } 107 return nil 108} 109 110func printGistHelp(command *Command, args *Args) { 111 utils.Check(command.UsageError("")) 112} 113 114func createGist(cmd *Command, args *Args) { 115 args.NoForward() 116 117 host, err := github.CurrentConfig().DefaultHostNoPrompt() 118 utils.Check(err) 119 gh := github.NewClient(host.Host) 120 121 filenames := []string{} 122 if args.IsParamsEmpty() { 123 filenames = append(filenames, "-") 124 } else { 125 filenames = args.Params 126 } 127 128 var gist *github.Gist 129 if args.Noop { 130 ui.Println("Would create gist") 131 gist = &github.Gist{ 132 HtmlUrl: fmt.Sprintf("https://gist.%s/%s", gh.Host.Host, "ID"), 133 } 134 } else { 135 gist, err = gh.CreateGist(filenames, args.Flag.Bool("--public")) 136 utils.Check(err) 137 } 138 139 flagIssueBrowse := args.Flag.Bool("--browse") 140 flagIssueCopy := args.Flag.Bool("--copy") 141 printBrowseOrCopy(args, gist.HtmlUrl, flagIssueBrowse, flagIssueCopy) 142} 143 144func showGist(cmd *Command, args *Args) { 145 args.NoForward() 146 147 if args.ParamsSize() < 1 { 148 utils.Check(cmd.UsageError("you must specify a gist ID")) 149 } 150 151 host, err := github.CurrentConfig().DefaultHostNoPrompt() 152 utils.Check(err) 153 gh := github.NewClient(host.Host) 154 155 id := args.GetParam(0) 156 filename := "" 157 if args.ParamsSize() > 1 { 158 filename = args.GetParam(1) 159 } 160 161 err = getGist(gh, id, filename) 162 utils.Check(err) 163} 164