|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "time" |
| 9 | + |
| 10 | + msapps "github.com/deploymenttheory/go-api-sdk-apple/microsoft_mac_apps_version_tracker" |
| 11 | + "github.com/deploymenttheory/go-api-sdk-apple/microsoft_mac_apps_version_tracker/services/apps" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + fmt.Println("=== Microsoft Mac Apps - Get App By Name Example ===") |
| 16 | + fmt.Println() |
| 17 | + |
| 18 | + // Create a new client with default configuration |
| 19 | + client, err := msapps.NewClient(nil) |
| 20 | + if err != nil { |
| 21 | + log.Fatalf("Failed to create client: %v", err) |
| 22 | + } |
| 23 | + defer client.Close() |
| 24 | + |
| 25 | + ctx := context.Background() |
| 26 | + |
| 27 | + // Example 1: Find Microsoft Teams by name |
| 28 | + fmt.Println("=== Example 1: Find Microsoft Teams ===") |
| 29 | + |
| 30 | + teams, err := client.Apps.GetAppByName(ctx, apps.AppNameTeams) |
| 31 | + if err != nil { |
| 32 | + log.Fatalf("Error finding Teams: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + fmt.Printf("Found: %s\n", teams.Name) |
| 36 | + fmt.Printf("Bundle ID: %s\n", teams.BundleID) |
| 37 | + fmt.Printf("Version: %s\n", teams.Version) |
| 38 | + fmt.Printf("Size: %.2f MB (%.2f GB)\n", teams.SizeMB, teams.SizeMB/1024) |
| 39 | + fmt.Printf("Package ID: %s\n", teams.PackageID) |
| 40 | + fmt.Printf("Download URL: %s\n", teams.DownloadURL) |
| 41 | + fmt.Printf("Direct URL: %s\n", teams.DirectURL) |
| 42 | + |
| 43 | + // Display components |
| 44 | + if len(teams.Components) > 0 { |
| 45 | + fmt.Printf("\nBundled Components (%d):\n", teams.ComponentCount) |
| 46 | + for _, comp := range teams.Components { |
| 47 | + fmt.Printf(" - %s (v%s)\n", comp.Name, comp.Version) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Display timestamps |
| 52 | + detected, err := teams.ParseDetectedTime() |
| 53 | + if err == nil { |
| 54 | + fmt.Printf("\nDetected: %s\n", detected.Format("2006-01-02 15:04:05")) |
| 55 | + } |
| 56 | + |
| 57 | + lastModified, err := teams.ParseLastModifiedTime() |
| 58 | + if err == nil { |
| 59 | + fmt.Printf("Last Modified: %s\n", lastModified.Format("2006-01-02 15:04:05")) |
| 60 | + } |
| 61 | + |
| 62 | + // Example 2: Find Microsoft Defender |
| 63 | + fmt.Println("\n=== Example 2: Find Defender for Mac ===") |
| 64 | + |
| 65 | + defender, err := client.Apps.GetAppByName(ctx, apps.AppNameDefender) |
| 66 | + if err != nil { |
| 67 | + log.Printf("Error finding Defender: %v", err) |
| 68 | + } else { |
| 69 | + fmt.Printf("Found: %s\n", defender.Name) |
| 70 | + fmt.Printf("Version: %s\n", defender.Version) |
| 71 | + fmt.Printf("Bundle ID: %s\n", defender.BundleID) |
| 72 | + fmt.Printf("Size: %.2f MB\n", defender.SizeMB) |
| 73 | + |
| 74 | + // Show DLP components |
| 75 | + if len(defender.Components) > 0 { |
| 76 | + fmt.Printf("\nDefender includes %d components:\n", len(defender.Components)) |
| 77 | + for _, comp := range defender.Components { |
| 78 | + fmt.Printf(" - %s (v%s)\n", comp.Name, comp.Version) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Example 3: Find Office 365 Suite |
| 84 | + fmt.Println("\n=== Example 3: Find Office 365 for Mac ===") |
| 85 | + |
| 86 | + office, err := client.Apps.GetAppByName(ctx, apps.AppNameOffice365) |
| 87 | + if err != nil { |
| 88 | + log.Printf("Error finding Office 365: %v", err) |
| 89 | + } else { |
| 90 | + fmt.Printf("Found: %s\n", office.Name) |
| 91 | + fmt.Printf("Version: %s\n", office.Version) |
| 92 | + fmt.Printf("Size: %.2f GB\n", office.SizeMB/1024) |
| 93 | + fmt.Printf("\nOffice 365 Suite includes:\n") |
| 94 | + for _, comp := range office.Components { |
| 95 | + if comp.BundleID != nil { |
| 96 | + fmt.Printf(" - %s (v%s)\n", comp.Name, comp.Version) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Example 4: Check for multiple apps by name |
| 102 | + fmt.Println("\n=== Example 4: Check Multiple Applications ===") |
| 103 | + |
| 104 | + appNames := []string{ |
| 105 | + apps.AppNameExcel, |
| 106 | + apps.AppNameWord, |
| 107 | + apps.AppNamePowerPoint, |
| 108 | + apps.AppNameOutlook, |
| 109 | + apps.AppNameOneNote, |
| 110 | + apps.AppNameTeams, |
| 111 | + apps.AppNameOneDrive, |
| 112 | + apps.AppNameEdge, |
| 113 | + apps.AppNameVSCode, |
| 114 | + } |
| 115 | + |
| 116 | + fmt.Println("Checking for Microsoft applications:") |
| 117 | + for _, appName := range appNames { |
| 118 | + app, err := client.Apps.GetAppByName(ctx, appName) |
| 119 | + if err != nil { |
| 120 | + fmt.Printf(" ✗ %s: Not found\n", appName) |
| 121 | + } else { |
| 122 | + fmt.Printf(" ✓ %s: v%s\n", app.Name, app.Version) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Example 5: Compare versions and timestamps |
| 127 | + fmt.Println("\n=== Example 5: Version and Update Analysis ===") |
| 128 | + |
| 129 | + allApps, err := client.Apps.GetLatestApps(ctx) |
| 130 | + if err != nil { |
| 131 | + log.Printf("Error getting all apps: %v", err) |
| 132 | + } else { |
| 133 | + fmt.Println("Recent updates (within last 30 days):") |
| 134 | + thirtyDaysAgo := time.Now().AddDate(0, 0, -30) |
| 135 | + |
| 136 | + for _, app := range allApps.Apps { |
| 137 | + detected, err := app.ParseDetectedTime() |
| 138 | + if err == nil && detected.After(thirtyDaysAgo) { |
| 139 | + fmt.Printf(" - %s (v%s) - detected %s\n", |
| 140 | + app.Name, |
| 141 | + app.Version, |
| 142 | + detected.Format("2006-01-02")) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // Example 6: Generate deployment script |
| 148 | + fmt.Println("\n=== Example 6: Generate Download Script for Specific Apps ===") |
| 149 | + |
| 150 | + targetApps := []string{ |
| 151 | + apps.AppNameExcel, |
| 152 | + apps.AppNameWord, |
| 153 | + apps.AppNamePowerPoint, |
| 154 | + apps.AppNameTeams, |
| 155 | + } |
| 156 | + |
| 157 | + fmt.Println("#!/bin/bash") |
| 158 | + fmt.Println("# Microsoft Mac Apps Download Script") |
| 159 | + fmt.Println("# Generated:", time.Now().Format("2006-01-02 15:04:05")) |
| 160 | + fmt.Println() |
| 161 | + |
| 162 | + for _, appName := range targetApps { |
| 163 | + app, err := client.Apps.GetAppByName(ctx, appName) |
| 164 | + if err != nil { |
| 165 | + fmt.Printf("# Error: %s not found\n", appName) |
| 166 | + continue |
| 167 | + } |
| 168 | + |
| 169 | + fmt.Printf("# %s v%s (%.2f MB)\n", app.Name, app.Version, app.SizeMB) |
| 170 | + fmt.Printf("curl -L '%s' -o '%s.pkg'\n", app.DownloadURL, app.PackageID) |
| 171 | + fmt.Printf("# SHA256: %s\n", app.SHA256) |
| 172 | + fmt.Println() |
| 173 | + } |
| 174 | + |
| 175 | + // Example 7: Try to find a non-existent app (error handling) |
| 176 | + fmt.Println("=== Example 7: Error Handling ===") |
| 177 | + |
| 178 | + _, err = client.Apps.GetAppByName(ctx, "Non-existent Application") |
| 179 | + if err != nil { |
| 180 | + fmt.Printf("Expected error: %v\n", err) |
| 181 | + } |
| 182 | + |
| 183 | + // Example 8: Pretty print full JSON response |
| 184 | + fmt.Println("\n=== Example 8: Full JSON Response ===") |
| 185 | + |
| 186 | + jsonData, err := json.MarshalIndent(teams, "", " ") |
| 187 | + if err != nil { |
| 188 | + log.Printf("Error marshaling response to JSON: %v", err) |
| 189 | + } else { |
| 190 | + fmt.Println(string(jsonData)) |
| 191 | + } |
| 192 | + |
| 193 | + fmt.Println("\n=== Example Complete ===") |
| 194 | +} |
0 commit comments