From c83bc55b523251c0891a014155d0948f193e6f38 Mon Sep 17 00:00:00 2001 From: zeripath Date: Wed, 20 May 2020 20:42:03 +0100 Subject: [PATCH] TrimSpace when reading InternalToken from a file (#11502) (#11524) InternalTokens are fixed as alphanum strings therefore TrimSpace from these. Also use isatty to not add a terminal newline when redirecting generate. Fix #11498 Signed-off-by: Andrew Thornton Co-authored-by: Lauris BH Co-authored-by: Lauris BH --- cmd/generate.go | 23 ++++++++++++++++++++--- modules/setting/setting.go | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cmd/generate.go b/cmd/generate.go index 4e91b1d3f..13a99c94f 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -7,9 +7,11 @@ package cmd import ( "fmt" + "os" "code.gitea.io/gitea/modules/generate" + "github.com/mattn/go-isatty" "github.com/urfave/cli" ) @@ -59,7 +61,12 @@ func runGenerateInternalToken(c *cli.Context) error { return err } - fmt.Printf("%s\n", internalToken) + fmt.Printf("%s", internalToken) + + if isatty.IsTerminal(os.Stdout.Fd()) { + fmt.Printf("\n") + } + return nil } @@ -69,7 +76,12 @@ func runGenerateLfsJwtSecret(c *cli.Context) error { return err } - fmt.Printf("%s\n", JWTSecretBase64) + fmt.Printf("%s", JWTSecretBase64) + + if isatty.IsTerminal(os.Stdout.Fd()) { + fmt.Printf("\n") + } + return nil } @@ -79,6 +91,11 @@ func runGenerateSecretKey(c *cli.Context) error { return err } - fmt.Printf("%s\n", secretKey) + fmt.Printf("%s", secretKey) + + if isatty.IsTerminal(os.Stdout.Fd()) { + fmt.Printf("\n") + } + return nil } diff --git a/modules/setting/setting.go b/modules/setting/setting.go index ede4687c8..9507d4693 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1069,7 +1069,7 @@ func loadInternalToken(sec *ini.Section) string { return token } - return string(buf) + return strings.TrimSpace(string(buf)) default: log.Fatal("Unsupported URI-Scheme %q (INTERNAL_TOKEN_URI = %q)", tempURI.Scheme, uri) }