|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/rainbend/appstoreconnect-cli/internal/api" |
| 8 | + "github.com/rainbend/appstoreconnect-cli/internal/metadata" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +var initCmd = &cobra.Command{ |
| 13 | + Use: "init", |
| 14 | + Short: "Initialize local metadata from App Store Connect", |
| 15 | + Long: `Download promotional text, description, keywords and what's new |
| 16 | +from the latest version on App Store Connect, organized by platform and locale. |
| 17 | +
|
| 18 | +The metadata is saved to: |
| 19 | + .metadata/{platform}/{locale}/description.txt |
| 20 | + .metadata/{platform}/{locale}/keywords.txt |
| 21 | + .metadata/{platform}/{locale}/promotional_text.txt |
| 22 | + .metadata/{platform}/{locale}/whats_new.txt`, |
| 23 | + RunE: runInit, |
| 24 | +} |
| 25 | + |
| 26 | +func init() { |
| 27 | + initCmd.Flags().StringP("app-id", "a", os.Getenv("ASC_APP_ID"), "App Store Connect App ID (env: ASC_APP_ID)") |
| 28 | + initCmd.Flags().StringP("platform", "p", "ios", "Platform: ios or macos") |
| 29 | + rootCmd.AddCommand(initCmd) |
| 30 | +} |
| 31 | + |
| 32 | +func runInit(cmd *cobra.Command, args []string) error { |
| 33 | + if err := validateAuthFlags(); err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + appID, _ := cmd.Flags().GetString("app-id") |
| 38 | + if appID == "" { |
| 39 | + return fmt.Errorf("--app-id or ASC_APP_ID environment variable is required") |
| 40 | + } |
| 41 | + platformStr, _ := cmd.Flags().GetString("platform") |
| 42 | + |
| 43 | + platform, err := api.ParsePlatform(platformStr) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + client := api.NewClient(keyID, issuerID, privateKeyPath) |
| 49 | + |
| 50 | + fmt.Printf("Fetching latest %s version...\n", platformStr) |
| 51 | + versions, err := client.ListAppStoreVersions(appID, platform) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("listing versions: %w", err) |
| 54 | + } |
| 55 | + if len(versions) == 0 { |
| 56 | + return fmt.Errorf("no versions found for platform %s", platformStr) |
| 57 | + } |
| 58 | + |
| 59 | + version := versions[0] |
| 60 | + fmt.Printf("Found version %s (%s)\n", version.Attributes.VersionString, version.Attributes.AppStoreState) |
| 61 | + |
| 62 | + localizations, err := client.ListVersionLocalizations(version.ID) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("listing localizations: %w", err) |
| 65 | + } |
| 66 | + if len(localizations) == 0 { |
| 67 | + return fmt.Errorf("no localizations found for version %s", version.Attributes.VersionString) |
| 68 | + } |
| 69 | + |
| 70 | + fmt.Printf("Saving %d localization(s)...\n", len(localizations)) |
| 71 | + |
| 72 | + for _, loc := range localizations { |
| 73 | + m := metadata.Localization{ |
| 74 | + Description: loc.Attributes.Description, |
| 75 | + Keywords: loc.Attributes.Keywords, |
| 76 | + PromotionalText: loc.Attributes.PromotionalText, |
| 77 | + WhatsNew: loc.Attributes.WhatsNew, |
| 78 | + } |
| 79 | + if err := metadata.Write(platformStr, loc.Attributes.Locale, m); err != nil { |
| 80 | + fmt.Fprintf(os.Stderr, "Warning: failed to write %s: %v\n", loc.Attributes.Locale, err) |
| 81 | + continue |
| 82 | + } |
| 83 | + fmt.Printf(" %s\n", loc.Attributes.Locale) |
| 84 | + } |
| 85 | + |
| 86 | + fmt.Println("Done! Metadata saved to .metadata/ directory.") |
| 87 | + return nil |
| 88 | +} |
0 commit comments