2020-09-30 01:11:33 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-07 21:40:02 -04:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-30 01:11:33 -04:00
|
|
|
|
|
|
|
package repos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/tea/cmd/flags"
|
2020-12-15 12:38:22 -05:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-09-30 01:11:33 -04:00
|
|
|
"code.gitea.io/tea/modules/print"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-09-05 13:11:17 -04:00
|
|
|
var repoFieldsFlag = flags.FieldsFlag(print.RepoFields, []string{
|
|
|
|
"owner", "name", "type", "ssh",
|
|
|
|
})
|
|
|
|
|
2020-10-06 04:05:22 -04:00
|
|
|
// CmdReposListFlags contains all flags needed for repo listing
|
|
|
|
var CmdReposListFlags = append([]cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "watched",
|
|
|
|
Aliases: []string{"w"},
|
|
|
|
Required: false,
|
|
|
|
Usage: "List your watched repos instead",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "starred",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Required: false,
|
|
|
|
Usage: "List your starred repos instead",
|
|
|
|
},
|
2021-09-05 13:11:17 -04:00
|
|
|
repoFieldsFlag,
|
2020-10-06 04:05:22 -04:00
|
|
|
&typeFilterFlag,
|
|
|
|
&flags.PaginationPageFlag,
|
|
|
|
&flags.PaginationLimitFlag,
|
|
|
|
}, flags.LoginOutputFlags...)
|
|
|
|
|
2020-09-30 01:11:33 -04:00
|
|
|
// CmdReposList represents a sub command of repos to list them
|
|
|
|
var CmdReposList = cli.Command{
|
2020-12-16 11:47:40 -05:00
|
|
|
Name: "list",
|
|
|
|
Aliases: []string{"ls"},
|
2020-10-06 04:05:22 -04:00
|
|
|
Usage: "List repositories you have access to",
|
|
|
|
Description: "List repositories you have access to",
|
2020-09-30 01:11:33 -04:00
|
|
|
Action: RunReposList,
|
2020-10-06 04:05:22 -04:00
|
|
|
Flags: CmdReposListFlags,
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunReposList list repositories
|
2020-12-15 12:38:22 -05:00
|
|
|
func RunReposList(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
client := ctx.Login.Client()
|
2020-09-30 01:11:33 -04:00
|
|
|
|
2020-12-15 12:38:22 -05:00
|
|
|
typeFilter, err := getTypeFilter(cmd)
|
2020-10-06 04:05:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rps []*gitea.Repository
|
|
|
|
if ctx.Bool("starred") {
|
|
|
|
user, _, err := client.GetMyUserInfo()
|
2020-09-30 01:11:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-06 04:05:22 -04:00
|
|
|
rps, _, err = client.SearchRepos(gitea.SearchRepoOptions{
|
2020-12-15 12:38:22 -05:00
|
|
|
ListOptions: ctx.GetListOptions(),
|
2020-10-06 04:05:22 -04:00
|
|
|
StarredByUserID: user.ID,
|
|
|
|
})
|
|
|
|
} else if ctx.Bool("watched") {
|
|
|
|
rps, _, err = client.GetMyWatchedRepos() // TODO: this does not expose pagination..
|
|
|
|
} else {
|
|
|
|
rps, _, err = client.ListMyRepos(gitea.ListReposOptions{
|
2020-12-15 12:38:22 -05:00
|
|
|
ListOptions: ctx.GetListOptions(),
|
2020-10-06 04:05:22 -04:00
|
|
|
})
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 04:05:22 -04:00
|
|
|
reposFiltered := rps
|
|
|
|
if typeFilter != gitea.RepoTypeNone {
|
|
|
|
reposFiltered = filterReposByType(rps, typeFilter)
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 13:11:17 -04:00
|
|
|
fields, err := repoFieldsFlag.GetValues(cmd)
|
2020-12-21 10:41:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
print.ReposList(reposFiltered, ctx.Output, fields)
|
2020-10-06 04:05:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-30 01:11:33 -04:00
|
|
|
|
2020-10-06 04:05:22 -04:00
|
|
|
func filterReposByType(repos []*gitea.Repository, t gitea.RepoType) []*gitea.Repository {
|
|
|
|
var filtered []*gitea.Repository
|
|
|
|
for _, r := range repos {
|
|
|
|
switch t {
|
|
|
|
case gitea.RepoTypeFork:
|
|
|
|
if !r.Fork {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case gitea.RepoTypeMirror:
|
|
|
|
if !r.Mirror {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case gitea.RepoTypeSource:
|
|
|
|
if r.Fork || r.Mirror {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
|
2020-10-06 04:05:22 -04:00
|
|
|
filtered = append(filtered, r)
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
2020-10-06 04:05:22 -04:00
|
|
|
return filtered
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|