Skip to content

Commit a68092a

Browse files
committed
DUPLO-13771 Fix SonarQube PR issue 2/2
1 parent 602788d commit a68092a

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

internal/interactive.go

+25-15
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,7 @@ func TokenViaListener(baseUrl string, admin bool, cmd string, port int, timeout
107107

108108
// If we are done, send the result to the channel.
109109
if completed {
110-
rp := TokenResult{}
111-
if len(tokenBytes) == 0 {
112-
rp.err = errors.New("canceled")
113-
} else {
114-
err = json.Unmarshal(tokenBytes, &rp)
115-
if err != nil {
116-
message := fmt.Sprintf("%s: cannot unmarshal response from JSON: %s", "/v2/callbackWithOtp", err.Error())
117-
rp.err = errors.New(message)
118-
}
119-
}
110+
rp := getTokenResult(tokenBytes)
120111
done <- rp
121112
}
122113
})
@@ -125,11 +116,7 @@ func TokenViaListener(baseUrl string, admin bool, cmd string, port int, timeout
125116
}()
126117

127118
// Open the browser.
128-
adminFlag := ""
129-
if admin {
130-
adminFlag = "&isAdmin=true"
131-
}
132-
url := fmt.Sprintf("%s/app/user/verify-token?localAppName=%s&localPort=%d%s&redirect=true", baseUrl, cmd, localPort, adminFlag)
119+
url := getInteractiveUrl(admin, baseUrl, cmd, localPort)
133120
err = open.Run(url)
134121
DieIf(err, "failed to open interactive browser session")
135122

@@ -142,6 +129,29 @@ func TokenViaListener(baseUrl string, admin bool, cmd string, port int, timeout
142129
}
143130
}
144131

132+
func getTokenResult(tokenBytes []byte) TokenResult {
133+
tokenResult := TokenResult{}
134+
if len(tokenBytes) == 0 {
135+
tokenResult.err = errors.New("canceled")
136+
} else {
137+
err := json.Unmarshal(tokenBytes, &tokenResult)
138+
if err != nil {
139+
message := fmt.Sprintf("%s: cannot unmarshal response from JSON: %s", "/v2/callbackWithOtp", err.Error())
140+
tokenResult.err = errors.New(message)
141+
}
142+
}
143+
return tokenResult
144+
}
145+
146+
func getInteractiveUrl(admin bool, baseUrl string, cmd string, localPort int) string {
147+
adminFlag := ""
148+
if admin {
149+
adminFlag = "&isAdmin=true"
150+
}
151+
url := fmt.Sprintf("%s/app/user/verify-token?localAppName=%s&localPort=%d%s&redirect=true", baseUrl, cmd, localPort, adminFlag)
152+
return url
153+
}
154+
145155
func MustTokenInteractive(host string, admin bool, cmd string, port int) (tokenResult TokenResult) {
146156
tokenResult = TokenViaListener(host, admin, cmd, port, 180*time.Second)
147157
DieIf(tokenResult.err, "failed to get token from interactive browser session")

0 commit comments

Comments
 (0)