2018-09-03 06:43:00 +00: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 02:23:42 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-09-03 06:43:00 +00:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
2020-09-23 14:23:27 +00:00
|
|
|
"github.com/skratchdot/open-golang/open"
|
2020-01-04 17:44:25 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-09-03 06:43:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdLogin represents to login a gitea server.
|
|
|
|
var CmdLogin = cli.Command{
|
|
|
|
Name: "login",
|
2019-11-03 20:34:41 +00:00
|
|
|
Usage: "Log in to a Gitea server",
|
|
|
|
Description: `Log in to a Gitea server`,
|
2020-09-15 02:23:42 +00:00
|
|
|
Action: runLoginAddInteractive,
|
2020-01-04 17:44:25 +00:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
&cmdLoginList,
|
|
|
|
&cmdLoginAdd,
|
2020-09-23 14:23:27 +00:00
|
|
|
&cmdLoginEdit,
|
|
|
|
&cmdLoginSetDefault,
|
2018-09-03 06:43:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-23 14:23:27 +00:00
|
|
|
// cmdLoginEdit represents to login a gitea server.
|
|
|
|
var cmdLoginEdit = cli.Command{
|
|
|
|
Name: "edit",
|
|
|
|
Usage: "Edit Gitea logins",
|
|
|
|
Description: `Edit Gitea logins`,
|
|
|
|
Action: runLoginEdit,
|
|
|
|
Flags: []cli.Flag{&OutputFlag},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginEdit(ctx *cli.Context) error {
|
|
|
|
return open.Start(yamlConfigPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmdLoginSetDefault represents to login a gitea server.
|
|
|
|
var cmdLoginSetDefault = cli.Command{
|
|
|
|
Name: "default",
|
|
|
|
Usage: "Get or Set Default Login",
|
|
|
|
Description: `Get or Set Default Login`,
|
|
|
|
ArgsUsage: "<Login>",
|
|
|
|
Action: runLoginSetDefault,
|
|
|
|
Flags: []cli.Flag{&OutputFlag},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginSetDefault(ctx *cli.Context) error {
|
|
|
|
if err := loadConfig(yamlConfigPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ctx.Args().Len() == 0 {
|
|
|
|
l, err := getDefaultLogin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Default Login: %s\n", l.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
loginExist := false
|
|
|
|
for i := range config.Logins {
|
|
|
|
config.Logins[i].Default = false
|
|
|
|
if config.Logins[i].Name == ctx.Args().First() {
|
|
|
|
config.Logins[i].Default = true
|
|
|
|
loginExist = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !loginExist {
|
|
|
|
return fmt.Errorf("login '%s' not found", ctx.Args().First())
|
|
|
|
}
|
|
|
|
|
|
|
|
return saveConfig(yamlConfigPath)
|
|
|
|
}
|
|
|
|
|
2018-09-03 06:43:00 +00:00
|
|
|
// CmdLogin represents to login a gitea server.
|
|
|
|
var cmdLoginAdd = cli.Command{
|
|
|
|
Name: "add",
|
2019-11-03 20:34:41 +00:00
|
|
|
Usage: "Add a Gitea login",
|
|
|
|
Description: `Add a Gitea login`,
|
2018-09-03 06:43:00 +00:00
|
|
|
Flags: []cli.Flag{
|
2020-01-04 17:44:25 +00:00
|
|
|
&cli.StringFlag{
|
2020-09-15 02:23:42 +00:00
|
|
|
Name: "name",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "Login name",
|
2018-09-03 06:43:00 +00:00
|
|
|
},
|
2020-01-04 17:44:25 +00:00
|
|
|
&cli.StringFlag{
|
2020-04-19 03:09:03 +00:00
|
|
|
Name: "url",
|
|
|
|
Aliases: []string{"u"},
|
|
|
|
Value: "https://try.gitea.io",
|
|
|
|
EnvVars: []string{"GITEA_SERVER_URL"},
|
|
|
|
Usage: "Server URL",
|
|
|
|
Required: true,
|
2018-09-03 06:43:00 +00:00
|
|
|
},
|
2020-01-04 17:44:25 +00:00
|
|
|
&cli.StringFlag{
|
2020-09-15 02:23:42 +00: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 03:09:03 +00:00
|
|
|
},
|
2020-06-12 02:43:55 +00:00
|
|
|
&cli.StringFlag{
|
2020-04-19 03:09:03 +00:00
|
|
|
Name: "ssh-key",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "Path to a SSH key to use for pull/push operations",
|
2018-09-03 06:43:00 +00:00
|
|
|
},
|
2020-01-04 17:44:25 +00:00
|
|
|
&cli.BoolFlag{
|
2020-03-06 03:43:28 +00:00
|
|
|
Name: "insecure",
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
Usage: "Disable TLS verification",
|
2018-09-03 06:43:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: runLoginAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginAdd(ctx *cli.Context) error {
|
2020-09-15 02:23:42 +00: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 06:43:00 +00:00
|
|
|
}
|
2020-09-15 02:23:42 +00:00
|
|
|
giteaURL = strings.TrimSpace(stdin)
|
|
|
|
if len(giteaURL) == 0 {
|
|
|
|
fmt.Println("URL is required!")
|
|
|
|
return nil
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
2020-09-15 02:23:42 +00: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 06:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := loadConfig(yamlConfigPath)
|
|
|
|
if err != nil {
|
2019-11-03 20:34:41 +00:00
|
|
|
log.Fatal("Unable to load config file " + yamlConfigPath)
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
httpClient := &http.Client{}
|
2020-09-15 02:23:42 +00:00
|
|
|
if insecure {
|
2018-09-03 06:43:00 +00:00
|
|
|
cookieJar, _ := cookiejar.New(nil)
|
2020-09-16 02:01:41 +00:00
|
|
|
httpClient = &http.Client{
|
2018-09-03 06:43:00 +00:00
|
|
|
Jar: cookieJar,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
2020-09-16 02:01:41 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
client, err := gitea.NewClient(giteaURL,
|
|
|
|
gitea.SetToken(token),
|
|
|
|
gitea.SetBasicAuth(user, passwd),
|
|
|
|
gitea.SetHTTPClient(httpClient),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
2020-09-15 02:23:42 +00:00
|
|
|
|
2020-09-16 02:01:41 +00:00
|
|
|
u, _, err := client.GetMyUserInfo()
|
2018-09-03 06:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 02:23:42 +00:00
|
|
|
if len(token) == 0 {
|
|
|
|
// create token
|
|
|
|
host, _ := os.Hostname()
|
2020-09-16 02:01:41 +00:00
|
|
|
tl, _, err := client.ListAccessTokens(gitea.ListAccessTokensOptions{})
|
2020-09-15 02:23:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 02:01:41 +00:00
|
|
|
t, _, err := client.CreateAccessToken(gitea.CreateAccessTokenOption{Name: tokenName})
|
2020-09-15 02:23:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
token = t.Token
|
|
|
|
}
|
|
|
|
|
2019-11-03 20:34:41 +00:00
|
|
|
fmt.Println("Login successful! Login name " + u.UserName)
|
2018-09-03 06:43:00 +00:00
|
|
|
|
2020-09-15 02:23:42 +00: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 06:43:00 +00:00
|
|
|
err = addLogin(Login{
|
2020-09-15 02:23:42 +00:00
|
|
|
Name: name,
|
|
|
|
URL: giteaURL,
|
|
|
|
Token: token,
|
|
|
|
Insecure: insecure,
|
|
|
|
SSHKey: sshKey,
|
2020-04-19 03:09:03 +00:00
|
|
|
User: u.UserName,
|
2018-09-03 06:43:00 +00: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 20:34:41 +00:00
|
|
|
Usage: "List Gitea logins",
|
|
|
|
Description: `List Gitea logins`,
|
2018-09-03 06:43:00 +00:00
|
|
|
Action: runLoginList,
|
2020-07-16 16:12:22 +00:00
|
|
|
Flags: []cli.Flag{&OutputFlag},
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginList(ctx *cli.Context) error {
|
|
|
|
err := loadConfig(yamlConfigPath)
|
|
|
|
if err != nil {
|
2019-11-03 20:34:41 +00:00
|
|
|
log.Fatal("Unable to load config file " + yamlConfigPath)
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:12:22 +00:00
|
|
|
headers := []string{
|
|
|
|
"Name",
|
|
|
|
"URL",
|
|
|
|
"SSHHost",
|
2020-08-10 16:31:38 +00:00
|
|
|
"User",
|
2020-09-23 14:23:27 +00:00
|
|
|
"Default",
|
2020-07-16 16:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2018-09-03 06:43:00 +00:00
|
|
|
for _, l := range config.Logins {
|
2020-07-16 16:12:22 +00:00
|
|
|
values = append(values, []string{
|
|
|
|
l.Name,
|
|
|
|
l.URL,
|
|
|
|
l.GetSSHHost(),
|
2020-08-10 16:31:38 +00:00
|
|
|
l.User,
|
2020-09-23 14:23:27 +00:00
|
|
|
fmt.Sprint(l.Default),
|
2020-07-16 16:12:22 +00:00
|
|
|
})
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:12:22 +00:00
|
|
|
Output(outputValue, headers, values)
|
|
|
|
|
2018-09-03 06:43:00 +00:00
|
|
|
return nil
|
|
|
|
}
|