Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f7c4a0c
gh gist list
vilmibm Sep 11, 2020
b171241
start on gist view
vilmibm Sep 11, 2020
f3b4493
print aliases as valid yaml when piping
Sep 11, 2020
b6502de
allow naming stdin gist files
Sep 11, 2020
39b6ec8
share gist getting
Sep 11, 2020
0d3056d
gh gist edit
Sep 11, 2020
dc345a9
support --web for gist view
Sep 12, 2020
02d94d6
hmm
vilmibm Sep 12, 2020
269adab
improve list output
vilmibm Sep 14, 2020
1edff18
s/private/secret
vilmibm Sep 14, 2020
415c2ac
put gist in core commands
vilmibm Sep 14, 2020
2df6a6e
s/private/secret/
vilmibm Sep 14, 2020
e7ab1b7
linter appeasement
vilmibm Sep 14, 2020
ecfbaaa
linter appeasement
vilmibm Sep 14, 2020
190d76a
linter appeasement
vilmibm Sep 14, 2020
41382a5
better err
vilmibm Sep 14, 2020
b3266d9
linter appeasement
vilmibm Sep 14, 2020
00b4eab
include in readme
vilmibm Sep 14, 2020
1887fc0
working on list tests, need to debug
vilmibm Sep 15, 2020
0af61ff
Merge remote-tracking branch 'origin/trunk' into more-gists
vilmibm Sep 15, 2020
425f707
fix tests
vilmibm Sep 15, 2020
9fd87fa
wip tests
vilmibm Sep 15, 2020
ba5b639
finish list tests
Sep 15, 2020
a61c897
show filename if no description
Sep 15, 2020
f124b42
tweak gist view; support --filename
Sep 15, 2020
4a46786
linter appeasement
Sep 15, 2020
ada2c56
test gist view
Sep 15, 2020
62f54f0
start on edit tests
Sep 15, 2020
a9ab2a9
move Edit to opts for testing
Sep 15, 2020
15cf786
wip tests
Sep 15, 2020
0d45dd8
finish edit tests
Sep 15, 2020
2b70e82
better time stub
vilmibm Sep 16, 2020
7c986c0
help typo
vilmibm Sep 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ We'd love to hear your feedback about `gh`. If you spot bugs or have features th
- `gh pr [status, list, view, checkout, create]`
- `gh issue [status, list, view, create]`
- `gh repo [view, create, clone, fork]`
- `gh gist [create, list, view, edit]`
- `gh auth [login, logout, refresh, status]`
- `gh config [get, set]`
- `gh help`
Expand Down
7 changes: 7 additions & 0 deletions internal/ghinstance/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ func RESTPrefix(hostname string) string {
}
return "https://api.github.com/"
}

func GistPrefix(hostname string) string {
if IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/gist/", hostname)
}
return fmt.Sprintf("https://gist.%s/", hostname)
}
7 changes: 1 addition & 6 deletions pkg/cmd/alias/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ func listRun(opts *ListOptions) error {
sort.Strings(keys)

for _, alias := range keys {
if tp.IsTTY() {
// ensure that screen readers pause
tp.AddField(alias+":", nil, nil)
} else {
tp.AddField(alias, nil, nil)
}
tp.AddField(alias+":", nil, nil)
tp.AddField(aliasMap[alias], nil, nil)
tp.EndRow()
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/cmd/gist/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
type CreateOptions struct {
IO *iostreams.IOStreams

Description string
Public bool
Filenames []string
Description string
Public bool
Filenames []string
FilenameOverride string

HttpClient func() (*http.Client, error)
}
Expand Down Expand Up @@ -84,6 +85,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co

cmd.Flags().StringVarP(&opts.Description, "desc", "d", "", "A description for this gist")
cmd.Flags().BoolVarP(&opts.Public, "public", "p", false, "List the gist publicly (default: private)")
cmd.Flags().StringVarP(&opts.FilenameOverride, "filename", "f", "", "Provide a filename to be used when reading from STDIN")
return cmd
}

Expand All @@ -93,7 +95,7 @@ func createRun(opts *CreateOptions) error {
fileArgs = []string{"-"}
}

files, err := processFiles(opts.IO.In, fileArgs)
files, err := processFiles(opts.IO.In, opts.FilenameOverride, fileArgs)
if err != nil {
return fmt.Errorf("failed to collect files for posting: %w", err)
}
Expand Down Expand Up @@ -137,7 +139,7 @@ func createRun(opts *CreateOptions) error {
return nil
}

func processFiles(stdin io.ReadCloser, filenames []string) (map[string]string, error) {
func processFiles(stdin io.ReadCloser, filenameOverride string, filenames []string) (map[string]string, error) {
fs := map[string]string{}

if len(filenames) == 0 {
Expand All @@ -149,7 +151,11 @@ func processFiles(stdin io.ReadCloser, filenames []string) (map[string]string, e
var content []byte
var err error
if f == "-" {
filename = fmt.Sprintf("gistfile%d.txt", i)
if filenameOverride != "" {
filename = filenameOverride
} else {
filename = fmt.Sprintf("gistfile%d.txt", i)
}
content, err = ioutil.ReadAll(stdin)
if err != nil {
return fs, fmt.Errorf("failed to read from stdin: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gist/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (

func Test_processFiles(t *testing.T) {
fakeStdin := strings.NewReader("hey cool how is it going")
files, err := processFiles(ioutil.NopCloser(fakeStdin), []string{"-"})
files, err := processFiles(ioutil.NopCloser(fakeStdin), "", []string{"-"})
if err != nil {
t.Fatalf("unexpected error processing files: %s", err)
}
Expand Down
210 changes: 210 additions & 0 deletions pkg/cmd/gist/edit/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package edit

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"sort"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/ghinstance"
"github.com/cli/cli/pkg/cmd/gist/shared"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/pkg/prompt"
"github.com/cli/cli/pkg/surveyext"
"github.com/spf13/cobra"
)

type EditOptions struct {
IO *iostreams.IOStreams
HttpClient func() (*http.Client, error)
Config func() (config.Config, error)

Edit func(string, string, string, *iostreams.IOStreams) (string, error)

Selector string
Filename string
}

func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command {
opts := EditOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Config: f.Config,
Edit: func(editorCmd, filename, defaultContent string, io *iostreams.IOStreams) (string, error) {
return surveyext.Edit(
editorCmd,
"*."+filename,
defaultContent,
io.In, io.Out, io.ErrOut, nil)
},
}

cmd := &cobra.Command{
Use: "edit {<gist ID> | <gist URL>}",
Short: "Edit one of your gists",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
opts.Selector = args[0]

if runF != nil {
return runF(&opts)
}

return editRun(&opts)
},
}
cmd.Flags().StringVarP(&opts.Filename, "filename", "f", "", "a specific file to edit")

return cmd
}

func editRun(opts *EditOptions) error {
gistID := opts.Selector

u, err := url.Parse(opts.Selector)
if err == nil {
if strings.HasPrefix(u.Path, "/") {
gistID = u.Path[1:]
}
}

client, err := opts.HttpClient()
if err != nil {
return err
}

gist, err := shared.GetGist(client, ghinstance.OverridableDefault(), gistID)
if err != nil {
return err
}

filesToUpdate := map[string]string{}

for {
filename := opts.Filename
candidates := []string{}
for filename := range gist.Files {
candidates = append(candidates, filename)
}

sort.Strings(candidates)

if filename == "" {
if len(candidates) == 1 {
filename = candidates[0]
} else {
if !opts.IO.CanPrompt() {
return errors.New("unsure what file to edit; either specify --filename or run interactively")
}
err = prompt.SurveyAskOne(&survey.Select{
Message: "Edit which file?",
Options: candidates,
}, &filename)

if err != nil {
return fmt.Errorf("could not prompt: %w", err)
}
}
}

if _, ok := gist.Files[filename]; !ok {
return fmt.Errorf("gist has no file %q", filename)
}

editorCommand, err := cmdutil.DetermineEditor(opts.Config)
if err != nil {
return err
}
text, err := opts.Edit(editorCommand, filename, gist.Files[filename].Content, opts.IO)

if err != nil {
return err
}

if text != gist.Files[filename].Content {
gistFile := gist.Files[filename]
gistFile.Content = text // so it appears if they re-edit
filesToUpdate[filename] = text
}

if !opts.IO.CanPrompt() {
break
}

if len(candidates) == 1 {
break
}

choice := ""

err = prompt.SurveyAskOne(&survey.Select{
Message: "What next?",
Options: []string{
"Edit another file",
"Submit",
"Cancel",
},
}, &choice)

if err != nil {
return fmt.Errorf("could not prompt: %w", err)
}

stop := false

switch choice {
case "Edit another file":
continue
case "Submit":
stop = true
case "Cancel":
return cmdutil.SilentError
}

if stop {
break
}
}

err = updateGist(client, ghinstance.OverridableDefault(), gist)
if err != nil {
return err
}

return nil
}

func updateGist(client *http.Client, hostname string, gist *shared.Gist) error {
body := shared.Gist{
Description: gist.Description,
Files: gist.Files,
}

path := "gists/" + gist.ID

requestByte, err := json.Marshal(body)
if err != nil {
return err
}

requestBody := bytes.NewReader(requestByte)

result := shared.Gist{}

apiClient := api.NewClientFromHTTP(client)
err = apiClient.REST(hostname, "POST", path, requestBody, &result)

if err != nil {
return err
}

return nil
}
Loading