This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add export support to teamschats service layer (#5126)
more boilerplate adaptation
- Loading branch information
1 parent
7ab1276
commit 8badbdd
Showing
17 changed files
with
906 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package export | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/alcionai/corso/src/cli/flags" | ||
"github.com/alcionai/corso/src/cli/utils" | ||
"github.com/alcionai/corso/src/pkg/control" | ||
) | ||
|
||
// called by export.go to map subcommands to provider-specific handling. | ||
func addTeamsChatsCommands(cmd *cobra.Command) *cobra.Command { | ||
var c *cobra.Command | ||
|
||
switch cmd.Use { | ||
case exportCommand: | ||
c, _ = utils.AddCommand(cmd, teamschatsExportCmd(), utils.MarkPreviewCommand()) | ||
|
||
c.Use = c.Use + " " + teamschatsServiceCommandUseSuffix | ||
|
||
flags.AddBackupIDFlag(c, true) | ||
flags.AddTeamsChatsDetailsAndRestoreFlags(c) | ||
flags.AddExportConfigFlags(c) | ||
flags.AddFailFastFlag(c) | ||
} | ||
|
||
return c | ||
} | ||
|
||
const ( | ||
teamschatsServiceCommand = "chats" | ||
teamschatsServiceCommandUseSuffix = "<destination> --backup <backupId>" | ||
|
||
//nolint:lll | ||
teamschatsServiceCommandExportExamples = `# Export a specific chat from the last backup (1234abcd...) to /my-exports | ||
corso export chats my-exports --backup 1234abcd-12ab-cd34-56de-1234abcd --chat 98765abcdef | ||
# Export all of Bob's chats to the current directory | ||
corso export chats . --backup 1234abcd-12ab-cd34-56de-1234abcd \ | ||
--chat '*' | ||
# Export all chats that were created before 2020 to /my-exports | ||
corso export chats my-exports --backup 1234abcd-12ab-cd34-56de-1234abcd | ||
--chat-created-before 2020-01-01T00:00:00` | ||
) | ||
|
||
// `corso export chats [<flag>...] <destination>` | ||
func teamschatsExportCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: teamschatsServiceCommand, | ||
Aliases: []string{teamsServiceCommand}, | ||
Short: "Export M365 Chats data", | ||
RunE: exportTeamsChatsCmd, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return errors.New("missing export destination") | ||
} | ||
|
||
return nil | ||
}, | ||
Example: teamschatsServiceCommandExportExamples, | ||
} | ||
} | ||
|
||
// processes an teamschats service export. | ||
func exportTeamsChatsCmd(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
if utils.HasNoFlagsAndShownHelp(cmd) { | ||
return nil | ||
} | ||
|
||
opts := utils.MakeTeamsChatsOpts(cmd) | ||
|
||
if flags.RunModeFV == flags.RunModeFlagTest { | ||
return nil | ||
} | ||
|
||
if err := utils.ValidateTeamsChatsRestoreFlags(flags.BackupIDFV, opts, false); err != nil { | ||
return err | ||
} | ||
|
||
sel := utils.IncludeTeamsChatsRestoreDataSelectors(ctx, opts) | ||
utils.FilterTeamsChatsRestoreInfoSelectors(sel, opts) | ||
|
||
acceptedTeamsChatsFormatTypes := []string{ | ||
string(control.DefaultFormat), | ||
string(control.JSONFormat), | ||
} | ||
|
||
return runExport( | ||
ctx, | ||
cmd, | ||
args, | ||
opts.ExportCfg, | ||
sel.Selector, | ||
flags.BackupIDFV, | ||
"Chats", | ||
acceptedTeamsChatsFormatTypes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package export | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/alcionai/corso/src/cli/flags" | ||
flagsTD "github.com/alcionai/corso/src/cli/flags/testdata" | ||
cliTD "github.com/alcionai/corso/src/cli/testdata" | ||
"github.com/alcionai/corso/src/cli/utils" | ||
"github.com/alcionai/corso/src/internal/tester" | ||
) | ||
|
||
type TeamsChatsUnitSuite struct { | ||
tester.Suite | ||
} | ||
|
||
func TestTeamsChatsUnitSuite(t *testing.T) { | ||
suite.Run(t, &TeamsChatsUnitSuite{Suite: tester.NewUnitSuite(t)}) | ||
} | ||
|
||
func (suite *TeamsChatsUnitSuite) TestAddTeamsChatsCommands() { | ||
expectUse := teamschatsServiceCommand + " " + teamschatsServiceCommandUseSuffix | ||
|
||
table := []struct { | ||
name string | ||
use string | ||
expectUse string | ||
expectShort string | ||
expectRunE func(*cobra.Command, []string) error | ||
}{ | ||
{"export teamschats", exportCommand, expectUse, teamschatsExportCmd().Short, exportTeamsChatsCmd}, | ||
} | ||
for _, test := range table { | ||
suite.Run(test.name, func() { | ||
t := suite.T() | ||
parent := &cobra.Command{Use: exportCommand} | ||
|
||
cmd := cliTD.SetUpCmdHasFlags( | ||
t, | ||
parent, | ||
addTeamsChatsCommands, | ||
[]cliTD.UseCobraCommandFn{ | ||
flags.AddAllProviderFlags, | ||
flags.AddAllStorageFlags, | ||
}, | ||
flagsTD.WithFlags( | ||
teamschatsServiceCommand, | ||
[]string{ | ||
flagsTD.RestoreDestination, | ||
"--" + flags.RunModeFN, flags.RunModeFlagTest, | ||
"--" + flags.BackupFN, flagsTD.BackupInput, | ||
"--" + flags.FormatFN, flagsTD.FormatType, | ||
"--" + flags.ArchiveFN, | ||
}, | ||
flagsTD.PreparedProviderFlags(), | ||
flagsTD.PreparedStorageFlags())) | ||
|
||
cliTD.CheckCmdChild( | ||
t, | ||
parent, | ||
3, | ||
test.expectUse, | ||
test.expectShort, | ||
test.expectRunE) | ||
|
||
opts := utils.MakeTeamsChatsOpts(cmd) | ||
|
||
assert.Equal(t, flagsTD.BackupInput, flags.BackupIDFV) | ||
assert.Equal(t, flagsTD.Archive, opts.ExportCfg.Archive) | ||
assert.Equal(t, flagsTD.FormatType, opts.ExportCfg.Format) | ||
flagsTD.AssertStorageFlags(t, cmd) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.