2018-09-03 08:43:00 +02:00
|
|
|
// 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"
|
2020-09-15 04:23:42 +02:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-09-03 08:43:00 +02:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
2020-01-04 18:44:25 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-09-03 08:43:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdLogin represents to login a gitea server.
|
|
|
|
var CmdLogin = cli.Command{
|
|
|
|
Name: "login",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Log in to a Gitea server",
|
|
|
|
Description: `Log in to a Gitea server`,
|
2020-09-15 04:23:42 +02:00
|
|
|
Action: runLoginAddInteractive,
|
2020-01-04 18:44:25 +01:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
&cmdLoginList,
|
|
|
|
&cmdLoginAdd,
|
2018-09-03 08:43:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdLogin represents to login a gitea server.
|
|
|
|
var cmdLoginAdd = cli.Command{
|
|
|
|
Name: "add",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Add a Gitea login",
|
|
|
|
Description: `Add a Gitea login`,
|
2018-09-03 08:43:00 +02:00
|
|
|
Flags: []cli.Flag{
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2020-09-15 04:23:42 +02:00
|
|
|
Name: "name",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "Login name",
|
2018-09-03 08:43:00 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2020-04-19 05:09:03 +02:00
|
|
|
Name: "url",
|
|
|
|
Aliases: []string{"u"},
|
|
|
|
Value: "https://try.gitea.io",
|
|
|
|
EnvVars: []string{"GITEA_SERVER_URL"},
|
|
|
|
Usage: "Server URL",
|
|
|
|
Required: true,
|
2018-09-03 08:43:00 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2020-09-15 04:23:42 +02:00
|
|
|
Name: "token",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Value: "",
|
|
|
|
EnvVars: []string{"GITEA_SERVER_TOKEN"},
|
|
|
|
Usage: "Access token. Can be obtained from Settings > Applications",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "user",
|
|
|
|
Value: "",
|
|
|
|
EnvVars: []string{"GITEA_SERVER_USER"},
|
|
|
|
Usage: "User for basic auth (will create token)",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "password",
|
|
|
|
Aliases: []string{"pwd"},
|
|
|
|
Value: "",
|
|
|
|
EnvVars: []string{"GITEA_SERVER_PASSWORD"},
|
|
|
|
Usage: "Password for basic auth (will create token)",
|
2020-04-19 05:09:03 +02:00
|
|
|
},
|
2020-06-12 04:43:55 +02:00
|
|
|
&cli.StringFlag{
|
2020-04-19 05:09:03 +02:00
|
|
|
Name: "ssh-key",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "Path to a SSH key to use for pull/push operations",
|
2018-09-03 08:43:00 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.BoolFlag{
|
2020-03-06 04:43:28 +01:00
|
|
|
Name: "insecure",
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
Usage: "Disable TLS verification",
|
2018-09-03 08:43:00 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: runLoginAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginAdd(ctx *cli.Context) error {
|
2020-09-15 04:23:42 +02:00
|
|
|
return runLoginAddMain(
|
|
|
|
ctx.String("name"),
|
|
|
|
ctx.String("token"),
|
|
|
|
ctx.String("user"),
|
|
|
|
ctx.String("password"),
|
|
|
|
ctx.String("ssh-key"),
|
|
|
|
ctx.String("url"),
|
|
|
|
ctx.Bool("insecure"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginAddInteractive(ctx *cli.Context) error {
|
|
|
|
var stdin, name, token, user, passwd, sshKey, giteaURL string
|
|
|
|
var insecure = false
|
|
|
|
|
|
|
|
fmt.Print("URL of Gitea instance: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
2020-09-15 04:23:42 +02:00
|
|
|
giteaURL = strings.TrimSpace(stdin)
|
|
|
|
if len(giteaURL) == 0 {
|
|
|
|
fmt.Println("URL is required!")
|
|
|
|
return nil
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
2020-09-15 04:23:42 +02:00
|
|
|
|
|
|
|
parsedURL, err := url.Parse(giteaURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name = strings.ReplaceAll(strings.Title(parsedURL.Host), ".", "")
|
|
|
|
|
|
|
|
fmt.Print("Name of new Login [" + name + "]: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
|
|
|
}
|
|
|
|
if len(strings.TrimSpace(stdin)) != 0 {
|
|
|
|
name = strings.TrimSpace(stdin)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Print("Do you have a token [Yes/no]: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
|
|
|
}
|
|
|
|
if len(stdin) != 0 && strings.ToLower(stdin[:1]) == "n" {
|
|
|
|
fmt.Print("Username: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
|
|
|
}
|
|
|
|
user = strings.TrimSpace(stdin)
|
|
|
|
|
|
|
|
fmt.Print("Password: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
|
|
|
}
|
|
|
|
passwd = strings.TrimSpace(stdin)
|
|
|
|
} else {
|
|
|
|
fmt.Print("Token: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
|
|
|
}
|
|
|
|
token = strings.TrimSpace(stdin)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Print("Set Optional settings [yes/No]: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
|
|
|
}
|
|
|
|
if len(stdin) != 0 && strings.ToLower(stdin[:1]) == "y" {
|
|
|
|
fmt.Print("SSH Key Path: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
|
|
|
}
|
|
|
|
sshKey = strings.TrimSpace(stdin)
|
|
|
|
|
|
|
|
fmt.Print("Allow Insecure connections [yes/No]: ")
|
|
|
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
|
|
|
stdin = ""
|
|
|
|
}
|
|
|
|
insecure = len(stdin) != 0 && strings.ToLower(stdin[:1]) == "y"
|
|
|
|
}
|
|
|
|
|
|
|
|
return runLoginAddMain(name, token, user, passwd, sshKey, giteaURL, insecure)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginAddMain(name, token, user, passwd, sshKey, giteaURL string, insecure bool) error {
|
|
|
|
|
|
|
|
if len(giteaURL) == 0 {
|
|
|
|
log.Fatal("You have to input Gitea server URL")
|
|
|
|
}
|
|
|
|
if len(token) == 0 && (len(user)+len(passwd)) == 0 {
|
|
|
|
log.Fatal("No token set")
|
|
|
|
} else if len(user) != 0 && len(passwd) == 0 {
|
|
|
|
log.Fatal("No password set")
|
|
|
|
} else if len(user) == 0 && len(passwd) != 0 {
|
|
|
|
log.Fatal("No user set")
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := loadConfig(yamlConfigPath)
|
|
|
|
if err != nil {
|
2019-11-03 21:34:41 +01:00
|
|
|
log.Fatal("Unable to load config file " + yamlConfigPath)
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 04:23:42 +02:00
|
|
|
client := gitea.NewClient(giteaURL, token)
|
|
|
|
if len(token) == 0 {
|
|
|
|
client.SetBasicAuth(user, passwd)
|
|
|
|
}
|
|
|
|
if insecure {
|
2018-09-03 08:43:00 +02:00
|
|
|
cookieJar, _ := cookiejar.New(nil)
|
|
|
|
|
|
|
|
client.SetHTTPClient(&http.Client{
|
|
|
|
Jar: cookieJar,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2020-09-15 04:23:42 +02:00
|
|
|
|
2018-09-03 08:43:00 +02:00
|
|
|
u, err := client.GetMyUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 04:23:42 +02:00
|
|
|
if len(token) == 0 {
|
|
|
|
// create token
|
|
|
|
host, _ := os.Hostname()
|
|
|
|
tl, err := client.ListAccessTokens(gitea.ListAccessTokensOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tokenName := host + "-tea"
|
|
|
|
for i := range tl {
|
|
|
|
if tl[i].Name == tokenName {
|
|
|
|
tokenName += time.Now().Format("2006-01-02_15-04-05")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t, err := client.CreateAccessToken(gitea.CreateAccessTokenOption{Name: tokenName})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
token = t.Token
|
|
|
|
}
|
|
|
|
|
2019-11-03 21:34:41 +01:00
|
|
|
fmt.Println("Login successful! Login name " + u.UserName)
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2020-09-15 04:23:42 +02:00
|
|
|
if len(name) == 0 {
|
|
|
|
parsedURL, err := url.Parse(giteaURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name = strings.ReplaceAll(strings.Title(parsedURL.Host), ".", "")
|
|
|
|
for _, l := range config.Logins {
|
|
|
|
if l.Name == name {
|
|
|
|
name += "_" + u.UserName
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 08:43:00 +02:00
|
|
|
err = addLogin(Login{
|
2020-09-15 04:23:42 +02:00
|
|
|
Name: name,
|
|
|
|
URL: giteaURL,
|
|
|
|
Token: token,
|
|
|
|
Insecure: insecure,
|
|
|
|
SSHKey: sshKey,
|
2020-04-19 05:09:03 +02:00
|
|
|
User: u.UserName,
|
2018-09-03 08:43:00 +02:00
|
|
|
})
|
|
|
|
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",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "List Gitea logins",
|
|
|
|
Description: `List Gitea logins`,
|
2018-09-03 08:43:00 +02:00
|
|
|
Action: runLoginList,
|
2020-07-16 18:12:22 +02:00
|
|
|
Flags: []cli.Flag{&OutputFlag},
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginList(ctx *cli.Context) error {
|
|
|
|
err := loadConfig(yamlConfigPath)
|
|
|
|
if err != nil {
|
2019-11-03 21:34:41 +01:00
|
|
|
log.Fatal("Unable to load config file " + yamlConfigPath)
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 18:12:22 +02:00
|
|
|
headers := []string{
|
|
|
|
"Name",
|
|
|
|
"URL",
|
|
|
|
"SSHHost",
|
2020-08-10 18:31:38 +02:00
|
|
|
"User",
|
2020-07-16 18:12:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2018-09-03 08:43:00 +02:00
|
|
|
for _, l := range config.Logins {
|
2020-07-16 18:12:22 +02:00
|
|
|
values = append(values, []string{
|
|
|
|
l.Name,
|
|
|
|
l.URL,
|
|
|
|
l.GetSSHHost(),
|
2020-08-10 18:31:38 +02:00
|
|
|
l.User,
|
2020-07-16 18:12:22 +02:00
|
|
|
})
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 18:12:22 +02:00
|
|
|
Output(outputValue, headers, values)
|
|
|
|
|
2018-09-03 08:43:00 +02:00
|
|
|
return nil
|
|
|
|
}
|