Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.

initial imlementation of list repos (#3) #13

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,11 @@ pipeline:
when:
event: [ tag ]

generate-coverage:
image: golang:1.12
pull: true
environment:
TAGS: bindata
commands:
- make coverage
when:
event: [ push, pull_request ]
branch: [ master ]

coverage:
image: robertstettner/drone-codecov
secrets: [ codecov_token ]
files:
- coverage.all
- coverage.out
when:
event: [ push, pull_request ]
branch: [ master ]
Expand Down
21 changes: 7 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ fmt:
vet:
$(GO) vet $(PACKAGES)

.PHONY: errcheck
errcheck:
@hash errcheck > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u github.com/kisielk/errcheck; \
fi
errcheck $(PACKAGES)

.PHONY: lint
lint:
@hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
Expand Down Expand Up @@ -112,13 +105,6 @@ fmt-check:
test:
GO111MODULE=on $(GO) test -mod=vendor -tags='sqlite sqlite_unlock_notify' $(PACKAGES)

.PHONY: coverage
coverage:
@hash gocovmerge > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u github.com/wadey/gocovmerge; \
fi
gocovmerge $(shell find . -type f -name "coverage.out") > coverage.all;\

.PHONY: unit-test-coverage
unit-test-coverage:
$(GO) test -tags='sqlite sqlite_unlock_notify' -cover -coverprofile coverage.out $(PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1
Expand Down Expand Up @@ -190,6 +176,13 @@ endif
release-copy:
cd $(DIST); for file in `find /build -type f -name "*"`; do cp $${file} ./release/; done;

.PHONY: release-compress
release-compress:
@hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u github.com/ulikunitz/xz/cmd/gxz; \
fi
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && gxz -k -9 $${file}; done;

.PHONY: release-check
release-check:
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "checksumming $${file}" && $(SHASUM) `echo $${file} | sed 's/^..//'` > $${file}.sha256; done;
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@ This project acts as a command line tool for operating one or multiple Gitea ins
the Gitea API implementation.

## Installation

Currently no prebuilt binaries are provided.
To install, a Go installation is needed.

```sh
go get code.gitea.io/tea
go install code.gitea.io/tea
```

If the `tea` executable is not found, you might need to set up your `$GOPATH` and `$PATH` variables first:

```sh
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
```

## Usage

First of all, you have to create a token on your `personal settings -> application` page of your gitea instance.
Use this token to login with `tea`:

```sh
tea login add --name=try --url=https://try.gitea.io --token=xxxxxx
```

Now you can use the `tea` commands:

```sh
tea issues
tea releases
Expand All @@ -34,6 +40,7 @@ tea releases
## Compilation

To compile the sources yourself run the following:

```sh
go get code.gitea.io/tea
cd "${GOPATH}/src/code.gitea.io/tea"
Expand Down
146 changes: 146 additions & 0 deletions cmd/repos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2019 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 cmd

import (
"fmt"
"log"

"code.gitea.io/sdk/gitea"

"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`,
Action: runReposList,
Subcommands: []cli.Command{
CmdReposList,
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login, optional when inside a gitea repository",
},
},
}

// 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,
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login, optional when inside a gitea repository",
},
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",
},
},
}

// runReposList list repositories
func runReposList(ctx *cli.Context) error {
login := initCommandLoginOnly(ctx)

mode := ctx.String("mode")
org := ctx.String("org")
user := ctx.String("user")

var rps []*gitea.Repository
var err error

if org != "" {
rps, err = login.Client().ListOrgRepos(org)
} else if user != "" {
rps, err = login.Client().ListUserRepos(user)
} else {
rps, err = login.Client().ListMyRepos()
}
if err != nil {
log.Fatal(err)
}

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
}

if len(rps) == 0 {
fmt.Println("No repositories found")
return nil
}

fmt.Println("Name | Type/Mode | SSH-URL | Owner")
for _, rp := range repos {
var mode = "source"
if rp.Fork {
mode = "fork"
}
if rp.Mirror {
mode = "mirror"
}
fmt.Printf("%s | %s | %s | %s\n", rp.FullName, mode, rp.SSHURL, rp.Owner.UserName)
}

return nil
}

func initCommandLoginOnly(ctx *cli.Context) *Login {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
}

var login *Login
if loginFlag := getGlobalFlag(ctx, "login"); loginFlag == "" {
login, err = getActiveLogin()
if err != nil {
log.Fatal(err)
}
} else {
login = getLoginByName(loginFlag)
if login == nil {
log.Fatal("indicated login name", loginFlag, "does not exist")
}
}
return login
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
cmd.CmdIssues,
cmd.CmdPulls,
cmd.CmdReleases,
cmd.CmdRepos,
}
err := app.Run(os.Args)
if err != nil {
Expand Down