diff --git a/cmd/repos.go b/cmd/repos.go index 08c0fa2..7b2f1e0 100644 --- a/cmd/repos.go +++ b/cmd/repos.go @@ -8,6 +8,8 @@ import ( "fmt" "log" + "code.gitea.io/sdk/gitea" + "github.com/urfave/cli" ) @@ -16,7 +18,11 @@ var CmdRepos = cli.Command{ Name: "repos", Usage: "Operate with repositories", Description: `Operate with repositories`, - Action: runRepos, + Action: runReposList, + Subcommands: []cli.Command{ + CmdReposList, + CmdReposFork, + }, Flags: []cli.Flag{ cli.StringFlag{ Name: "login, l", @@ -25,7 +31,15 @@ var CmdRepos = cli.Command{ }, } -func runRepos(ctx *cli.Context) error { +// 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, +} + +func runReposList(ctx *cli.Context) error { login := initCommandLoginOnly(ctx) rps, err := login.Client().ListMyRepos() @@ -54,6 +68,50 @@ func runRepos(ctx *cli.Context) error { return nil } +// CmdReposFork represents a sub command of issues to list issues +var CmdReposFork = cli.Command{ + Name: "fork", + Usage: "fork repository", + Description: `fork repository`, + Action: runReposFork, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "login, l", + Usage: "Indicate one login, optional when inside a gitea repository", + }, + cli.StringFlag{ + Name: "repo, r", + Usage: "Indicate one repo, optional when inside a gitea repository", + }, + cli.StringFlag{ + Name: "org", + Usage: "Organization to fork the repository for (optional, default = logged in user)", + }, + }, +} + +func runReposFork(ctx *cli.Context) error { + login, owner, repo := initCommand(ctx) + 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 +} + func initCommandLoginOnly(ctx *cli.Context) *Login { err := loadConfig(yamlConfigPath) if err != nil {