Skip to content
This repository was archived by the owner on Aug 24, 2024. It is now read-only.

Commit 9bc11f9

Browse files
committed
Add sub-commands for trackInfo
1 parent 576b07c commit 9bc11f9

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

main.go

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,36 @@ func main() {
4343
{
4444
Name: "trackInfo",
4545
Usage: "Display Info about a track",
46-
Action: func(c *cli.Context) error {
47-
return trackInfo(c.Context, pkg, credsFile)
46+
Subcommands: []*cli.Command{
47+
{
48+
Name: "production",
49+
Aliases: []string{"p"},
50+
Action: func(c *cli.Context) error {
51+
return trackInfo(c.Context, pkg, credsFile, c.Command.Name)
52+
},
53+
},
54+
{
55+
Name: "alpha",
56+
Aliases: []string{"a"},
57+
Action: func(c *cli.Context) error {
58+
return trackInfo(c.Context, pkg, credsFile, c.Command.Name)
59+
},
60+
},
61+
{
62+
Name: "beta",
63+
Aliases: []string{"b"},
64+
Action: func(c *cli.Context) error {
65+
return trackInfo(c.Context, pkg, credsFile, c.Command.Name)
66+
},
67+
},
68+
{
69+
Name: "list",
70+
Usage: "List all the tracks",
71+
Aliases: []string{"l"},
72+
Action: func(c *cli.Context) error {
73+
return listTracks(c.Context, pkg, credsFile)
74+
},
75+
},
4876
},
4977
},
5078
},
@@ -56,21 +84,38 @@ func main() {
5684
}
5785
}
5886

59-
func trackInfo(ctx context.Context, pkg string, credsFile string) error {
87+
func buildService(ctx context.Context, pkg string, credsFile string) (*androidpublisher.Service, string, error) {
6088
androidpublisherService, err := androidpublisher.NewService(ctx, option.WithCredentialsFile(credsFile))
6189
if err != nil {
62-
return err
90+
return nil, "", err
6391
}
6492

6593
var appEdit androidpublisher.AppEdit
6694
editCreate := androidpublisherService.Edits.Insert(pkg, &appEdit)
6795
editCreateRsp, err := editCreate.Do()
96+
if err != nil {
97+
return nil, "", err
98+
}
99+
return androidpublisherService, editCreateRsp.Id, nil
100+
}
101+
102+
func marshallAndPrint(v interface{}) error {
103+
json, err := json.MarshalIndent(v, "", " ")
104+
if err != nil {
105+
return err
106+
}
107+
108+
fmt.Println(string(json))
109+
return nil
110+
}
111+
112+
func trackInfo(ctx context.Context, pkg string, credsFile string, track string) error {
113+
service, editId, err := buildService(ctx, pkg, credsFile)
68114
if err != nil {
69115
return err
70116
}
71-
editId := editCreateRsp.Id
72117

73-
c := androidpublisherService.Edits.Tracks.Get(pkg, editId, "production")
118+
c := service.Edits.Tracks.Get(pkg, editId, track)
74119
rsp, err := c.Do()
75120
if err != nil {
76121
return err
@@ -79,13 +124,24 @@ func trackInfo(ctx context.Context, pkg string, credsFile string) error {
79124
if len(rsp.Releases) != 1 {
80125
log.Fatal("Should have received one response")
81126
}
82-
release := rsp.Releases[0]
127+
return marshallAndPrint(rsp.Releases[0])
128+
}
83129

84-
json, err := json.MarshalIndent(release, "", " ")
130+
func listTracks(ctx context.Context, pkg string, credsFile string) error {
131+
service, editId, err := buildService(ctx, pkg, credsFile)
85132
if err != nil {
86133
return err
87134
}
88-
fmt.Println(string(json))
89135

90-
return nil
136+
c := service.Edits.Tracks.List(pkg, editId)
137+
rsp, err := c.Do()
138+
if err != nil {
139+
return err
140+
}
141+
142+
trackNames := []string{}
143+
for _, track := range rsp.Tracks {
144+
trackNames = append(trackNames, track.Track)
145+
}
146+
return marshallAndPrint(trackNames)
91147
}

0 commit comments

Comments
 (0)