2020-09-30 05:11:33 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/tea/cmd/flags"
|
2020-12-15 17:38:22 +00:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-09-30 05:11:33 +00:00
|
|
|
"code.gitea.io/tea/modules/print"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2020-10-06 08:05:22 +00: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",
|
|
|
|
},
|
2020-10-10 01:17:31 +00:00
|
|
|
&printFieldsFlag,
|
2020-10-06 08:05:22 +00:00
|
|
|
&typeFilterFlag,
|
|
|
|
&flags.PaginationPageFlag,
|
|
|
|
&flags.PaginationLimitFlag,
|
|
|
|
}, flags.LoginOutputFlags...)
|
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
// CmdReposList represents a sub command of repos to list them
|
|
|
|
var CmdReposList = cli.Command{
|
|
|
|
Name: "ls",
|
2020-10-02 15:46:51 +00:00
|
|
|
Aliases: []string{"list"},
|
2020-10-06 08:05:22 +00:00
|
|
|
Usage: "List repositories you have access to",
|
|
|
|
Description: "List repositories you have access to",
|
2020-09-30 05:11:33 +00:00
|
|
|
Action: RunReposList,
|
2020-10-06 08:05:22 +00:00
|
|
|
Flags: CmdReposListFlags,
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunReposList list repositories
|
2020-12-15 17:38:22 +00:00
|
|
|
func RunReposList(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
client := ctx.Login.Client()
|
2020-09-30 05:11:33 +00:00
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
typeFilter, err := getTypeFilter(cmd)
|
2020-10-06 08:05:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rps []*gitea.Repository
|
|
|
|
if ctx.Bool("starred") {
|
|
|
|
user, _, err := client.GetMyUserInfo()
|
2020-09-30 05:11:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-06 08:05:22 +00:00
|
|
|
rps, _, err = client.SearchRepos(gitea.SearchRepoOptions{
|
2020-12-15 17:38:22 +00:00
|
|
|
ListOptions: ctx.GetListOptions(),
|
2020-10-06 08:05:22 +00: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 17:38:22 +00:00
|
|
|
ListOptions: ctx.GetListOptions(),
|
2020-10-06 08:05:22 +00:00
|
|
|
})
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 08:05:22 +00:00
|
|
|
reposFiltered := rps
|
|
|
|
if typeFilter != gitea.RepoTypeNone {
|
|
|
|
reposFiltered = filterReposByType(rps, typeFilter)
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
print.ReposList(reposFiltered, ctx.Output, getFields(cmd))
|
2020-10-06 08:05:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-30 05:11:33 +00:00
|
|
|
|
2020-10-06 08:05:22 +00: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 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 08:05:22 +00:00
|
|
|
filtered = append(filtered, r)
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
2020-10-06 08:05:22 +00:00
|
|
|
return filtered
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|