summaryrefslogtreecommitdiff
path: root/internal/commands/util.go
diff options
context:
space:
mode:
authorJames Elliott <james-d-elliott@users.noreply.github.com>2022-11-25 23:44:55 +1100
committerGitHub <noreply@github.com>2022-11-25 23:44:55 +1100
commit3e4ac7821d51ac447bb39e7e1ea3c385dc3084d9 (patch)
tree69594576856eb8b587158d9245f70aff4fec429a /internal/commands/util.go
parent3c291b5685212813f98f365c8d963e0f107860cb (diff)
refactor: remove pre1 migration path (#4356)
This removes pre1 migrations and improves a lot of tooling.
Diffstat (limited to 'internal/commands/util.go')
-rw-r--r--internal/commands/util.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/internal/commands/util.go b/internal/commands/util.go
index e4af026b4..1534b4bb3 100644
--- a/internal/commands/util.go
+++ b/internal/commands/util.go
@@ -3,8 +3,10 @@ package commands
import (
"fmt"
"os"
+ "syscall"
"github.com/spf13/pflag"
+ "golang.org/x/term"
"github.com/authelia/authelia/v4/internal/utils"
)
@@ -99,3 +101,38 @@ func flagsGetRandomCharacters(flags *pflag.FlagSet, flagNameLength, flagNameChar
return utils.RandomString(n, charset, true), nil
}
+
+func termReadPasswordStrWithPrompt(prompt, flag string) (data string, err error) {
+ var d []byte
+
+ if d, err = termReadPasswordWithPrompt(prompt, flag); err != nil {
+ return "", err
+ }
+
+ return string(d), nil
+}
+
+func termReadPasswordWithPrompt(prompt, flag string) (data []byte, err error) {
+ fd := int(syscall.Stdin) //nolint:unconvert,nolintlint
+
+ if isTerm := term.IsTerminal(fd); !isTerm {
+ switch len(flag) {
+ case 0:
+ return nil, ErrStdinIsNotTerminal
+ case 1:
+ return nil, fmt.Errorf("you must either use an interactive terminal or use the -%s flag", flag)
+ default:
+ return nil, fmt.Errorf("you must either use an interactive terminal or use the --%s flag", flag)
+ }
+ }
+
+ fmt.Print(prompt)
+
+ if data, err = term.ReadPassword(fd); err != nil {
+ return nil, fmt.Errorf("failed to read the input from the terminal: %w", err)
+ }
+
+ fmt.Println("")
+
+ return data, nil
+}