Initial, working implementation
This commit is contained in:
parent
b0cd1fb655
commit
e8475d9fb8
5 changed files with 164 additions and 8 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/MakeNowJust/heredoc"
|
||||
cmdCreate "github.com/cli/cli/v2/pkg/cmd/repo/autolink/create"
|
||||
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"
|
||||
)
|
||||
|
|
@ -24,6 +25,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))
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,13 +15,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var autolinkFields = []string{
|
||||
"id",
|
||||
"isAlphanumeric",
|
||||
"keyPrefix",
|
||||
"urlTemplate",
|
||||
}
|
||||
|
||||
type listOptions struct {
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
Browser browser.Browser
|
||||
|
|
@ -70,7 +63,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*listOptions) error) *cobra.Comman
|
|||
}
|
||||
|
||||
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "List autolink references in the web browser")
|
||||
cmdutil.AddJSONFlags(cmd, &opts.Exporter, autolinkFields)
|
||||
cmdutil.AddJSONFlags(cmd, &opts.Exporter, domain.AutolinkFields)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,13 @@ type Autolink struct {
|
|||
URLTemplate string `json:"url_template"`
|
||||
}
|
||||
|
||||
var AutolinkFields = []string{
|
||||
"id",
|
||||
"isAlphanumeric",
|
||||
"keyPrefix",
|
||||
"urlTemplate",
|
||||
}
|
||||
|
||||
func (a *Autolink) ExportData(fields []string) map[string]interface{} {
|
||||
return cmdutil.StructExportData(a, fields)
|
||||
}
|
||||
|
|
|
|||
47
pkg/cmd/repo/autolink/view/http.go
Normal file
47
pkg/cmd/repo/autolink/view/http.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package view
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/cli/cli/v2/api"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain"
|
||||
)
|
||||
|
||||
type AutolinkViewer struct {
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
func (a *AutolinkViewer) View(repo ghrepo.Interface, id string) (*domain.Autolink, 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.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := a.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return nil, fmt.Errorf("HTTP 404: Either no autolink with this ID exists for this repository or you are missing admin rights to the repository. (https://api.github.com/%s)", path)
|
||||
} else if resp.StatusCode > 299 {
|
||||
return nil, api.HandleHTTPError(resp)
|
||||
}
|
||||
|
||||
var autolink domain.Autolink
|
||||
err = json.NewDecoder(resp.Body).Decode(&autolink)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &autolink, nil
|
||||
}
|
||||
107
pkg/cmd/repo/autolink/view/view.go
Normal file
107
pkg/cmd/repo/autolink/view/view.go
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
package view
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/cli/cli/v2/internal/browser"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/pkg/cmd/repo/autolink/domain"
|
||||
"github.com/cli/cli/v2/pkg/cmdutil"
|
||||
"github.com/cli/cli/v2/pkg/iostreams"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type viewOptions struct {
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
Browser browser.Browser
|
||||
AutolinkClient AutolinkViewClient
|
||||
IO *iostreams.IOStreams
|
||||
Exporter cmdutil.Exporter
|
||||
|
||||
ID string
|
||||
}
|
||||
|
||||
type AutolinkViewClient interface {
|
||||
View(repo ghrepo.Interface, id string) (*domain.Autolink, error)
|
||||
}
|
||||
|
||||
func NewCmdView(f *cmdutil.Factory, runF func(*viewOptions) error) *cobra.Command {
|
||||
opts := &viewOptions{
|
||||
Browser: f.Browser,
|
||||
IO: f.IOStreams,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "view <id>",
|
||||
Short: "View an autolink reference",
|
||||
Long: heredoc.Docf(`
|
||||
View an autolink reference for a repository.
|
||||
|
||||
Information about autolinks is only available to repository administrators.
|
||||
`),
|
||||
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.ID = args[0]
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts.AutolinkClient = &AutolinkViewer{HTTPClient: httpClient}
|
||||
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
|
||||
return viewRun(opts)
|
||||
},
|
||||
}
|
||||
|
||||
cmdutil.AddJSONFlags(cmd, &opts.Exporter, domain.AutolinkFields)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func viewRun(opts *viewOptions) error {
|
||||
repo, err := opts.BaseRepo()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cs := opts.IO.ColorScheme()
|
||||
|
||||
autolink, err := opts.AutolinkClient.View(repo, opts.ID)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s %w", cs.Red("error viewing autolink:"), err)
|
||||
}
|
||||
|
||||
if opts.Exporter != nil {
|
||||
return opts.Exporter.Write(opts.IO, autolink)
|
||||
}
|
||||
|
||||
msg := heredoc.Docf(`
|
||||
Autolink in %s
|
||||
|
||||
ID: %d
|
||||
Key Prefix: %s
|
||||
URL Template: %s
|
||||
Alphanumeric: %t
|
||||
`,
|
||||
ghrepo.FullName(repo),
|
||||
autolink.ID,
|
||||
autolink.KeyPrefix,
|
||||
autolink.URLTemplate,
|
||||
autolink.IsAlphanumeric,
|
||||
)
|
||||
fmt.Fprint(opts.IO.Out, msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue