@@ -9,46 +9,83 @@ import (
9
9
10
10
"google.golang.org/api/androidpublisher/v3"
11
11
"google.golang.org/api/option"
12
+
13
+ "github.com/urfave/cli/v2"
12
14
)
13
15
14
16
func main () {
15
- args := os .Args [1 :]
16
- if len (args ) != 2 {
17
- fmt .Println ("Usage: packageId credentialsFilePath" )
18
- os .Exit (1 )
17
+ var credsFile string
18
+ var pkg string
19
+
20
+ app := & cli.App {
21
+ Name : "Android Publisher" ,
22
+ Usage : "Access the Android Publisher API" ,
23
+
24
+ Flags : []cli.Flag {
25
+ & cli.StringFlag {
26
+ Name : "cred" ,
27
+ Aliases : []string {"c" },
28
+ Value : "google-play-api-key.json" ,
29
+ Usage : "Load Google Play API Credentials from `FILE`" ,
30
+ Destination : & credsFile ,
31
+ EnvVars : []string {"GOOGLE_PLAY_API_CREDENTIALS_FILE" },
32
+ },
33
+ & cli.StringFlag {
34
+ Name : "package" ,
35
+ Aliases : []string {"p" },
36
+ Usage : "App Package ID" ,
37
+ Destination : & pkg ,
38
+ EnvVars : []string {"APB_PACKAGE" },
39
+ Required : true ,
40
+ },
41
+ },
42
+ Commands : []* cli.Command {
43
+ {
44
+ Name : "trackInfo" ,
45
+ Usage : "Display Info about a track" ,
46
+ Action : func (c * cli.Context ) error {
47
+ return trackInfo (c .Context , pkg , credsFile )
48
+ },
49
+ },
50
+ },
19
51
}
20
- pkg := args [0 ]
21
- credsFile := args [1 ]
22
52
23
- ctx := context .Background ()
24
- androidpublisherService , err := androidpublisher .NewService (ctx , option .WithCredentialsFile (credsFile ))
53
+ err := app .Run (os .Args )
25
54
if err != nil {
26
55
log .Fatal (err )
27
56
}
57
+ }
28
58
29
- var appEdit androidpublisher.AppEdit
30
- c1 := androidpublisherService .Edits .Insert (pkg , & appEdit )
31
- rsp , err := c1 .Do ()
59
+ func trackInfo (ctx context.Context , pkg string , credsFile string ) error {
60
+ androidpublisherService , err := androidpublisher .NewService (ctx , option .WithCredentialsFile (credsFile ))
32
61
if err != nil {
33
- log . Fatal ( err )
62
+ return err
34
63
}
35
64
36
- editId := rsp .Id
65
+ var appEdit androidpublisher.AppEdit
66
+ editCreate := androidpublisherService .Edits .Insert (pkg , & appEdit )
67
+ editCreateRsp , err := editCreate .Do ()
68
+ if err != nil {
69
+ return err
70
+ }
71
+ editId := editCreateRsp .Id
37
72
38
- c3 := androidpublisherService .Edits .Tracks .Get (pkg , editId , "production" )
39
- rsp3 , err := c3 .Do ()
73
+ c := androidpublisherService .Edits .Tracks .Get (pkg , editId , "production" )
74
+ rsp , err := c .Do ()
40
75
if err != nil {
41
- log . Fatal ( err )
76
+ return err
42
77
}
43
78
44
- if len (rsp3 .Releases ) != 1 {
79
+ if len (rsp .Releases ) != 1 {
45
80
log .Fatal ("Should have received one response" )
46
81
}
47
- release := rsp3 .Releases [0 ]
82
+ release := rsp .Releases [0 ]
48
83
49
84
json , err := json .MarshalIndent (release , "" , " " )
50
85
if err != nil {
51
- log . Fatal ( err )
86
+ return err
52
87
}
53
88
fmt .Println (string (json ))
89
+
90
+ return nil
54
91
}
0 commit comments