This repository was archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (130 loc) · 3.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"google.golang.org/api/androidpublisher/v3"
"google.golang.org/api/option"
"github.com/urfave/cli/v2"
)
func main() {
var credsFile string
var pkg string
app := &cli.App{
Name: "Android Publisher",
Usage: "Access the Android Publisher API",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cred",
Aliases: []string{"c"},
Value: "google-play-api-key.json",
Usage: "Load Google Play API Credentials from `FILE`",
Destination: &credsFile,
EnvVars: []string{"GOOGLE_PLAY_API_CREDENTIALS_FILE"},
},
&cli.StringFlag{
Name: "package",
Aliases: []string{"p"},
Usage: "App Package ID",
Destination: &pkg,
EnvVars: []string{"APB_PACKAGE"},
Required: true,
},
},
Commands: []*cli.Command{
{
Name: "trackInfo",
Usage: "Display Info about a track",
Subcommands: []*cli.Command{
{
Name: "production",
Aliases: []string{"p"},
Action: func(c *cli.Context) error {
return trackInfo(c.Context, pkg, credsFile, c.Command.Name)
},
},
{
Name: "alpha",
Aliases: []string{"a"},
Action: func(c *cli.Context) error {
return trackInfo(c.Context, pkg, credsFile, c.Command.Name)
},
},
{
Name: "beta",
Aliases: []string{"b"},
Action: func(c *cli.Context) error {
return trackInfo(c.Context, pkg, credsFile, c.Command.Name)
},
},
{
Name: "list",
Usage: "List all the tracks",
Aliases: []string{"l"},
Action: func(c *cli.Context) error {
return listTracks(c.Context, pkg, credsFile)
},
},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func buildService(ctx context.Context, pkg string, credsFile string) (*androidpublisher.Service, string, error) {
androidpublisherService, err := androidpublisher.NewService(ctx, option.WithCredentialsFile(credsFile))
if err != nil {
return nil, "", err
}
var appEdit androidpublisher.AppEdit
editCreate := androidpublisherService.Edits.Insert(pkg, &appEdit)
editCreateRsp, err := editCreate.Do()
if err != nil {
return nil, "", err
}
return androidpublisherService, editCreateRsp.Id, nil
}
func marshallAndPrint(v interface{}) error {
json, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
fmt.Println(string(json))
return nil
}
func trackInfo(ctx context.Context, pkg string, credsFile string, track string) error {
service, editId, err := buildService(ctx, pkg, credsFile)
if err != nil {
return err
}
c := service.Edits.Tracks.Get(pkg, editId, track)
rsp, err := c.Do()
if err != nil {
return err
}
if len(rsp.Releases) != 1 {
log.Fatal("Should have received one response")
}
return marshallAndPrint(rsp.Releases[0])
}
func listTracks(ctx context.Context, pkg string, credsFile string) error {
service, editId, err := buildService(ctx, pkg, credsFile)
if err != nil {
return err
}
c := service.Edits.Tracks.List(pkg, editId)
rsp, err := c.Do()
if err != nil {
return err
}
trackNames := []string{}
for _, track := range rsp.Tracks {
trackNames = append(trackNames, track.Track)
}
return marshallAndPrint(trackNames)
}