mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-03 04:27:21 -05:00
819cc1ab21
Adds a new subcommand to clone repos: ``` tea clone --login try --depth 1 norwin/test tea clone gitea/tea tea clone noerw/tea # will set up `master` to track `upstream` remote tea clone try.gitea.io/noerw/test # will automatically set --login ``` This is just a replacement for `git clone` with small benefits: - [x] does not depend on `git`, as tea ships with go-git - [x] spares you typing of URLs and autoselects https/ssh based on your login config - [x] forked repos: set up origin + upstream remote Co-authored-by: Norwin <git@nroo.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io> Reviewed-on: https://gitea.com/gitea/tea/pulls/411 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Norwin <noerw@noreply.gitea.io> Co-committed-by: Norwin <noerw@noreply.gitea.io>
200 lines
4.1 KiB
Go
200 lines
4.1 KiB
Go
// 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 config
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
)
|
|
|
|
// Login represents a login to a gitea server, you even could add multiple logins for one gitea server
|
|
type Login struct {
|
|
Name string `yaml:"name"`
|
|
URL string `yaml:"url"`
|
|
Token string `yaml:"token"`
|
|
Default bool `yaml:"default"`
|
|
SSHHost string `yaml:"ssh_host"`
|
|
// optional path to the private key
|
|
SSHKey string `yaml:"ssh_key"`
|
|
Insecure bool `yaml:"insecure"`
|
|
// User is username from gitea
|
|
User string `yaml:"user"`
|
|
// Created is auto created unix timestamp
|
|
Created int64 `yaml:"created"`
|
|
}
|
|
|
|
// GetLogins return all login available by config
|
|
func GetLogins() ([]Login, error) {
|
|
if err := loadConfig(); err != nil {
|
|
return nil, err
|
|
}
|
|
return config.Logins, nil
|
|
}
|
|
|
|
// GetDefaultLogin return the default login
|
|
func GetDefaultLogin() (*Login, error) {
|
|
if err := loadConfig(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(config.Logins) == 0 {
|
|
return nil, errors.New("No available login")
|
|
}
|
|
for _, l := range config.Logins {
|
|
if l.Default {
|
|
return &l, nil
|
|
}
|
|
}
|
|
|
|
return &config.Logins[0], nil
|
|
}
|
|
|
|
// SetDefaultLogin set the default login by name (case insensitive)
|
|
func SetDefaultLogin(name string) error {
|
|
if err := loadConfig(); err != nil {
|
|
return err
|
|
}
|
|
|
|
loginExist := false
|
|
for i := range config.Logins {
|
|
config.Logins[i].Default = false
|
|
if strings.ToLower(config.Logins[i].Name) == strings.ToLower(name) {
|
|
config.Logins[i].Default = true
|
|
loginExist = true
|
|
}
|
|
}
|
|
|
|
if !loginExist {
|
|
return fmt.Errorf("login '%s' not found", name)
|
|
}
|
|
|
|
return saveConfig()
|
|
}
|
|
|
|
// GetLoginByName get login by name (case insensitive)
|
|
func GetLoginByName(name string) *Login {
|
|
err := loadConfig()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, l := range config.Logins {
|
|
if strings.ToLower(l.Name) == strings.ToLower(name) {
|
|
return &l
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetLoginByToken get login by token
|
|
func GetLoginByToken(token string) *Login {
|
|
err := loadConfig()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, l := range config.Logins {
|
|
if l.Token == token {
|
|
return &l
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetLoginByHost finds a login by it's server URL
|
|
func GetLoginByHost(host string) *Login {
|
|
err := loadConfig()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, l := range config.Logins {
|
|
loginURL, err := url.Parse(l.URL)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if loginURL.Host == host {
|
|
return &l
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteLogin delete a login by name from config
|
|
func DeleteLogin(name string) error {
|
|
var idx = -1
|
|
for i, l := range config.Logins {
|
|
if l.Name == name {
|
|
idx = i
|
|
break
|
|
}
|
|
}
|
|
if idx == -1 {
|
|
return fmt.Errorf("can not delete login '%s', does not exist", name)
|
|
}
|
|
|
|
config.Logins = append(config.Logins[:idx], config.Logins[idx+1:]...)
|
|
|
|
return saveConfig()
|
|
}
|
|
|
|
// AddLogin save a login to config
|
|
func AddLogin(login *Login) error {
|
|
if err := loadConfig(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// save login to global var
|
|
config.Logins = append(config.Logins, *login)
|
|
|
|
// save login to config file
|
|
return saveConfig()
|
|
}
|
|
|
|
// Client returns a client to operate Gitea API. You may provide additional modifiers
|
|
// for the client like gitea.SetBasicAuth() for customization
|
|
func (l *Login) Client(options ...func(*gitea.Client)) *gitea.Client {
|
|
httpClient := &http.Client{}
|
|
if l.Insecure {
|
|
cookieJar, _ := cookiejar.New(nil)
|
|
|
|
httpClient = &http.Client{
|
|
Jar: cookieJar,
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}}
|
|
}
|
|
|
|
options = append(options, gitea.SetToken(l.Token), gitea.SetHTTPClient(httpClient))
|
|
|
|
client, err := gitea.NewClient(l.URL, options...)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return client
|
|
}
|
|
|
|
// GetSSHHost returns SSH host name
|
|
func (l *Login) GetSSHHost() string {
|
|
if l.SSHHost != "" {
|
|
return l.SSHHost
|
|
}
|
|
|
|
u, err := url.Parse(l.URL)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
return u.Hostname()
|
|
}
|