Initial working implementation
This commit is contained in:
parent
d10fbbfaea
commit
908513f97f
3 changed files with 141 additions and 0 deletions
|
|
@ -3,8 +3,10 @@ package autolink
|
|||
import (
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
cmdCreate "github.com/cli/cli/v2/pkg/cmd/repo/autolink/create"
|
||||
cmdDelete "github.com/cli/cli/v2/pkg/cmd/repo/autolink/delete"
|
||||
cmdList "github.com/cli/cli/v2/pkg/cmd/repo/autolink/list"
|
||||
cmdView "github.com/cli/cli/v2/pkg/cmd/repo/autolink/view"
|
||||
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
|
@ -26,6 +28,7 @@ func NewCmdAutolink(f *cmdutil.Factory) *cobra.Command {
|
|||
cmd.AddCommand(cmdList.NewCmdList(f, nil))
|
||||
cmd.AddCommand(cmdCreate.NewCmdCreate(f, nil))
|
||||
cmd.AddCommand(cmdView.NewCmdView(f, nil))
|
||||
cmd.AddCommand(cmdDelete.NewCmdDelete(f, nil))
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
101
pkg/cmd/repo/autolink/delete/delete.go
Normal file
101
pkg/cmd/repo/autolink/delete/delete.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package delete
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cli/cli/v2/internal/browser"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/internal/prompter"
|
||||
"github.com/cli/cli/v2/pkg/cmd/repo/autolink/view"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type deleteOptions struct {
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
Browser browser.Browser
|
||||
AutolinkDeleteClient AutolinkDeleteClient
|
||||
AutolinkViewClient view.AutolinkViewClient
|
||||
IO *iostreams.IOStreams
|
||||
|
||||
ID string
|
||||
Confirmed bool
|
||||
Prompter prompter.Prompter
|
||||
}
|
||||
|
||||
type AutolinkDeleteClient interface {
|
||||
Delete(repo ghrepo.Interface, id string) error
|
||||
}
|
||||
|
||||
func NewCmdDelete(f *cmdutil.Factory, runF func(*deleteOptions) error) *cobra.Command {
|
||||
opts := &deleteOptions{
|
||||
Browser: f.Browser,
|
||||
IO: f.IOStreams,
|
||||
Prompter: f.Prompter,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "delete <id>",
|
||||
Short: "Delete an autolink reference",
|
||||
Long: "Delete an autolink reference for a repository.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
||||
httpClient, err := f.HttpClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts.AutolinkDeleteClient = &AutolinkDeleter{HTTPClient: httpClient}
|
||||
opts.AutolinkViewClient = &view.AutolinkViewer{HTTPClient: httpClient}
|
||||
opts.ID = args[0]
|
||||
|
||||
opts.Confirmed = cmd.Flags().Changed("yes")
|
||||
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
|
||||
return deleteRun(opts)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&opts.Confirmed, "yes", false, "Confirm deletion without prompting")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func deleteRun(opts *deleteOptions) error {
|
||||
repo, err := opts.BaseRepo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out := opts.IO.Out
|
||||
cs := opts.IO.ColorScheme()
|
||||
|
||||
autolink, err := opts.AutolinkViewClient.View(repo, opts.ID)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s %w", cs.Red("error deleting autolink:"), err)
|
||||
}
|
||||
|
||||
if !opts.Confirmed {
|
||||
fmt.Fprintf(out, "Autolink %s has key prefix %s.", cs.Cyan(opts.ID), autolink.KeyPrefix)
|
||||
|
||||
if err := opts.Prompter.ConfirmDeletion(autolink.KeyPrefix); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = opts.AutolinkDeleteClient.Delete(repo, opts.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(out, "%s Autolink %s deleted from %s\n", cs.SuccessIcon(), cs.Cyan(opts.ID), cs.Bold(ghrepo.FullName(repo)))
|
||||
|
||||
return nil
|
||||
}
|
||||
37
pkg/cmd/repo/autolink/delete/http.go
Normal file
37
pkg/cmd/repo/autolink/delete/http.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package delete
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
)
|
||||
|
||||
type AutolinkDeleter struct {
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
func (a *AutolinkDeleter) Delete(repo ghrepo.Interface, id string) error {
|
||||
path := fmt.Sprintf("repos/%s/%s/autolinks/%s", repo.RepoOwner(), repo.RepoName(), id)
|
||||
url := ghinstance.RESTPrefix(repo.RepoHost()) + path
|
||||
req, err := http.NewRequest(http.MethodDelete, url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := a.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return fmt.Errorf("error deleting autolink: HTTP 404: Perhaps you are missing admin rights to the repository? (https://api.github.com/%s)", path)
|
||||
} else if resp.StatusCode > 299 {
|
||||
return api.HandleHTTPError(resp)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue