2020-09-30 01:11:33 -04: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 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",
|
|
|
|
},
|
2020-10-09 21:17:31 -04:00
|
|
|
&printFieldsFlag,
|
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{
|
|
|
|
Name: "ls",
|
2020-10-02 11:46:51 -04:00
|
|
|
Aliases: []string{"list"},
|
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
|
|
|
|
func RunReposList(ctx *cli.Context) error {
|
|
|
|
login := config.InitCommandLoginOnly(flags.GlobalLoginValue)
|
|
|
|
client := login.Client()
|
|
|
|
|
2020-10-06 04:05:22 -04: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 01:11:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-06 04:05:22 -04: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 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
|
|
|
}
|
|
|
|
|
2020-10-09 21:17:31 -04:00
|
|
|
print.ReposList(reposFiltered, getFields(ctx))
|
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
|
|
|
}
|