@@ -9,11 +9,9 @@ import (
9
9
"strings"
10
10
"sync"
11
11
12
- "github.com/MakeNowJust/heredoc"
13
12
"github.com/aquasecurity/go-dep-parser/pkg/io"
14
13
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
15
14
"github.com/aquasecurity/go-dep-parser/pkg/types"
16
- "github.com/fatih/color"
17
15
dart "github.com/kyoshidajp/dep-doctor/cmd/dart/pub"
18
16
erlang_elixir "github.com/kyoshidajp/dep-doctor/cmd/erlang_elixir/hex"
19
17
"github.com/kyoshidajp/dep-doctor/cmd/github"
@@ -68,6 +66,15 @@ func (p RepositoryParams) SearchableParams() []github.FetchRepositoryParam {
68
66
return params
69
67
}
70
68
69
+ func Prepare () error {
70
+ token := os .Getenv (github .TOKEN_NAME )
71
+ if len (token ) == 0 {
72
+ m := fmt .Sprintf ("The Environment variable `%s` is not set. It must be set before execution. For example, please refer to https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens" , github .TOKEN_NAME )
73
+ return errors .New (m )
74
+ }
75
+ return nil
76
+ }
77
+
71
78
func FetchRepositoryParams (libs []types.Library , d Doctor ) RepositoryParams {
72
79
var params []github.FetchRepositoryParam
73
80
var wg sync.WaitGroup
@@ -157,6 +164,7 @@ func Diagnose(d Doctor, r io.ReadSeekCloserAt, year int, ignores []string) map[s
157
164
Ignored : isIgnore ,
158
165
Diagnosed : true ,
159
166
IsActive : r .IsActive (year ),
167
+ Error : r .Error ,
160
168
}
161
169
diagnoses [r .Name ] = diagnosis
162
170
}
@@ -180,6 +188,11 @@ func Diagnose(d Doctor, r io.ReadSeekCloserAt, year int, ignores []string) map[s
180
188
return diagnoses
181
189
}
182
190
191
+ func Report (diagnoses map [string ]Diagnosis , strict_mode bool ) error {
192
+ reporter := NewStdoutReporter (diagnoses , strict_mode )
193
+ return reporter .Report ()
194
+ }
195
+
183
196
type Options struct {
184
197
packageManager string
185
198
filePath string
@@ -231,6 +244,10 @@ var diagnoseCmd = &cobra.Command{
231
244
Use : "diagnose" ,
232
245
Short : "Diagnose dependencies" ,
233
246
Run : func (cmd * cobra.Command , args []string ) {
247
+ if err := Prepare (); err != nil {
248
+ log .Fatal (err )
249
+ }
250
+
234
251
doctor , ok := doctors [o .packageManager ]
235
252
if ! ok {
236
253
m := doctors .UnknownErrorMessage (o .packageManager )
@@ -269,66 +286,3 @@ func init() {
269
286
fmt .Println (err .Error ())
270
287
}
271
288
}
272
-
273
- func Report (diagnoses map [string ]Diagnosis , strict_mode bool ) error {
274
- errMessages , warnMessages , ignoredMessages := []string {}, []string {}, []string {}
275
- errCount , warnCount , infoCount := 0 , 0 , 0
276
- unDiagnosedCount , ignoredCount := 0 , 0
277
-
278
- lib_names := make ([]string , 0 , len (diagnoses ))
279
- for key := range diagnoses {
280
- lib_names = append (lib_names , key )
281
- }
282
- sort .Strings (lib_names )
283
-
284
- for _ , lib_name := range lib_names {
285
- diagnosis := diagnoses [lib_name ]
286
- if diagnosis .Ignored {
287
- ignoredMessages = append (ignoredMessages , fmt .Sprintf ("[info] %s (ignored):" , diagnosis .Name ))
288
- ignoredCount += 1
289
- infoCount += 1
290
- continue
291
- }
292
-
293
- if ! diagnosis .Diagnosed {
294
- warnMessages = append (warnMessages , fmt .Sprintf ("[warn] %s (unknown): %s" , diagnosis .Name , diagnosis .ErrorMessage ()))
295
- unDiagnosedCount += 1
296
- warnCount += 1
297
- continue
298
- }
299
- if diagnosis .Archived {
300
- errMessages = append (errMessages , fmt .Sprintf ("[error] %s (archived): %s" , diagnosis .Name , diagnosis .URL ))
301
- errCount += 1
302
- }
303
- if ! diagnosis .IsActive {
304
- warnMessages = append (warnMessages , fmt .Sprintf ("[warn] %s (not-maintained): %s" , diagnosis .Name , diagnosis .URL ))
305
- warnCount += 1
306
- }
307
- }
308
-
309
- fmt .Printf ("\n " )
310
- if len (ignoredMessages ) > 0 {
311
- fmt .Println (strings .Join (ignoredMessages , "\n " ))
312
- }
313
- if len (warnMessages ) > 0 {
314
- color .Yellow (strings .Join (warnMessages , "\n " ))
315
- }
316
- if len (errMessages ) > 0 {
317
- color .Red (strings .Join (errMessages , "\n " ))
318
- }
319
-
320
- color .Green (heredoc .Docf (`
321
- Diagnosis completed! %d libraries.
322
- %d error, %d warn (%d unknown), %d info (%d ignored)` ,
323
- len (diagnoses ),
324
- errCount ,
325
- warnCount , unDiagnosedCount ,
326
- infoCount , ignoredCount ),
327
- )
328
-
329
- if len (errMessages ) > 0 || strict_mode && warnCount > 0 {
330
- return errors .New ("has error" )
331
- }
332
-
333
- return nil
334
- }
0 commit comments