init project

This commit is contained in:
Lunny Xiao
2018-09-03 14:43:00 +08:00
commit 9d5cda4bfe
15 changed files with 1300 additions and 0 deletions

212
cmd/config.go Normal file
View File

@ -0,0 +1,212 @@
// Copyright 2018 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 (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path/filepath"
"strings"
"code.gitea.io/git"
"code.gitea.io/sdk/gitea"
local_git "code.gitea.io/tea/modules/git"
"code.gitea.io/tea/modules/utils"
"github.com/go-gitea/yaml"
)
type Login struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Token string `yaml:"token"`
Active bool `yaml:"active"`
SSHHost string `yaml:"ssh_host"`
Insecure bool `yaml:"insecure"`
}
func (l *Login) Client() *gitea.Client {
client := gitea.NewClient(l.URL, l.Token)
if l.Insecure {
cookieJar, _ := cookiejar.New(nil)
client.SetHTTPClient(&http.Client{
Jar: cookieJar,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
})
}
return client
}
func (l *Login) GetSSHHost() string {
if l.SSHHost != "" {
return l.SSHHost
}
u, err := url.Parse(l.URL)
if err != nil {
return ""
}
return u.Hostname()
}
type Config struct {
Logins []Login `yaml:"logins"`
}
var (
config Config
yamlConfigPath string
)
func init() {
homeDir, err := utils.Home()
if err != nil {
log.Fatal("Retrieve home dir failed")
}
dir := filepath.Join(homeDir, ".tea")
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
log.Fatal("Init tea config dir", dir, "failed")
}
yamlConfigPath = filepath.Join(dir, "tea.yml")
}
func splitRepo(repoPath string) (string, string) {
p := strings.Split(repoPath, "/")
if len(p) >= 2 {
return p[0], p[1]
}
return repoPath, ""
}
func getActiveLogin() (*Login, error) {
if len(config.Logins) == 0 {
return nil, errors.New("No available login")
}
for _, l := range config.Logins {
if l.Active {
return &l, nil
}
}
return &config.Logins[0], nil
}
func getLoginByName(name string) *Login {
for _, l := range config.Logins {
if l.Name == name {
return &l
}
}
return nil
}
func addLogin(login Login) error {
for _, l := range config.Logins {
if l.Name == login.Name {
if l.URL == login.URL && l.Token == login.Token {
return nil
}
return errors.New("login name has already been used")
}
if l.URL == login.URL && l.Token == login.Token {
return errors.New("URL has been added")
}
}
u, err := url.Parse(login.URL)
if err != nil {
return err
}
if login.SSHHost == "" {
login.SSHHost = u.Hostname()
}
config.Logins = append(config.Logins, login)
return nil
}
func isFileExist(fileName string) (bool, error) {
f, err := os.Stat(fileName)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if f.IsDir() {
return false, errors.New("the same name directory exist")
}
return true, nil
}
func loadConfig(ymlPath string) error {
exist, _ := isFileExist(ymlPath)
if exist {
Println("Found config file", ymlPath)
bs, err := ioutil.ReadFile(ymlPath)
if err != nil {
return err
}
err = yaml.Unmarshal(bs, &config)
if err != nil {
return err
}
}
return nil
}
func saveConfig(ymlPath string) error {
bs, err := yaml.Marshal(&config)
if err != nil {
return err
}
return ioutil.WriteFile(ymlPath, bs, 0660)
}
func curGitRepoPath() (*Login, string, error) {
cmd := git.NewCommand("remote", "get-url", "origin")
u, err := cmd.RunInDir(filepath.Dir(os.Args[0]))
if err != nil || len(u) == 0 {
return nil, "", errors.New("You have to indicated a repo or execute the command in a repo")
}
p, err := local_git.ParseURL(strings.TrimSpace(u))
if err != nil {
return nil, "", fmt.Errorf("Git remote URL parse failed: %s", err.Error())
}
for _, l := range config.Logins {
if p.Scheme == "http" || p.Scheme == "https" {
if strings.HasPrefix(u, l.URL) {
ps := strings.Split(p.Path, "/")
path := strings.Join(ps[len(ps)-2:], "/")
return &l, strings.TrimSuffix(path, ".git"), nil
}
} else if p.Scheme == "ssh" {
if l.GetSSHHost() == p.Host {
return &l, strings.TrimLeft(strings.TrimSuffix(p.Path, ".git"), "/"), nil
}
}
}
return nil, "", errors.New("No Gitea login found")
}

179
cmd/issues.go Normal file
View File

@ -0,0 +1,179 @@
// Copyright 2018 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"
"os"
"strconv"
"strings"
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli"
)
// CmdIssues represents to login a gitea server.
var CmdIssues = cli.Command{
Name: "issues",
Usage: "Log in a Gitea server",
Description: `Log in a Gitea server`,
Action: runIssues,
Subcommands: []cli.Command{
CmdIssuesList,
CmdIssuesCreate,
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login",
},
cli.StringFlag{
Name: "repo, r",
Usage: "Indicate one repository",
},
},
}
var CmdIssuesList = cli.Command{
Name: "ls",
Usage: "Log in a Gitea server",
Description: `Log in a Gitea server`,
Action: runIssuesList,
}
func runIssues(ctx *cli.Context) error {
if len(os.Args) == 3 {
return runIssueDetail(ctx, os.Args[2])
}
return runIssuesList(ctx)
}
func runIssueDetail(ctx *cli.Context, index string) error {
login, owner, repo := initCommand(ctx)
if strings.HasPrefix(index, "#") {
index = index[1:]
}
idx, err := strconv.ParseInt(index, 10, 64)
if err != nil {
return err
}
issue, err := login.Client().GetIssue(owner, repo, idx)
if err != nil {
return err
}
fmt.Printf("#%d %s\n%s created %s\n\n%s", issue.Index,
issue.Title,
issue.Poster.UserName,
issue.Created.Format("2006-01-02 15:04:05"),
issue.Body,
)
return nil
}
func runIssuesList(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
issues, err := login.Client().ListRepoIssues(owner, repo, gitea.ListIssueOption{
Page: 0,
State: string(gitea.StateOpen),
})
if err != nil {
log.Fatal(err)
}
if len(issues) == 0 {
fmt.Println("No issues left")
return nil
}
for _, issue := range issues {
name := issue.Poster.FullName
if len(name) == 0 {
name = issue.Poster.UserName
}
fmt.Printf("#%d\t%s\t%s\t%s\n", issue.Index, name, issue.Updated.Format("2006-01-02 15:04:05"), issue.Title)
}
return nil
}
var CmdIssuesCreate = cli.Command{
Name: "create",
Usage: "Create an issue on repository",
Description: `Create an issue on repository`,
Action: runIssuesCreate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "title, t",
Usage: "issue title to create",
},
cli.StringFlag{
Name: "body, b",
Usage: "issue body to create",
},
},
}
func initCommand(ctx *cli.Context) (*Login, string, string) {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
}
var login *Login
if ctx.IsSet("login") {
login = getLoginByName(ctx.String("login"))
if login == nil {
log.Fatal("indicated login name", ctx.String("login"), "is not exist")
}
} else {
login, err = getActiveLogin()
if err != nil {
log.Fatal("get active login failed")
}
}
var repoPath string
if !ctx.IsSet("repo") {
login, repoPath, err = curGitRepoPath()
if err != nil {
log.Fatal(err.Error())
}
} else {
repoPath = ctx.String("repo")
}
owner, repo := splitRepo(repoPath)
return login, owner, repo
}
func runIssuesCreate(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
_, err := login.Client().CreateIssue(owner, repo, gitea.CreateIssueOption{
Title: ctx.String("title"),
Body: ctx.String("body"),
// TODO:
//Assignee string `json:"assignee"`
//Assignees []string `json:"assignees"`
//Deadline *time.Time `json:"due_date"`
//Milestone int64 `json:"milestone"`
//Labels []int64 `json:"labels"`
//Closed bool `json:"closed"`
})
if err != nil {
log.Fatal(err)
}
return nil
}

35
cmd/log.go Normal file
View File

@ -0,0 +1,35 @@
// Copyright 2018 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"
var (
showLog bool
)
// Println println content according the flag
func Println(a ...interface{}) {
if showLog {
fmt.Println(a...)
}
}
// Printf printf content according the flag
func Printf(format string, a ...interface{}) {
if showLog {
fmt.Printf(format, a...)
}
}
// Error println content as an error information
func Error(a ...interface{}) {
fmt.Println(a...)
}
// Errorf printf content as an error information
func Errorf(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

135
cmd/login.go Normal file
View File

@ -0,0 +1,135 @@
// Copyright 2018 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 (
"crypto/tls"
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli"
)
// CmdLogin represents to login a gitea server.
var CmdLogin = cli.Command{
Name: "login",
Usage: "Log in a Gitea server",
Description: `Log in a Gitea server`,
Action: runLoginList,
Subcommands: []cli.Command{
cmdLoginList,
cmdLoginAdd,
},
}
// CmdLogin represents to login a gitea server.
var cmdLoginAdd = cli.Command{
Name: "add",
Usage: "Log in a Gitea server",
Description: `Log in a Gitea server`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "Name for the gitea login",
},
cli.StringFlag{
Name: "url, u",
Value: "https://try.gitea.io",
EnvVar: "GITEA_SERVER_URL",
Usage: "Gitea server URL",
},
cli.StringFlag{
Name: "token, t",
Value: "",
EnvVar: "GITEA_SERVER_TOKEN",
Usage: "token for operating the Gitea login",
},
cli.BoolFlag{
Name: "insecure, i",
Usage: "insecure visit gitea server",
},
},
Action: runLoginAdd,
}
func runLoginAdd(ctx *cli.Context) error {
if !ctx.IsSet("url") {
log.Fatal("You have to input Gitea server URL")
}
if !ctx.IsSet("token") {
log.Fatal("No token found")
}
if !ctx.IsSet("name") {
log.Fatal("You have to set a name for the login")
}
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
}
client := gitea.NewClient(ctx.String("url"), ctx.String("token"))
if ctx.Bool("insecure") {
cookieJar, _ := cookiejar.New(nil)
client.SetHTTPClient(&http.Client{
Jar: cookieJar,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
})
}
u, err := client.GetMyUserInfo()
if err != nil {
log.Fatal(err)
}
fmt.Println("Login successful! Login name", u.UserName)
err = addLogin(Login{
Name: ctx.String("name"),
URL: ctx.String("url"),
Token: ctx.String("token"),
Insecure: ctx.Bool("insecure"),
})
if err != nil {
log.Fatal(err)
}
err = saveConfig(yamlConfigPath)
if err != nil {
log.Fatal(err)
}
return nil
}
// CmdLogin represents to login a gitea server.
var cmdLoginList = cli.Command{
Name: "ls",
Usage: "Log in a Gitea server",
Description: `Log in a Gitea server`,
Action: runLoginList,
}
func runLoginList(ctx *cli.Context) error {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
}
fmt.Printf("Name\tURL\tSSHHost\n")
for _, l := range config.Logins {
fmt.Printf("%s\t%s\t%s\n", l.Name, l.URL, l.GetSSHHost())
}
return nil
}

60
cmd/logout.go Normal file
View File

@ -0,0 +1,60 @@
// Copyright 2018 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 (
"errors"
"log"
"os"
"github.com/urfave/cli"
)
// CmdLogout represents to logout a gitea server.
var CmdLogout = cli.Command{
Name: "logout",
Usage: "Log out from a Gitea server",
Description: `Log out from a Gitea server`,
Action: runLogout,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "name wants to log out",
},
},
}
func runLogout(ctx *cli.Context) error {
var name string
if len(os.Args) == 3 {
name = os.Args[2]
} else if ctx.IsSet("name") {
name = ctx.String("name")
} else {
return errors.New("need log out server name")
}
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed", yamlConfigPath)
}
var idx = -1
for i, l := range config.Logins {
if l.Name == name {
idx = i
break
}
}
if idx > -1 {
config.Logins = append(config.Logins[:idx], config.Logins[idx+1:]...)
err = saveConfig(yamlConfigPath)
if err != nil {
log.Fatal("save config file failed", yamlConfigPath)
}
}
return nil
}

60
cmd/pulls.go Normal file
View File

@ -0,0 +1,60 @@
// Copyright 2018 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"
)
// CmdPulls represents to login a gitea server.
var CmdPulls = cli.Command{
Name: "pulls",
Usage: "Log in a Gitea server",
Description: `Log in a Gitea server`,
Action: runPulls,
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login",
},
cli.StringFlag{
Name: "repo, r",
Usage: "Indicate one repository",
},
},
}
func runPulls(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
prs, err := login.Client().ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{
Page: 0,
State: string(gitea.StateOpen),
})
if err != nil {
log.Fatal(err)
}
if len(prs) == 0 {
fmt.Println("No pull requests left")
return nil
}
for _, pr := range prs {
name := pr.Poster.FullName
if len(name) == 0 {
name = pr.Poster.UserName
}
fmt.Printf("#%d\t%s\t%s\t%s\n", pr.Index, name, pr.Updated.Format("2006-01-02 15:04:05"), pr.Title)
}
return nil
}

53
cmd/releases.go Normal file
View File

@ -0,0 +1,53 @@
// Copyright 2018 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"
"github.com/urfave/cli"
)
// CmdReleases represents to login a gitea server.
var CmdReleases = cli.Command{
Name: "releases",
Usage: "Log in a Gitea server",
Description: `Log in a Gitea server`,
Action: runReleases,
Flags: []cli.Flag{
cli.StringFlag{
Name: "login, l",
Usage: "Indicate one login",
},
cli.StringFlag{
Name: "repo, r",
Usage: "Indicate one repository",
},
},
}
func runReleases(ctx *cli.Context) error {
login, owner, repo := initCommand(ctx)
releases, err := login.Client().ListReleases(owner, repo)
if err != nil {
log.Fatal(err)
}
if len(releases) == 0 {
fmt.Println("No Releases")
return nil
}
for _, release := range releases {
fmt.Printf("#%s\t%s\t%s\t%s\n", release.TagName,
release.Title,
release.PublishedAt.Format("2006-01-02 15:04:05"),
release.TarURL)
}
return nil
}