From 13457c01266f8c39223dc86661dca227e2eff9a0 Mon Sep 17 00:00:00 2001 From: Kousik Mitra Date: Sat, 10 Jun 2023 16:13:27 +0530 Subject: [PATCH] Add option to remove file from gist --- pkg/cmd/gist/edit/edit.go | 36 ++++++++++++++++--- pkg/cmd/gist/edit/edit_test.go | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/gist/edit/edit.go b/pkg/cmd/gist/edit/edit.go index 3ad28b6c459..a9492ec206a 100644 --- a/pkg/cmd/gist/edit/edit.go +++ b/pkg/cmd/gist/edit/edit.go @@ -32,11 +32,12 @@ type EditOptions struct { Edit func(string, string, string, *iostreams.IOStreams) (string, error) - Selector string - EditFilename string - AddFilename string - SourceFile string - Description string + Selector string + EditFilename string + AddFilename string + RemoveFilename string + SourceFile string + Description string } func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command { @@ -82,6 +83,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman cmd.Flags().StringVarP(&opts.AddFilename, "add", "a", "", "Add a new file to the gist") cmd.Flags().StringVarP(&opts.Description, "desc", "d", "", "New description for the gist") cmd.Flags().StringVarP(&opts.EditFilename, "filename", "f", "", "Select a file to edit") + cmd.Flags().StringVarP(&opts.RemoveFilename, "remove-file", "r", "", "Remove a file from the gist") return cmd } @@ -187,6 +189,17 @@ func editRun(opts *EditOptions) error { return updateGist(apiClient, host, gist) } + // Remove a file from the gist + if opts.RemoveFilename != "" { + files, err := removeFile(gist.Files, opts.RemoveFilename) + if err != nil { + return err + } + + gist.Files = files + return updateGist(apiClient, host, gist) + } + filesToUpdate := map[string]string{} for { @@ -337,3 +350,16 @@ func getFilesToAdd(file string, content []byte) (map[string]*shared.GistFile, er }, }, nil } + +func removeFile(files map[string]*shared.GistFile, filename string) (map[string]*shared.GistFile, error) { + if _, found := files[filename]; !found { + return nil, fmt.Errorf("gist has no file %q", filename) + } + + for name := range files { + if strings.Compare(name, filename) == 0 { + files[name] = nil + } + } + return files, nil +} diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index 5e52a8ffb1e..47683ad5799 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -80,6 +80,14 @@ func TestNewCmdEdit(t *testing.T) { Description: "my new description", }, }, + { + name: "remove", + cli: "123 --remove-file cool.md", + wants: EditOptions{ + Selector: "123", + RemoveFilename: "cool.md", + }, + }, } for _, tt := range tests { @@ -394,6 +402,63 @@ func Test_editRun(t *testing.T) { }, }, }, + { + name: "remove file, file does not exist", + gist: &shared.Gist{ + ID: "1234", + Files: map[string]*shared.GistFile{ + "sample.txt": { + Filename: "sample.txt", + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + }, + Owner: &shared.GistOwner{Login: "octocat"}, + }, + opts: &EditOptions{ + RemoveFilename: "sample2.txt", + }, + wantErr: "gist has no file \"sample2.txt\"", + }, + { + name: "remove file from existing gist", + gist: &shared.Gist{ + ID: "1234", + Files: map[string]*shared.GistFile{ + "sample.txt": { + Filename: "sample.txt", + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + "sample2.txt": { + Filename: "sample2.txt", + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + }, + Owner: &shared.GistOwner{Login: "octocat"}, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.REST("POST", "gists/1234"), + httpmock.StatusStringResponse(201, "{}")) + }, + opts: &EditOptions{ + RemoveFilename: "sample2.txt", + }, + wantParams: map[string]interface{}{ + "description": "", + "updated_at": "0001-01-01T00:00:00Z", + "public": false, + "files": map[string]interface{}{ + "sample.txt": map[string]interface{}{ + "filename": "sample.txt", + "content": "bwhiizzzbwhuiiizzzz", + "type": "text/plain", + }, + "sample2.txt": nil, + }, + }, + }, { name: "edit gist using file from source parameter", gist: &shared.Gist{