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 login
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/tea/modules/interact"
|
2020-12-12 14:28:37 +01:00
|
|
|
"code.gitea.io/tea/modules/task"
|
2020-09-30 07:11:33 +02:00
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdLoginAdd represents to login a gitea server.
|
|
|
|
var CmdLoginAdd = cli.Command{
|
|
|
|
Name: "add",
|
|
|
|
Usage: "Add a Gitea login",
|
2020-10-02 17:57:48 +02:00
|
|
|
Description: `Add a Gitea login, without args it will create one interactively`,
|
2022-09-13 20:14:02 +02:00
|
|
|
ArgsUsage: " ", // command does not accept arguments
|
2020-09-30 07:11:33 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "Login name",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-10-02 17:57:48 +02:00
|
|
|
Name: "url",
|
|
|
|
Aliases: []string{"u"},
|
|
|
|
Value: "https://gitea.com",
|
|
|
|
EnvVars: []string{"GITEA_SERVER_URL"},
|
|
|
|
Usage: "Server URL",
|
2020-09-30 07:11:33 +02:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
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)",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "ssh-key",
|
|
|
|
Aliases: []string{"s"},
|
2022-09-14 21:00:08 +02:00
|
|
|
Usage: "Path to a SSH key/certificate to use, overrides auto-discovery",
|
2020-09-30 07:11:33 +02:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "insecure",
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
Usage: "Disable TLS verification",
|
|
|
|
},
|
2022-09-14 21:00:08 +02:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "ssh-agent-principal",
|
|
|
|
Aliases: []string{"c"},
|
|
|
|
Usage: "Use SSH certificate with specified principal to login (needs a running ssh-agent with certificate loaded)",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "ssh-agent-key",
|
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "Use SSH public key or SSH fingerprint to login (needs a running ssh-agent with ssh key loaded)",
|
|
|
|
},
|
2020-09-30 07:11:33 +02:00
|
|
|
},
|
|
|
|
Action: runLoginAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginAdd(ctx *cli.Context) error {
|
2020-10-02 17:57:48 +02:00
|
|
|
// if no args create login interactive
|
2020-10-05 20:34:10 +02:00
|
|
|
if ctx.NumFlags() == 0 {
|
2020-10-02 17:57:48 +02:00
|
|
|
return interact.CreateLogin()
|
|
|
|
}
|
|
|
|
|
2022-09-14 21:00:08 +02:00
|
|
|
sshAgent := false
|
|
|
|
if ctx.String("ssh-agent-key") != "" || ctx.String("ssh-agent-principal") != "" {
|
|
|
|
sshAgent = true
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:57:48 +02:00
|
|
|
// else use args to add login
|
2020-12-12 14:28:37 +01:00
|
|
|
return task.CreateLogin(
|
2020-09-30 07:11:33 +02:00
|
|
|
ctx.String("name"),
|
|
|
|
ctx.String("token"),
|
|
|
|
ctx.String("user"),
|
|
|
|
ctx.String("password"),
|
|
|
|
ctx.String("ssh-key"),
|
|
|
|
ctx.String("url"),
|
2022-09-14 21:00:08 +02:00
|
|
|
ctx.String("ssh-agent-principal"),
|
|
|
|
ctx.String("ssh-agent-key"),
|
|
|
|
ctx.Bool("insecure"),
|
|
|
|
sshAgent)
|
2020-09-30 07:11:33 +02:00
|
|
|
}
|