mirror of
https://github.com/fluxcd/flux2.git
synced 2026-02-24 00:21:46 +00:00
Merge pull request #2038 from fluxcd/makkes/prompt-for-tokens
Prompt for access tokens in 'bootstrap' command
This commit is contained in:
commit
21e5acc0e0
4 changed files with 37 additions and 2 deletions
|
|
@ -111,7 +111,11 @@ func init() {
|
||||||
func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
|
func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
ghToken := os.Getenv(ghTokenEnvVar)
|
ghToken := os.Getenv(ghTokenEnvVar)
|
||||||
if ghToken == "" {
|
if ghToken == "" {
|
||||||
return fmt.Errorf("%s environment variable not found", ghTokenEnvVar)
|
var err error
|
||||||
|
ghToken, err = readPasswordFromStdin("Please enter your GitHub personal access token (PAT): ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not read token: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bootstrapValidate(); err != nil {
|
if err := bootstrapValidate(); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,11 @@ func init() {
|
||||||
func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
|
func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
glToken := os.Getenv(glTokenEnvVar)
|
glToken := os.Getenv(glTokenEnvVar)
|
||||||
if glToken == "" {
|
if glToken == "" {
|
||||||
return fmt.Errorf("%s environment variable not found", glTokenEnvVar)
|
var err error
|
||||||
|
glToken, err = readPasswordFromStdin("Please enter your GitLab personal access token (PAT): ")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not read token: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if projectNameIsValid, err := regexp.MatchString(gitlabProjectRegex, gitlabArgs.repository); err != nil || !projectNameIsValid {
|
if projectNameIsValid, err := regexp.MatchString(gitlabProjectRegex, gitlabArgs.repository); err != nil || !projectNameIsValid {
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,16 @@ limitations under the License.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"golang.org/x/term"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||||
|
|
||||||
|
|
@ -167,3 +171,25 @@ func homeDir() string {
|
||||||
}
|
}
|
||||||
return os.Getenv("USERPROFILE") // windows
|
return os.Getenv("USERPROFILE") // windows
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readPasswordFromStdin reads a password from stdin and returns the input
|
||||||
|
// with trailing newline and/or carriage return removed. It also makes sure that terminal
|
||||||
|
// echoing is turned off if stdin is a terminal.
|
||||||
|
func readPasswordFromStdin(prompt string) (string, error) {
|
||||||
|
var out string
|
||||||
|
var err error
|
||||||
|
fmt.Fprint(os.Stdout, prompt)
|
||||||
|
stdinFD := int(os.Stdin.Fd())
|
||||||
|
if term.IsTerminal(stdinFD) {
|
||||||
|
var inBytes []byte
|
||||||
|
inBytes, err = term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
|
out = string(inBytes)
|
||||||
|
} else {
|
||||||
|
out, err = bufio.NewReader(os.Stdin).ReadString('\n')
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not read from stdin: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
return strings.TrimRight(out, "\r\n"), nil
|
||||||
|
}
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -29,6 +29,7 @@ require (
|
||||||
github.com/spf13/cobra v1.1.3
|
github.com/spf13/cobra v1.1.3
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
|
||||||
|
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
|
||||||
k8s.io/api v0.22.2
|
k8s.io/api v0.22.2
|
||||||
k8s.io/apiextensions-apiserver v0.22.2
|
k8s.io/apiextensions-apiserver v0.22.2
|
||||||
k8s.io/apimachinery v0.22.2
|
k8s.io/apimachinery v0.22.2
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue