@@ -18,9 +18,9 @@ type CLI struct {
1818 Stderr io.Writer
1919 Option Option
2020
21- Config Config
22- Remote Config
23- Client * github.Client
21+ Defined Config
22+ Actual Config
23+ GitHub * github.Client
2424}
2525
2626type Option struct {
@@ -40,13 +40,13 @@ func (c *CLI) Run(args []string) error {
4040 if err != nil {
4141 return err
4242 }
43- c .Config = cfg
43+ c .Defined = cfg
4444
4545 client , err := github .NewClient (c .Option .DryRun )
4646 if err != nil {
4747 return err
4848 }
49- c .Client = client
49+ c .GitHub = client
5050
5151 if err := c .Validate (); err != nil {
5252 // fmt.Fprintf(c.Stderr, "Note: %s\n", err)
@@ -58,44 +58,36 @@ func (c *CLI) Run(args []string) error {
5858 return c .Import ()
5959 }
6060
61- eg := errgroup.Group {}
62- for _ , repo := range c .Config .Repos {
63- repo := repo
64- eg .Go (func () error {
65- return c .Sync (repo )
66- })
67- }
68-
69- return eg .Wait ()
61+ return c .Sync ()
7062}
7163
7264// applyLabels creates/edits labels described in YAML
7365func (c * CLI ) applyLabels (owner , repo string , label github.Label ) error {
74- ghLabel , err := c .Client .GetLabel (owner , repo , label )
66+ ghLabel , err := c .GitHub .GetLabel (owner , repo , label )
7567 if err != nil {
76- return c .Client .CreateLabel (owner , repo , label )
68+ return c .GitHub .CreateLabel (owner , repo , label )
7769 }
7870
7971 if ghLabel .Description != label .Description || ghLabel .Color != label .Color {
80- return c .Client .EditLabel (owner , repo , label )
72+ return c .GitHub .EditLabel (owner , repo , label )
8173 }
8274
8375 return nil
8476}
8577
8678// deleteLabels deletes the label not described in YAML but exists on GitHub
8779func (c * CLI ) deleteLabels (owner , repo string ) error {
88- labels , err := c .Client .ListLabels (owner , repo )
80+ labels , err := c .GitHub .ListLabels (owner , repo )
8981 if err != nil {
9082 return err
9183 }
9284
9385 for _ , label := range labels {
94- if c .Config .checkIfRepoHasLabel (owner + "/" + repo , label .Name ) {
86+ if c .Defined .checkIfRepoHasLabel (owner + "/" + repo , label .Name ) {
9587 // no need to delete
9688 continue
9789 }
98- err := c .Client .DeleteLabel (owner , repo , label )
90+ err := c .GitHub .DeleteLabel (owner , repo , label )
9991 if err != nil {
10092 return err
10193 }
@@ -104,14 +96,13 @@ func (c *CLI) deleteLabels(owner, repo string) error {
10496 return nil
10597}
10698
107- // Sync syncs labels based on YAML
108- func (c * CLI ) Sync (repo github.Repo ) error {
99+ func (c * CLI ) syncLabels (repo github.Repo ) error {
109100 slugs := strings .Split (repo .Name , "/" )
110101 if len (slugs ) != 2 {
111102 return fmt .Errorf ("repository name %q is invalid" , repo .Name )
112103 }
113104 for _ , labelName := range repo .Labels {
114- label , err := c .Config .getDefinedLabel (labelName )
105+ label , err := c .Defined .getDefinedLabel (labelName )
115106 if err != nil {
116107 return err
117108 }
@@ -124,18 +115,18 @@ func (c *CLI) Sync(repo github.Repo) error {
124115}
125116
126117func (c * CLI ) Validate () error {
127- if len (c .Config .Repos ) == 0 {
118+ if len (c .Defined .Repos ) == 0 {
128119 return fmt .Errorf ("no repos found in %s" , c .Option .Config )
129120 }
130121
131122 var cfg Config
132- for _ , repo := range c .Config .Repos {
123+ for _ , repo := range c .Defined .Repos {
133124 slugs := strings .Split (repo .Name , "/" )
134125 if len (slugs ) != 2 {
135126 // TODO: log
136127 continue
137128 }
138- labels , err := c .Client .ListLabels (slugs [0 ], slugs [1 ])
129+ labels , err := c .GitHub .ListLabels (slugs [0 ], slugs [1 ])
139130 if err != nil {
140131 // TODO: log
141132 continue
@@ -150,9 +141,9 @@ func (c *CLI) Validate() error {
150141 }
151142
152143 // used for Import func
153- c .Remote = cfg
144+ c .Actual = cfg
154145
155- if cmp .Equal (cfg , c .Config ) {
146+ if cmp .Equal (cfg , c .Defined ) {
156147 return errors .New ("existing labels and defined labels are the same" )
157148 }
158149
@@ -165,5 +156,17 @@ func (c *CLI) Import() error {
165156 return err
166157 }
167158 defer f .Close ()
168- return yaml .NewEncoder (f ).Encode (& c .Remote )
159+ return yaml .NewEncoder (f ).Encode (& c .Actual )
160+ }
161+
162+ // Sync syncs labels based on YAML
163+ func (c * CLI ) Sync () error {
164+ var eg errgroup.Group
165+ for _ , repo := range c .Defined .Repos {
166+ repo := repo
167+ eg .Go (func () error {
168+ return c .syncLabels (repo )
169+ })
170+ }
171+ return eg .Wait ()
169172}
0 commit comments