2020-09-30 13:11:33 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 09:40:02 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-30 13:11:33 +08:00
|
|
|
|
|
|
|
package login
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/tea/modules/interact"
|
2020-12-12 21:28:37 +08:00
|
|
|
"code.gitea.io/tea/modules/task"
|
2020-09-30 13:11:33 +08: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 23:57:48 +08:00
|
|
|
Description: `Add a Gitea login, without args it will create one interactively`,
|
2022-09-14 02:14:02 +08:00
|
|
|
ArgsUsage: " ", // command does not accept arguments
|
2020-09-30 13:11:33 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "Login name",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-10-02 23:57:48 +08:00
|
|
|
Name: "url",
|
|
|
|
Aliases: []string{"u"},
|
|
|
|
Value: "https://gitea.com",
|
|
|
|
EnvVars: []string{"GITEA_SERVER_URL"},
|
|
|
|
Usage: "Server URL",
|
2020-09-30 13:11:33 +08:00
|
|
|
},
|
2022-10-31 09:56:23 +08:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "no-version-check",
|
|
|
|
Aliases: []string{"nv"},
|
|
|
|
Usage: "Do not check version of Gitea instance",
|
|
|
|
},
|
2020-09-30 13:11:33 +08: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)",
|
|
|
|
},
|
2024-02-15 00:49:29 +08:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "otp",
|
|
|
|
EnvVars: []string{"GITEA_SERVER_OTP"},
|
|
|
|
Usage: "OTP token for auth, if necessary",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "scopes",
|
|
|
|
EnvVars: []string{"GITEA_SCOPES"},
|
|
|
|
Usage: "Token scopes to add when creating a new token, separated by a comma",
|
|
|
|
},
|
2020-09-30 13:11:33 +08:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "ssh-key",
|
|
|
|
Aliases: []string{"s"},
|
2022-09-15 03:00:08 +08:00
|
|
|
Usage: "Path to a SSH key/certificate to use, overrides auto-discovery",
|
2020-09-30 13:11:33 +08:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "insecure",
|
|
|
|
Aliases: []string{"i"},
|
|
|
|
Usage: "Disable TLS verification",
|
|
|
|
},
|
2022-09-15 03:00:08 +08: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 13:11:33 +08:00
|
|
|
},
|
|
|
|
Action: runLoginAdd,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLoginAdd(ctx *cli.Context) error {
|
2020-10-02 23:57:48 +08:00
|
|
|
// if no args create login interactive
|
2020-10-06 02:34:10 +08:00
|
|
|
if ctx.NumFlags() == 0 {
|
2020-10-02 23:57:48 +08:00
|
|
|
return interact.CreateLogin()
|
|
|
|
}
|
|
|
|
|
2022-09-15 03:00:08 +08:00
|
|
|
sshAgent := false
|
|
|
|
if ctx.String("ssh-agent-key") != "" || ctx.String("ssh-agent-principal") != "" {
|
|
|
|
sshAgent = true
|
|
|
|
}
|
|
|
|
|
2020-10-02 23:57:48 +08:00
|
|
|
// else use args to add login
|
2020-12-12 21:28:37 +08:00
|
|
|
return task.CreateLogin(
|
2020-09-30 13:11:33 +08:00
|
|
|
ctx.String("name"),
|
|
|
|
ctx.String("token"),
|
|
|
|
ctx.String("user"),
|
|
|
|
ctx.String("password"),
|
2024-02-15 00:49:29 +08:00
|
|
|
ctx.String("otp"),
|
|
|
|
ctx.String("scopes"),
|
2020-09-30 13:11:33 +08:00
|
|
|
ctx.String("ssh-key"),
|
|
|
|
ctx.String("url"),
|
2022-09-15 03:00:08 +08:00
|
|
|
ctx.String("ssh-agent-principal"),
|
|
|
|
ctx.String("ssh-agent-key"),
|
|
|
|
ctx.Bool("insecure"),
|
2022-10-31 09:56:23 +08:00
|
|
|
sshAgent,
|
|
|
|
!ctx.Bool("no-version-check"))
|
2020-09-30 13:11:33 +08:00
|
|
|
}
|