mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
tea repo list -R acme/service listed the account's repositories and tea repo create foo -R alice/project created alice/foo: the flag was silently ignored. repo list, repo create, repo migrate and search repo now fail with a usage error that suggests the intended command, such as "--repo is not used by repo list; did you mean: tea repo list acme". repo list takes the owner as an argument (--owner still works), and repo create accepts OWNER/NAME, creating in the organization OWNER. Signed-off-by: bircni <bircni@icloud.com>
126 lines
4.6 KiB
Go
126 lines
4.6 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package app
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"gitea.dev/tea/internal/clierr"
|
|
"gitea.dev/tea/internal/services"
|
|
"gitea.dev/tea/internal/settings"
|
|
"gitea.dev/tea/internal/terminal"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// repositoryArgument selects the repository from an optional positional owner/name or URL.
|
|
func (a *application) repositoryArgument(args []string) error {
|
|
if len(args) == 0 {
|
|
return nil
|
|
}
|
|
if a.options.repo != "" {
|
|
return clierr.Wrap(clierr.Usage, errors.New("supply the repository as an argument or --repo, not both"))
|
|
}
|
|
a.options.repo = args[0]
|
|
return nil
|
|
}
|
|
|
|
// rejectRepoFlag fails when --repo is passed to a command that acts on no repository; suggest
|
|
// words the command the user probably meant for the owner that --repo names.
|
|
func (a *application) rejectRepoFlag(cmd *cobra.Command, suggest func(owner string) string) error {
|
|
if a.options.repo == "" {
|
|
return nil
|
|
}
|
|
text := "--repo is not used by " + strings.TrimPrefix(cmd.CommandPath(), cmd.Root().Name()+" ")
|
|
if ref, err := settings.ParseRepository(a.options.repo, nil); err == nil && ref.Owner != "" {
|
|
text += "; did you mean: " + suggest(ref.Owner)
|
|
}
|
|
return clierr.Wrap(clierr.Usage, errors.New(text))
|
|
}
|
|
|
|
func (a *application) repoCommand() *cobra.Command {
|
|
group := &cobra.Command{Use: "repo", Short: "Work with Gitea repositories"}
|
|
var opts services.RepositoryListOptions
|
|
list := &cobra.Command{Use: "list [<owner>]", Short: "List your repositories", Args: usageArgs(cobra.MaximumNArgs(1)), RunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := a.rejectRepoFlag(cmd, func(owner string) string { return "tea repo list " + owner }); err != nil {
|
|
return err
|
|
}
|
|
if len(args) > 0 {
|
|
if cmd.Flags().Changed("owner") {
|
|
return clierr.Wrap(clierr.Usage, errors.New("supply the owner as an argument or --owner, not both"))
|
|
}
|
|
opts.Owner = args[0]
|
|
}
|
|
if err := validateRepositoryKind(opts.Kind); err != nil {
|
|
return err
|
|
}
|
|
scopes := 0
|
|
for _, selected := range []bool{opts.Owner != "", opts.Starred, opts.Watched} {
|
|
if selected {
|
|
scopes++
|
|
}
|
|
}
|
|
if scopes > 1 {
|
|
return clierr.Wrap(clierr.Usage, errors.New("--owner, --starred, and --watched select mutually exclusive collections"))
|
|
}
|
|
target, err := a.resolve(cmd.Context(), false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
items, more, err := (services.Repositories{Client: target.client}).List(cmd.Context(), opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := writeList(a, cmd, items, func(item services.Repository) []terminal.Cell {
|
|
return []terminal.Cell{{Text: item.FullName, Link: item.URL}}
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return a.morePagesHint(cmd, more, opts.Page)
|
|
}}
|
|
pageFlags(list, &opts.Limit, &opts.Page)
|
|
list.Flags().StringVar(&opts.Owner, "owner", "", "List repositories belonging to this user or organization")
|
|
list.Flags().StringVar(&opts.Kind, "type", "", "Repository kind: fork, source, mirror")
|
|
list.Flags().BoolVar(&opts.Starred, "starred", false, "List your starred repositories")
|
|
list.Flags().BoolVar(&opts.Watched, "watched", false, "List your watched repositories")
|
|
group.AddCommand(list, a.repositorySyncCommand())
|
|
var web bool
|
|
view := &cobra.Command{Use: "view [repository]", Short: "View the selected repository", Args: usageArgs(cobra.MaximumNArgs(1)), RunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := a.repositoryArgument(args); err != nil {
|
|
return err
|
|
}
|
|
target, err := a.resolve(cmd.Context(), true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
item, err := (services.Repositories{Client: target.client}).View(cmd.Context(), target.Repo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if web {
|
|
return a.openBrowser(cmd, item.URL)
|
|
}
|
|
return writeRecord(a, cmd, item, func(item services.Repository) terminal.Record {
|
|
record := repositoryRecord(item)
|
|
record.Lines = append([]string{terminal.SafeLine(item.Description)}, record.Lines...)
|
|
return record
|
|
})
|
|
}}
|
|
view.Flags().BoolVar(&web, "web", false, "Open the repository in a browser")
|
|
group.AddCommand(view)
|
|
group.AddCommand(&cobra.Command{Use: "set-default [repository]", Short: "Save the selected repository in local Git configuration", Annotations: textOutput("set-default configures Git"), Args: usageArgs(cobra.MaximumNArgs(1)), RunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := a.repositoryArgument(args); err != nil {
|
|
return err
|
|
}
|
|
target, err := a.resolve(cmd.Context(), true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = a.git(cmd.Context(), "config", "--local", "tea.repository", target.Host+"/"+target.Repo)
|
|
return err
|
|
}})
|
|
a.addRepositoryMutations(group)
|
|
return group
|
|
}
|