2020-09-30 01:11:33 -04: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 interact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
"code.gitea.io/tea/modules/task"
|
2020-10-02 22:54:09 -04:00
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2020-09-30 01:11:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateLogin create an login interactive
|
|
|
|
func CreateLogin() error {
|
2020-10-02 22:54:09 -04:00
|
|
|
var name, token, user, passwd, sshKey, giteaURL string
|
2020-09-30 01:11:33 -04:00
|
|
|
var insecure = false
|
|
|
|
|
2020-10-02 22:54:09 -04:00
|
|
|
promptI := &survey.Input{Message: "URL of Gitea instance: "}
|
|
|
|
if err := survey.AskOne(promptI, &giteaURL, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
2020-10-02 22:54:09 -04:00
|
|
|
giteaURL = strings.TrimSuffix(strings.TrimSpace(giteaURL), "/")
|
2020-09-30 01:11:33 -04:00
|
|
|
if len(giteaURL) == 0 {
|
|
|
|
fmt.Println("URL is required!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
name, err := task.GenerateLoginName(giteaURL, "")
|
2020-09-30 01:11:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-02 22:54:09 -04:00
|
|
|
promptI = &survey.Input{Message: "Name of new Login [" + name + "]: "}
|
|
|
|
if err := survey.AskOne(promptI, &name); err != nil {
|
|
|
|
return err
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
|
2020-10-02 22:54:09 -04:00
|
|
|
var hasToken bool
|
|
|
|
promptYN := &survey.Confirm{
|
|
|
|
Message: "Do you have an access token?",
|
|
|
|
Default: false,
|
|
|
|
}
|
|
|
|
if err = survey.AskOne(promptYN, &hasToken); err != nil {
|
|
|
|
return err
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
|
2020-10-02 22:54:09 -04:00
|
|
|
if hasToken {
|
|
|
|
promptI = &survey.Input{Message: "Token: "}
|
|
|
|
if err := survey.AskOne(promptI, &token, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-02 22:54:09 -04:00
|
|
|
promptI = &survey.Input{Message: "Username: "}
|
|
|
|
if err = survey.AskOne(promptI, &user, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
promptPW := &survey.Password{Message: "Password: "}
|
|
|
|
if err = survey.AskOne(promptPW, &passwd, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 22:54:09 -04:00
|
|
|
var optSettings bool
|
|
|
|
promptYN = &survey.Confirm{
|
|
|
|
Message: "Set Optional settings: ",
|
|
|
|
Default: false,
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
2020-10-02 22:54:09 -04:00
|
|
|
if err = survey.AskOne(promptYN, &optSettings); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if optSettings {
|
2020-12-11 08:42:41 -05:00
|
|
|
promptI = &survey.Input{Message: "SSH Key Path (leave empty for auto-discovery):"}
|
2020-10-02 22:54:09 -04:00
|
|
|
if err := survey.AskOne(promptI, &sshKey); err != nil {
|
|
|
|
return err
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
|
2020-10-02 22:54:09 -04:00
|
|
|
promptYN = &survey.Confirm{
|
|
|
|
Message: "Allow Insecure connections: ",
|
|
|
|
Default: false,
|
|
|
|
}
|
|
|
|
if err = survey.AskOne(promptYN, &insecure); err != nil {
|
|
|
|
return err
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:28:37 -05:00
|
|
|
return task.CreateLogin(name, token, user, passwd, sshKey, giteaURL, insecure)
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|