2020-09-30 07:11:33 +02:00
|
|
|
// 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"
|
2023-04-03 07:49:33 +02:00
|
|
|
"os"
|
2020-12-11 14:42:41 +01:00
|
|
|
"strings"
|
2020-09-30 07:11:33 +02:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
2022-09-14 21:00:08 +02:00
|
|
|
"code.gitea.io/tea/modules/utils"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2020-09-30 07:11:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2022-09-14 21:00:08 +02:00
|
|
|
SSHKey string `yaml:"ssh_key"`
|
|
|
|
Insecure bool `yaml:"insecure"`
|
|
|
|
SSHCertPrincipal string `yaml:"ssh_certificate_principal"`
|
|
|
|
SSHAgent bool `yaml:"ssh_agent"`
|
|
|
|
SSHKeyFingerprint string `yaml:"ssh_key_agent_pub"`
|
|
|
|
SSHPassphrase string `yaml:"-"`
|
2022-10-31 02:56:23 +01:00
|
|
|
VersionCheck bool `yaml:"version_check"`
|
2020-10-02 17:57:48 +02:00
|
|
|
// User is username from gitea
|
2020-09-30 07:11:33 +02:00
|
|
|
User string `yaml:"user"`
|
2020-10-02 17:57:48 +02:00
|
|
|
// Created is auto created unix timestamp
|
|
|
|
Created int64 `yaml:"created"`
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
// GetLogins return all login available by config
|
|
|
|
func GetLogins() ([]Login, error) {
|
|
|
|
if err := loadConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config.Logins, nil
|
|
|
|
}
|
|
|
|
|
2020-09-30 07:11:33 +02:00
|
|
|
// GetDefaultLogin return the default login
|
|
|
|
func GetDefaultLogin() (*Login, error) {
|
2020-12-12 14:28:37 +01:00
|
|
|
if err := loadConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Logins) == 0 {
|
2020-09-30 07:11:33 +02:00
|
|
|
return nil, errors.New("No available login")
|
|
|
|
}
|
2020-12-12 14:28:37 +01:00
|
|
|
for _, l := range config.Logins {
|
2020-09-30 07:11:33 +02:00
|
|
|
if l.Default {
|
|
|
|
return &l, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
return &config.Logins[0], nil
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
// 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)
|
2020-09-30 07:11:33 +02:00
|
|
|
func GetLoginByName(name string) *Login {
|
2020-12-12 14:28:37 +01:00
|
|
|
err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range config.Logins {
|
|
|
|
if strings.ToLower(l.Name) == strings.ToLower(name) {
|
2020-09-30 07:11:33 +02:00
|
|
|
return &l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
// GetLoginByToken get login by token
|
|
|
|
func GetLoginByToken(token string) *Login {
|
|
|
|
err := loadConfig()
|
2020-09-30 21:44:22 +02:00
|
|
|
if err != nil {
|
2020-12-12 14:28:37 +01:00
|
|
|
log.Fatal(err)
|
2020-09-30 21:44:22 +02:00
|
|
|
}
|
2020-09-30 07:11:33 +02:00
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
for _, l := range config.Logins {
|
|
|
|
if l.Token == token {
|
|
|
|
return &l
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 14:28:37 +01:00
|
|
|
return nil
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 14:09:27 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
// DeleteLogin delete a login by name from config
|
2020-10-02 17:45:55 +02:00
|
|
|
func DeleteLogin(name string) error {
|
2022-09-14 21:00:08 +02:00
|
|
|
idx := -1
|
2020-12-12 14:28:37 +01:00
|
|
|
for i, l := range config.Logins {
|
2020-10-02 17:45:55 +02:00
|
|
|
if l.Name == name {
|
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx == -1 {
|
|
|
|
return fmt.Errorf("can not delete login '%s', does not exist", name)
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
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)
|
2020-10-02 17:45:55 +02:00
|
|
|
|
2020-12-12 14:28:37 +01:00
|
|
|
// save login to config file
|
|
|
|
return saveConfig()
|
2020-10-02 17:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-08-30 17:19:45 +02:00
|
|
|
// Client returns a client to operate Gitea API. You may provide additional modifiers
|
|
|
|
// for the client like gitea.SetBasicAuth() for customization
|
2022-06-10 16:31:24 +02:00
|
|
|
func (l *Login) Client(options ...gitea.ClientOption) *gitea.Client {
|
2020-12-11 10:07:29 +01:00
|
|
|
httpClient := &http.Client{}
|
|
|
|
if l.Insecure {
|
|
|
|
cookieJar, _ := cookiejar.New(nil)
|
2020-09-30 21:44:22 +02:00
|
|
|
|
2020-12-11 10:07:29 +01:00
|
|
|
httpClient = &http.Client{
|
|
|
|
Jar: cookieJar,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
2022-09-14 21:00:08 +02:00
|
|
|
},
|
|
|
|
}
|
2020-09-30 21:44:22 +02:00
|
|
|
}
|
|
|
|
|
2022-10-31 02:56:23 +01:00
|
|
|
// versioncheck must be prepended in options to make sure we don't hit any version checks in the sdk
|
|
|
|
if !l.VersionCheck {
|
|
|
|
options = append([]gitea.ClientOption{gitea.SetGiteaVersion("")}, options...)
|
|
|
|
}
|
|
|
|
|
2021-08-30 17:19:45 +02:00
|
|
|
options = append(options, gitea.SetToken(l.Token), gitea.SetHTTPClient(httpClient))
|
|
|
|
|
2022-09-14 21:00:08 +02:00
|
|
|
if ok, err := utils.IsKeyEncrypted(l.SSHKey); ok && err == nil && l.SSHPassphrase == "" {
|
|
|
|
promptPW := &survey.Password{Message: "ssh-key is encrypted please enter the passphrase: "}
|
|
|
|
if err = survey.AskOne(promptPW, &l.SSHPassphrase, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.SSHCertPrincipal != "" {
|
|
|
|
options = append(options, gitea.UseSSHCert(l.SSHCertPrincipal, l.SSHKey, l.SSHPassphrase))
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.SSHKeyFingerprint != "" {
|
|
|
|
options = append(options, gitea.UseSSHPubkey(l.SSHKeyFingerprint, l.SSHKey, l.SSHPassphrase))
|
|
|
|
}
|
|
|
|
|
2021-08-30 17:19:45 +02:00
|
|
|
client, err := gitea.NewClient(l.URL, options...)
|
2020-09-30 07:11:33 +02:00
|
|
|
if err != nil {
|
2023-04-03 07:49:33 +02:00
|
|
|
var versionError *gitea.ErrUnknownVersion
|
|
|
|
if !errors.As(err, &versionError) {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "WARNING: could not detect gitea version: %s\nINFO: set gitea version: to last supported one\n", versionError)
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
2020-12-11 10:07:29 +01:00
|
|
|
return client
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2020-12-11 10:07:29 +01:00
|
|
|
// GetSSHHost returns SSH host name
|
|
|
|
func (l *Login) GetSSHHost() string {
|
|
|
|
if l.SSHHost != "" {
|
|
|
|
return l.SSHHost
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2020-12-11 10:07:29 +01:00
|
|
|
u, err := url.Parse(l.URL)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:21 +02:00
|
|
|
return u.Host
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|