-
Couldn't load subscription status.
- Fork 7.3k
bunch of gist stuff #1699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
bunch of gist stuff #1699
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
f7c4a0c
gh gist list
vilmibm b171241
start on gist view
vilmibm f3b4493
print aliases as valid yaml when piping
b6502de
allow naming stdin gist files
39b6ec8
share gist getting
0d3056d
gh gist edit
dc345a9
support --web for gist view
02d94d6
hmm
vilmibm 269adab
improve list output
vilmibm 1edff18
s/private/secret
vilmibm 415c2ac
put gist in core commands
vilmibm 2df6a6e
s/private/secret/
vilmibm e7ab1b7
linter appeasement
vilmibm ecfbaaa
linter appeasement
vilmibm 190d76a
linter appeasement
vilmibm 41382a5
better err
vilmibm b3266d9
linter appeasement
vilmibm 00b4eab
include in readme
vilmibm 1887fc0
working on list tests, need to debug
vilmibm 0af61ff
Merge remote-tracking branch 'origin/trunk' into more-gists
vilmibm 425f707
fix tests
vilmibm 9fd87fa
wip tests
vilmibm ba5b639
finish list tests
a61c897
show filename if no description
f124b42
tweak gist view; support --filename
4a46786
linter appeasement
ada2c56
test gist view
62f54f0
start on edit tests
a9ab2a9
move Edit to opts for testing
15cf786
wip tests
0d45dd8
finish edit tests
2b70e82
better time stub
vilmibm 7c986c0
help typo
vilmibm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.