remove the getM365 cmd (#3881)
#### Does this PR need a docs update or release note? - [x] ⛔ No #### Type of change - [x] 🧹 Tech Debt/Cleanup
This commit is contained in:
parent
916bb0b27c
commit
4ddbb1cc30
@ -1,157 +0,0 @@
|
||||
// get_item.go is a source file designed to retrieve an m365 object from an
|
||||
// existing M365 account. Data displayed is representative of the current
|
||||
// serialization abstraction versioning used by Microsoft Graph and stored by Corso.
|
||||
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/alcionai/clues"
|
||||
"github.com/microsoft/kiota-abstractions-go/serialization"
|
||||
kw "github.com/microsoft/kiota-serialization-json-go"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/alcionai/corso/src/cli/utils"
|
||||
"github.com/alcionai/corso/src/internal/common/ptr"
|
||||
"github.com/alcionai/corso/src/internal/common/str"
|
||||
"github.com/alcionai/corso/src/pkg/account"
|
||||
"github.com/alcionai/corso/src/pkg/backup/details"
|
||||
"github.com/alcionai/corso/src/pkg/credentials"
|
||||
"github.com/alcionai/corso/src/pkg/fault"
|
||||
"github.com/alcionai/corso/src/pkg/path"
|
||||
"github.com/alcionai/corso/src/pkg/services/m365/api"
|
||||
)
|
||||
|
||||
// Required inputs from user for command execution
|
||||
var (
|
||||
user, tenant, m365ID, category string
|
||||
)
|
||||
|
||||
func AddCommands(parent *cobra.Command) {
|
||||
exCmd := &cobra.Command{
|
||||
Use: "exchange",
|
||||
Short: "Get an M365ID item JSON",
|
||||
RunE: handleExchangeCmd,
|
||||
}
|
||||
|
||||
fs := exCmd.PersistentFlags()
|
||||
fs.StringVar(&m365ID, "id", "", "m365 identifier for object")
|
||||
fs.StringVar(&category, "category", "", "type of M365 data (contacts, email, events)")
|
||||
fs.StringVar(&user, "user", "", "m365 user id of M365 user")
|
||||
fs.StringVar(&tenant, "tenant", "", "m365 identifier for the tenant")
|
||||
|
||||
cobra.CheckErr(exCmd.MarkPersistentFlagRequired("user"))
|
||||
cobra.CheckErr(exCmd.MarkPersistentFlagRequired("id"))
|
||||
cobra.CheckErr(exCmd.MarkPersistentFlagRequired("category"))
|
||||
|
||||
parent.AddCommand(exCmd)
|
||||
}
|
||||
|
||||
func handleExchangeCmd(cmd *cobra.Command, args []string) error {
|
||||
if utils.HasNoFlagsAndShownHelp(cmd) {
|
||||
return nil
|
||||
}
|
||||
|
||||
tid := str.First(tenant, os.Getenv(account.AzureTenantID))
|
||||
|
||||
ctx := clues.Add(
|
||||
cmd.Context(),
|
||||
"item_id", m365ID,
|
||||
"resource_owner", user,
|
||||
"tenant", tid)
|
||||
|
||||
creds := account.M365Config{
|
||||
M365: credentials.GetM365(),
|
||||
AzureTenantID: tid,
|
||||
}
|
||||
|
||||
err := runDisplayM365JSON(ctx, creds, user, m365ID, fault.New(true))
|
||||
if err != nil {
|
||||
cmd.SilenceUsage = true
|
||||
cmd.SilenceErrors = true
|
||||
|
||||
return clues.Wrap(err, "getting item")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runDisplayM365JSON(
|
||||
ctx context.Context,
|
||||
creds account.M365Config,
|
||||
user, itemID string,
|
||||
errs *fault.Bus,
|
||||
) error {
|
||||
var (
|
||||
bs []byte
|
||||
err error
|
||||
cat = path.ToCategoryType(category)
|
||||
sw = kw.NewJsonSerializationWriter()
|
||||
)
|
||||
|
||||
ac, err := api.NewClient(creds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch cat {
|
||||
case path.EmailCategory:
|
||||
bs, err = getItem(ctx, ac.Mail(), user, itemID, true, errs)
|
||||
case path.EventsCategory:
|
||||
bs, err = getItem(ctx, ac.Events(), user, itemID, true, errs)
|
||||
case path.ContactsCategory:
|
||||
bs, err = getItem(ctx, ac.Contacts(), user, itemID, true, errs)
|
||||
default:
|
||||
return fmt.Errorf("unable to process category: %s", cat)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = sw.WriteStringValue("", ptr.To(string(bs)))
|
||||
if err != nil {
|
||||
return clues.Wrap(err, "Error writing string value: "+itemID)
|
||||
}
|
||||
|
||||
array, err := sw.GetSerializedContent()
|
||||
if err != nil {
|
||||
return clues.Wrap(err, "Error serializing item: "+itemID)
|
||||
}
|
||||
|
||||
fmt.Println(string(array))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type itemer interface {
|
||||
GetItem(
|
||||
ctx context.Context,
|
||||
user, itemID string,
|
||||
immutableID bool,
|
||||
errs *fault.Bus,
|
||||
) (serialization.Parsable, *details.ExchangeInfo, error)
|
||||
Serialize(
|
||||
ctx context.Context,
|
||||
item serialization.Parsable,
|
||||
user, itemID string,
|
||||
) ([]byte, error)
|
||||
}
|
||||
|
||||
func getItem(
|
||||
ctx context.Context,
|
||||
itm itemer,
|
||||
user, itemID string,
|
||||
immutableIDs bool,
|
||||
errs *fault.Bus,
|
||||
) ([]byte, error) {
|
||||
sp, _, err := itm.GetItem(ctx, user, itemID, immutableIDs, errs)
|
||||
if err != nil {
|
||||
return nil, clues.Wrap(err, "getting item")
|
||||
}
|
||||
|
||||
return itm.Serialize(ctx, sp, user, itemID)
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
. "github.com/alcionai/corso/src/cli/print"
|
||||
"github.com/alcionai/corso/src/cmd/getM365/exchange"
|
||||
"github.com/alcionai/corso/src/cmd/getM365/onedrive"
|
||||
"github.com/alcionai/corso/src/pkg/logger"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "getM365",
|
||||
}
|
||||
|
||||
func main() {
|
||||
ls := logger.Settings{
|
||||
Level: logger.LLDebug,
|
||||
Format: logger.LFText,
|
||||
}
|
||||
ctx, _ := logger.CtxOrSeed(context.Background(), ls)
|
||||
|
||||
ctx = SetRootCmd(ctx, rootCmd)
|
||||
defer logger.Flush(ctx)
|
||||
|
||||
exchange.AddCommands(rootCmd)
|
||||
onedrive.AddCommands(rootCmd)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
Err(ctx, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@ -1,207 +0,0 @@
|
||||
// get_item.go is a source file designed to retrieve an m365 object from an
|
||||
// existing M365 account. Data displayed is representative of the current
|
||||
// serialization abstraction versioning used by Microsoft Graph and stored by Corso.
|
||||
|
||||
package onedrive
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/alcionai/clues"
|
||||
"github.com/microsoft/kiota-abstractions-go/serialization"
|
||||
kjson "github.com/microsoft/kiota-serialization-json-go"
|
||||
"github.com/microsoftgraph/msgraph-sdk-go/models"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
. "github.com/alcionai/corso/src/cli/print"
|
||||
"github.com/alcionai/corso/src/cli/utils"
|
||||
"github.com/alcionai/corso/src/internal/common/ptr"
|
||||
"github.com/alcionai/corso/src/internal/common/str"
|
||||
"github.com/alcionai/corso/src/internal/m365/graph"
|
||||
"github.com/alcionai/corso/src/pkg/account"
|
||||
"github.com/alcionai/corso/src/pkg/credentials"
|
||||
"github.com/alcionai/corso/src/pkg/services/m365/api"
|
||||
)
|
||||
|
||||
const downloadURLKey = "@microsoft.graph.downloadUrl"
|
||||
|
||||
// Required inputs from user for command execution
|
||||
var (
|
||||
user, tenant, m365ID string
|
||||
)
|
||||
|
||||
func AddCommands(parent *cobra.Command) {
|
||||
exCmd := &cobra.Command{
|
||||
Use: "onedrive",
|
||||
Short: "Get an M365ID item",
|
||||
RunE: handleOneDriveCmd,
|
||||
}
|
||||
|
||||
fs := exCmd.PersistentFlags()
|
||||
fs.StringVar(&m365ID, "id", "", "m365 identifier for object")
|
||||
fs.StringVar(&user, "user", "", "m365 user id of M365 user")
|
||||
fs.StringVar(&tenant, "tenant", "", "m365 identifier for the tenant")
|
||||
|
||||
cobra.CheckErr(exCmd.MarkPersistentFlagRequired("user"))
|
||||
cobra.CheckErr(exCmd.MarkPersistentFlagRequired("id"))
|
||||
|
||||
parent.AddCommand(exCmd)
|
||||
}
|
||||
|
||||
func handleOneDriveCmd(cmd *cobra.Command, args []string) error {
|
||||
if utils.HasNoFlagsAndShownHelp(cmd) {
|
||||
return nil
|
||||
}
|
||||
|
||||
tid := str.First(tenant, os.Getenv(account.AzureTenantID))
|
||||
|
||||
ctx := clues.Add(
|
||||
cmd.Context(),
|
||||
"item_id", m365ID,
|
||||
"resource_owner", user,
|
||||
"tenant", tid)
|
||||
|
||||
// get account info
|
||||
creds := account.M365Config{
|
||||
M365: credentials.GetM365(),
|
||||
AzureTenantID: tid,
|
||||
}
|
||||
|
||||
gr := graph.NewNoTimeoutHTTPWrapper()
|
||||
|
||||
ac, err := api.NewClient(creds)
|
||||
if err != nil {
|
||||
return Only(ctx, clues.Wrap(err, "getting api client"))
|
||||
}
|
||||
|
||||
err = runDisplayM365JSON(ctx, ac, gr, creds, user, m365ID)
|
||||
if err != nil {
|
||||
cmd.SilenceUsage = true
|
||||
cmd.SilenceErrors = true
|
||||
|
||||
return Only(ctx, clues.Wrap(err, "getting item"))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type itemData struct {
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
type itemPrintable struct {
|
||||
Info json.RawMessage `json:"info"`
|
||||
Permissions json.RawMessage `json:"permissions"`
|
||||
Data itemData `json:"data"`
|
||||
}
|
||||
|
||||
func (i itemPrintable) MinimumPrintable() any {
|
||||
return i
|
||||
}
|
||||
|
||||
func runDisplayM365JSON(
|
||||
ctx context.Context,
|
||||
ac api.Client,
|
||||
gr graph.Requester,
|
||||
creds account.M365Config,
|
||||
userID, itemID string,
|
||||
) error {
|
||||
drive, err := ac.Users().GetDefaultDrive(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
driveID := ptr.Val(drive.GetId())
|
||||
|
||||
it := itemPrintable{}
|
||||
|
||||
item, err := ac.Drives().GetItem(ctx, driveID, itemID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if item != nil {
|
||||
content, err := getDriveItemContent(ctx, gr, item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// We could get size from item.GetSize(), but the
|
||||
// getDriveItemContent call is to ensure that we are able to
|
||||
// download the file.
|
||||
it.Data.Size = len(content)
|
||||
}
|
||||
|
||||
sInfo, err := serializeObject(item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(sInfo), &it.Info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
perms, err := ac.Drives().GetItemPermission(ctx, driveID, itemID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sPerms, err := serializeObject(perms)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(sPerms), &it.Permissions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
PrettyJSON(ctx, it)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func serializeObject(data serialization.Parsable) (string, error) {
|
||||
sw := kjson.NewJsonSerializationWriter()
|
||||
|
||||
err := sw.WriteObjectValue("", data)
|
||||
if err != nil {
|
||||
return "", clues.Wrap(err, "writing serializing info")
|
||||
}
|
||||
|
||||
content, err := sw.GetSerializedContent()
|
||||
if err != nil {
|
||||
return "", clues.Wrap(err, "getting serializing info")
|
||||
}
|
||||
|
||||
return string(content), err
|
||||
}
|
||||
|
||||
func getDriveItemContent(
|
||||
ctx context.Context,
|
||||
gr graph.Requester,
|
||||
item models.DriveItemable,
|
||||
) ([]byte, error) {
|
||||
url, ok := item.GetAdditionalData()[downloadURLKey].(*string)
|
||||
if !ok {
|
||||
return nil, clues.New("retrieving download url")
|
||||
}
|
||||
|
||||
resp, err := gr.Request(ctx, http.MethodGet, *url, nil, nil)
|
||||
if err != nil {
|
||||
return nil, clues.New("requesting item content").With("error", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
content, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, clues.New("reading item content").With("error", err)
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user