Skip to content

Commit

Permalink
Add option to remove file from gist
Browse files Browse the repository at this point in the history
  • Loading branch information
kousikmitra committed Jun 10, 2023
1 parent 63a4319 commit 13457c0
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
36 changes: 31 additions & 5 deletions pkg/cmd/gist/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
65 changes: 65 additions & 0 deletions pkg/cmd/gist/edit/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 13457c0

Please sign in to comment.