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"
|
|
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
"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
|
|
|
|
func RunReposList(ctx *cli.Context) error {
|
2020-12-11 09:07:29 +00:00
|
|
|
login, _, _ := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
|
2020-09-30 05:11:33 +00:00
|
|
|
client := login.Client()
|
|
|
|
|
2020-10-06 08:05:22 +00:00
|
|
|
typeFilter, err := getTypeFilter(ctx)
|
|
|
|
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{
|
|
|
|
ListOptions: flags.GetListOptions(ctx),
|
|
|
|
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{
|
|
|
|
ListOptions: flags.GetListOptions(ctx),
|
|
|
|
})
|
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-08 10:28:54 +00:00
|
|
|
print.ReposList(reposFiltered, flags.GlobalOutputValue, getFields(ctx))
|
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
|
|
|
}
|