mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[GH-26382] format command mmctl auth list (#26423)
This commit is contained in:
parent
6c470f6210
commit
1021e51270
@ -293,7 +293,8 @@ func listCmdF(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
serverNames := []string{}
|
serverNames := []string{}
|
||||||
var maxNameLen, maxUsernameLen, maxInstanceURLLen int
|
nameTitle, usernameTitle, instanceURLTitle := "Name", "Username", "InstanceURL"
|
||||||
|
maxNameLen, maxUsernameLen, maxInstanceURLLen := len(nameTitle), len(usernameTitle), len(instanceURLTitle)
|
||||||
for _, c := range *credentialsList {
|
for _, c := range *credentialsList {
|
||||||
serverNames = append(serverNames, c.Name)
|
serverNames = append(serverNames, c.Name)
|
||||||
if maxNameLen <= len(c.Name) {
|
if maxNameLen <= len(c.Name) {
|
||||||
@ -310,7 +311,7 @@ func listCmdF(cmd *cobra.Command, args []string) error {
|
|||||||
return serverNames[i] < serverNames[j]
|
return serverNames[i] < serverNames[j]
|
||||||
})
|
})
|
||||||
|
|
||||||
printer.Print(fmt.Sprintf("\n | Active | %*s | %*s | %*s |", maxNameLen, "Name", maxUsernameLen, "Username", maxInstanceURLLen, "InstanceURL"))
|
printer.Print(fmt.Sprintf("\n | Active | %*s | %*s | %*s |", maxNameLen, nameTitle, maxUsernameLen, usernameTitle, maxInstanceURLLen, instanceURLTitle))
|
||||||
printer.Print(fmt.Sprintf(" |%s|%s|%s|%s|", strings.Repeat("-", 8), strings.Repeat("-", maxNameLen+2), strings.Repeat("-", maxUsernameLen+2), strings.Repeat("-", maxInstanceURLLen+2)))
|
printer.Print(fmt.Sprintf(" |%s|%s|%s|%s|", strings.Repeat("-", 8), strings.Repeat("-", maxNameLen+2), strings.Repeat("-", maxUsernameLen+2), strings.Repeat("-", maxInstanceURLLen+2)))
|
||||||
for _, name := range serverNames {
|
for _, name := range serverNames {
|
||||||
c := (*credentialsList)[name]
|
c := (*credentialsList)[name]
|
||||||
|
88
server/cmd/mmctl/commands/auth_test.go
Normal file
88
server/cmd/mmctl/commands/auth_test.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See LICENSE.txt for license information.
|
||||||
|
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *MmctlUnitTestSuite) TestAuthList() {
|
||||||
|
originalUser := *currentUser
|
||||||
|
defer func() {
|
||||||
|
SetUser(&originalUser)
|
||||||
|
}()
|
||||||
|
|
||||||
|
tmp, errMkDir := os.MkdirTemp("", "mmctl-")
|
||||||
|
s.Require().NoError(errMkDir)
|
||||||
|
s.T().Cleanup(func() {
|
||||||
|
s.Require().NoError(os.RemoveAll(tmp))
|
||||||
|
})
|
||||||
|
|
||||||
|
testUser, err := user.Current()
|
||||||
|
s.Require().NoError(err)
|
||||||
|
testUser.HomeDir = tmp
|
||||||
|
SetUser(testUser)
|
||||||
|
viper.Set("config", filepath.Join(xdgConfigHomeVar, configParent, configFileName))
|
||||||
|
|
||||||
|
s.Run("short name", func() {
|
||||||
|
printer.Clean()
|
||||||
|
|
||||||
|
credentials := Credentials{
|
||||||
|
Name: "MyN",
|
||||||
|
Username: "Un",
|
||||||
|
AuthToken: "",
|
||||||
|
AuthMethod: "",
|
||||||
|
InstanceURL: "IURL",
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = CleanCredentials()
|
||||||
|
s.Require().NoError(err)
|
||||||
|
err = SaveCredentials(credentials)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
errListCmdF := listCmdF(&cobra.Command{}, []string{})
|
||||||
|
s.Require().NoError(errListCmdF)
|
||||||
|
s.Require().Len(printer.GetErrorLines(), 0)
|
||||||
|
lines := printer.GetLines()
|
||||||
|
s.Require().Len(lines, 4)
|
||||||
|
s.Require().Contains(lines[2], "| * | MyN | Un | IURL |")
|
||||||
|
s.Require().Contains(lines[1], "|--------|------|----------|-------------|")
|
||||||
|
s.Require().Contains(lines[0], "| Active | Name | Username | InstanceURL |")
|
||||||
|
})
|
||||||
|
|
||||||
|
s.Run("normal name", func() {
|
||||||
|
printer.Clean()
|
||||||
|
|
||||||
|
credentials := Credentials{
|
||||||
|
Name: "MyName",
|
||||||
|
Username: "MyUsername",
|
||||||
|
AuthToken: "",
|
||||||
|
AuthMethod: "",
|
||||||
|
InstanceURL: "My Instance URL",
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = CleanCredentials()
|
||||||
|
s.Require().NoError(err)
|
||||||
|
err = SaveCredentials(credentials)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
errListCmdF := listCmdF(&cobra.Command{}, []string{})
|
||||||
|
s.Require().NoError(errListCmdF)
|
||||||
|
s.Require().Len(printer.GetErrorLines(), 0)
|
||||||
|
lines := printer.GetLines()
|
||||||
|
s.Require().Len(lines, 4)
|
||||||
|
s.Require().Contains(lines[2], "| * | MyName | MyUsername | My Instance URL |")
|
||||||
|
s.Require().Contains(lines[1], "|--------|--------|------------|-----------------|")
|
||||||
|
s.Require().Contains(lines[0], "| Active | Name | Username | InstanceURL |")
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user