MMCTL: Add import delete cmd for removing the import files (#29764)
Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
0cf6361139
Коммит
09a2037b61
@@ -88,7 +88,6 @@ func (s *MmctlUnitTestSuite) TestExportCreateCmdF() {
|
||||
s.Equal(mockJob, printer.GetLines()[0].(*model.Job))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MmctlUnitTestSuite) TestExportDeleteCmdF() {
|
||||
printer.Clean()
|
||||
|
||||
|
||||
@@ -37,6 +37,14 @@ var ImportUploadCmd = &cobra.Command{
|
||||
RunE: withClient(importUploadCmdF),
|
||||
}
|
||||
|
||||
var ImportDeleteCmd = &cobra.Command{
|
||||
Use: "delete [importname]",
|
||||
Short: "Delete an import file",
|
||||
Example: " import delete import_file.zip",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: withClient(importDeleteCmdF),
|
||||
}
|
||||
|
||||
var ImportListCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Aliases: []string{"ls"},
|
||||
@@ -130,6 +138,7 @@ func init() {
|
||||
ImportProcessCmd,
|
||||
ImportJobCmd,
|
||||
ImportValidateCmd,
|
||||
ImportDeleteCmd,
|
||||
)
|
||||
RootCmd.AddCommand(ImportCmd)
|
||||
}
|
||||
@@ -250,6 +259,17 @@ func importUploadCmdF(c client.Client, command *cobra.Command, args []string) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func importDeleteCmdF(c client.Client, command *cobra.Command, args []string) error {
|
||||
importName := args[0]
|
||||
|
||||
if _, err := c.DeleteImport(context.TODO(), importName); err != nil {
|
||||
return fmt.Errorf("failed to delete import: %w", err)
|
||||
}
|
||||
|
||||
printer.Print(fmt.Sprintf("Import file %q has been deleted", importName))
|
||||
return nil
|
||||
}
|
||||
|
||||
func importProcessCmdF(c client.Client, command *cobra.Command, args []string) error {
|
||||
importFile := args[0]
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -430,3 +432,53 @@ func (s *MmctlE2ETestSuite) TestImportValidateCmdF() {
|
||||
s.Require().Equal("Validation complete\n", printer.GetLines()[2])
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MmctlE2ETestSuite) TestImportDeleteCmdF() {
|
||||
s.SetupTestHelper().InitBasic()
|
||||
s.Run("no permissions", func() {
|
||||
printer.Clean()
|
||||
|
||||
err := importDeleteCmdF(s.th.Client, &cobra.Command{}, []string{"import1.zip"})
|
||||
s.Require().EqualError(err, "failed to delete import: You do not have the appropriate permissions.")
|
||||
s.Require().Empty(printer.GetLines())
|
||||
s.Require().Empty(printer.GetErrorLines())
|
||||
})
|
||||
|
||||
s.RunForSystemAdminAndLocal("delete import", func(c client.Client) {
|
||||
importName := "import_test.zip"
|
||||
importFilePath := filepath.Join(server.GetPackagePath(), "tests", importName)
|
||||
importPath, err := filepath.Abs(filepath.Join(*s.th.App.Config().FileSettings.Directory,
|
||||
*s.th.App.Config().ImportSettings.Directory))
|
||||
s.Require().Nil(err)
|
||||
|
||||
cmd := &cobra.Command{}
|
||||
|
||||
newImportName := "new_import_test.zip"
|
||||
err = utils.CopyFile(importFilePath, filepath.Join(importPath, newImportName))
|
||||
s.Require().Nil(err)
|
||||
|
||||
printer.Clean()
|
||||
imports, appErr := s.th.App.ListImports()
|
||||
s.Require().Nil(appErr)
|
||||
s.Require().NotEmpty(imports)
|
||||
s.Require().Equal(newImportName, imports[0])
|
||||
|
||||
err = importDeleteCmdF(c, cmd, []string{newImportName})
|
||||
s.Require().Nil(err)
|
||||
s.Require().Empty(printer.GetErrorLines())
|
||||
s.Require().Len(printer.GetLines(), 1)
|
||||
s.Equal(fmt.Sprintf(`Import file "%s" has been deleted`, newImportName), printer.GetLines()[0])
|
||||
|
||||
imports, appErr = s.th.App.ListImports()
|
||||
s.Require().Nil(appErr)
|
||||
s.Require().Empty(imports)
|
||||
|
||||
//idempotency check
|
||||
|
||||
err = importDeleteCmdF(c, cmd, []string{newImportName})
|
||||
s.Require().Nil(err)
|
||||
s.Require().Empty(printer.GetErrorLines())
|
||||
s.Require().Len(printer.GetLines(), 2)
|
||||
s.Equal(fmt.Sprintf(`Import file "%s" has been deleted`, newImportName), printer.GetLines()[0])
|
||||
})
|
||||
}
|
||||
|
||||
@@ -536,3 +536,25 @@ func (s *MmctlUnitTestSuite) TestImportValidateCmdF() {
|
||||
s.Equal("Validation complete\n", printer.GetLines()[2])
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MmctlUnitTestSuite) TestDeleteImportCmdF() {
|
||||
s.Run("delete command succeeds", func() {
|
||||
printer.Clean()
|
||||
s.client.
|
||||
EXPECT().
|
||||
DeleteImport(context.TODO(), "import.zip").
|
||||
Return(&model.Response{}, nil).
|
||||
Times(2)
|
||||
|
||||
err := importDeleteCmdF(s.client, &cobra.Command{}, []string{"import.zip"})
|
||||
s.Require().Nil(err)
|
||||
s.Len(printer.GetLines(), 1)
|
||||
s.Equal("Import file \"import.zip\" has been deleted", printer.GetLines()[0])
|
||||
|
||||
//idempotency check
|
||||
err = importDeleteCmdF(s.client, &cobra.Command{}, []string{"import.zip"})
|
||||
s.Require().Nil(err)
|
||||
s.Len(printer.GetLines(), 2)
|
||||
s.Equal("Import file \"import.zip\" has been deleted", printer.GetLines()[1])
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user