2019-04-15 22:58:32 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2019-04-15 22:40:27 +02:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2019-05-06 18:21:41 +02:00
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
2019-04-15 22:40:27 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdRepos represents to login a gitea server.
|
|
|
|
var CmdRepos = cli.Command{
|
|
|
|
Name: "repos",
|
|
|
|
Usage: "Operate with repositories",
|
|
|
|
Description: `Operate with repositories`,
|
2019-05-06 18:21:41 +02:00
|
|
|
Action: runReposList,
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
CmdReposList,
|
|
|
|
CmdReposFork,
|
|
|
|
},
|
2019-05-09 09:16:49 +02:00
|
|
|
Flags: append([]cli.Flag{}, DefaultFlags...),
|
2019-04-15 22:40:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 18:21:41 +02:00
|
|
|
// CmdReposList represents a sub command of issues to list issues
|
|
|
|
var CmdReposList = cli.Command{
|
|
|
|
Name: "ls",
|
|
|
|
Usage: "List available repositories",
|
|
|
|
Description: `List available repositories`,
|
|
|
|
Action: runReposList,
|
2019-05-09 09:16:49 +02:00
|
|
|
Flags: append([]cli.Flag{
|
2019-05-06 18:56:14 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "mode",
|
|
|
|
Usage: "Indicate one login, optional when inside a gitea repository",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "org",
|
|
|
|
Usage: "Indicate one login, optional when inside a gitea repository",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "user",
|
|
|
|
Usage: "Indicate one login, optional when inside a gitea repository",
|
|
|
|
},
|
2019-05-09 09:16:49 +02:00
|
|
|
}, DefaultFlags...),
|
2019-05-06 18:21:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 18:56:14 +02:00
|
|
|
// runReposList list repositories
|
2019-05-06 18:21:41 +02:00
|
|
|
func runReposList(ctx *cli.Context) error {
|
2019-05-09 09:16:49 +02:00
|
|
|
login := initCommandLoginOnly()
|
2019-04-15 22:40:27 +02:00
|
|
|
|
2019-05-06 18:56:14 +02:00
|
|
|
mode := ctx.String("mode")
|
|
|
|
org := ctx.String("org")
|
|
|
|
user := ctx.String("user")
|
2019-04-15 22:40:27 +02:00
|
|
|
|
2019-05-06 18:56:14 +02:00
|
|
|
var rps []*gitea.Repository
|
|
|
|
var err error
|
2019-04-15 22:40:27 +02:00
|
|
|
|
2019-05-06 18:56:14 +02:00
|
|
|
if org != "" {
|
|
|
|
rps, err = login.Client().ListOrgRepos(org)
|
|
|
|
} else if user != "" {
|
|
|
|
rps, err = login.Client().ListUserRepos(user)
|
|
|
|
} else {
|
|
|
|
rps, err = login.Client().ListMyRepos()
|
|
|
|
}
|
2019-04-15 22:40:27 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-05-06 18:56:14 +02:00
|
|
|
var repos []*gitea.Repository
|
|
|
|
if mode == "" {
|
|
|
|
repos = rps
|
|
|
|
} else if mode == "fork" {
|
|
|
|
for _, rp := range rps {
|
|
|
|
if rp.Fork == true {
|
|
|
|
repos = append(repos, rp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if mode == "mirror" {
|
|
|
|
for _, rp := range rps {
|
|
|
|
if rp.Mirror == true {
|
|
|
|
repos = append(repos, rp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if mode == "source" {
|
|
|
|
for _, rp := range rps {
|
|
|
|
if rp.Mirror != true && rp.Fork != true {
|
|
|
|
repos = append(repos, rp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Unknown mode '%s'\nUse one of the following:\n- fork\n- mirror\n- source\n", mode)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:16:49 +02:00
|
|
|
headers := []string{
|
|
|
|
string("Name"),
|
|
|
|
string("Type/Mode"),
|
|
|
|
string("SSH-URL"),
|
|
|
|
string("Owner"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2019-04-15 22:40:27 +02:00
|
|
|
if len(rps) == 0 {
|
2019-05-09 09:16:49 +02:00
|
|
|
Output(outputValue, headers, values)
|
2019-04-15 22:40:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-06 18:56:14 +02:00
|
|
|
for _, rp := range repos {
|
2019-04-15 22:40:27 +02:00
|
|
|
var mode = "source"
|
|
|
|
if rp.Fork {
|
|
|
|
mode = "fork"
|
|
|
|
}
|
|
|
|
if rp.Mirror {
|
|
|
|
mode = "mirror"
|
|
|
|
}
|
2019-05-09 09:16:49 +02:00
|
|
|
values = append(
|
|
|
|
values,
|
|
|
|
[]string{
|
|
|
|
rp.FullName,
|
|
|
|
mode,
|
|
|
|
rp.SSHURL,
|
|
|
|
rp.Owner.UserName,
|
|
|
|
},
|
|
|
|
)
|
2019-04-15 22:40:27 +02:00
|
|
|
}
|
2019-05-09 09:16:49 +02:00
|
|
|
Output(outputValue, headers, values)
|
2019-04-15 22:40:27 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-06 18:21:41 +02:00
|
|
|
// CmdReposFork represents a sub command of issues to list issues
|
|
|
|
var CmdReposFork = cli.Command{
|
|
|
|
Name: "fork",
|
|
|
|
Usage: "fork repository",
|
|
|
|
Description: `fork repository`,
|
|
|
|
Action: runReposFork,
|
2019-05-09 09:16:49 +02:00
|
|
|
Flags: append([]cli.Flag{
|
2019-05-06 18:21:41 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "org",
|
|
|
|
Usage: "Organization to fork the repository for (optional, default = logged in user)",
|
|
|
|
},
|
2019-05-09 09:16:49 +02:00
|
|
|
}, RepoDefaultFlags...),
|
2019-05-06 18:21:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runReposFork(ctx *cli.Context) error {
|
2019-05-09 09:16:49 +02:00
|
|
|
login, owner, repo := initCommand()
|
2019-05-06 18:21:41 +02:00
|
|
|
forkOptions := gitea.CreateForkOption{}
|
|
|
|
if org := ctx.String("org"); org != "" {
|
|
|
|
forkOptions = gitea.CreateForkOption{
|
|
|
|
Organization: &org,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := login.Client().CreateFork(owner, repo, forkOptions)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, _ := login.Client().GetMyUserInfo()
|
|
|
|
|
|
|
|
fmt.Printf("Forked '%s/%s' to '%s/%s'\n", owner, repo, user.UserName, repo)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:16:49 +02:00
|
|
|
func initCommandLoginOnly() *Login {
|
2019-04-15 22:40:27 +02:00
|
|
|
err := loadConfig(yamlConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("load config file failed", yamlConfigPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
var login *Login
|
2019-05-09 09:16:49 +02:00
|
|
|
if loginValue == "" {
|
2019-04-15 22:40:27 +02:00
|
|
|
login, err = getActiveLogin()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-09 09:16:49 +02:00
|
|
|
login = getLoginByName(loginValue)
|
2019-04-15 22:40:27 +02:00
|
|
|
if login == nil {
|
2019-05-09 09:16:49 +02:00
|
|
|
log.Fatal("indicated login name ", loginValue, " does not exist")
|
2019-04-15 22:40:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return login
|
|
|
|
}
|