Skip to content

Commit d4ddbca

Browse files
authored
Merge pull request #36 from kyoshidajp/care_github_token_error
Care GitHub token error
2 parents cb96ffb + 821aedd commit d4ddbca

File tree

4 files changed

+124
-71
lines changed

4 files changed

+124
-71
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
.vscode/
33
.env
4+
.env.invalid
45
.envrc
56

67
cover.html

cmd/diagnose.go

+19-65
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import (
99
"strings"
1010
"sync"
1111

12-
"github.com/MakeNowJust/heredoc"
1312
"github.com/aquasecurity/go-dep-parser/pkg/io"
1413
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
1514
"github.com/aquasecurity/go-dep-parser/pkg/types"
16-
"github.com/fatih/color"
1715
dart "github.com/kyoshidajp/dep-doctor/cmd/dart/pub"
1816
erlang_elixir "github.com/kyoshidajp/dep-doctor/cmd/erlang_elixir/hex"
1917
"github.com/kyoshidajp/dep-doctor/cmd/github"
@@ -68,6 +66,15 @@ func (p RepositoryParams) SearchableParams() []github.FetchRepositoryParam {
6866
return params
6967
}
7068

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+
7178
func FetchRepositoryParams(libs []types.Library, d Doctor) RepositoryParams {
7279
var params []github.FetchRepositoryParam
7380
var wg sync.WaitGroup
@@ -157,6 +164,7 @@ func Diagnose(d Doctor, r io.ReadSeekCloserAt, year int, ignores []string) map[s
157164
Ignored: isIgnore,
158165
Diagnosed: true,
159166
IsActive: r.IsActive(year),
167+
Error: r.Error,
160168
}
161169
diagnoses[r.Name] = diagnosis
162170
}
@@ -180,6 +188,11 @@ func Diagnose(d Doctor, r io.ReadSeekCloserAt, year int, ignores []string) map[s
180188
return diagnoses
181189
}
182190

191+
func Report(diagnoses map[string]Diagnosis, strict_mode bool) error {
192+
reporter := NewStdoutReporter(diagnoses, strict_mode)
193+
return reporter.Report()
194+
}
195+
183196
type Options struct {
184197
packageManager string
185198
filePath string
@@ -231,6 +244,10 @@ var diagnoseCmd = &cobra.Command{
231244
Use: "diagnose",
232245
Short: "Diagnose dependencies",
233246
Run: func(cmd *cobra.Command, args []string) {
247+
if err := Prepare(); err != nil {
248+
log.Fatal(err)
249+
}
250+
234251
doctor, ok := doctors[o.packageManager]
235252
if !ok {
236253
m := doctors.UnknownErrorMessage(o.packageManager)
@@ -269,66 +286,3 @@ func init() {
269286
fmt.Println(err.Error())
270287
}
271288
}
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-
}

cmd/github/github.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package github
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"os"
87
"strings"
98
"time"
@@ -25,6 +24,7 @@ type GitHubRepository struct {
2524
URL string
2625
Archived bool
2726
LastCommittedAt time.Time
27+
Error error
2828
}
2929

3030
func (r GitHubRepository) IsActive(year int) bool {
@@ -102,11 +102,6 @@ func ParseGitHubURL(url string) (GitHubRepository, error) {
102102

103103
func FetchFromGitHub(params []FetchRepositoryParam) []GitHubRepository {
104104
token := os.Getenv(TOKEN_NAME)
105-
if len(token) == 0 {
106-
m := fmt.Sprintf("Environment variable `%s` is not found.", TOKEN_NAME)
107-
log.Fatal(m)
108-
}
109-
110105
src := oauth2.StaticTokenSource(
111106
&oauth2.Token{AccessToken: token},
112107
)
@@ -152,8 +147,15 @@ func FetchFromGitHub(params []FetchRepositoryParam) []GitHubRepository {
152147
}
153148

154149
repos := []GitHubRepository{}
150+
155151
err := client.Query(context.Background(), &query, variables)
156152
if err != nil {
153+
for _, param := range params {
154+
repos = append(repos, GitHubRepository{
155+
Name: param.PackageName,
156+
Error: err,
157+
})
158+
}
157159
return repos
158160
}
159161

cmd/report.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"sort"
7+
"strings"
8+
9+
"github.com/MakeNowJust/heredoc"
10+
"github.com/fatih/color"
11+
)
12+
13+
type Reporter interface {
14+
Report() error
15+
}
16+
17+
type StdoutReporter struct {
18+
diagnoses map[string]Diagnosis
19+
strict_mode bool
20+
}
21+
22+
func NewStdoutReporter(diagnosis map[string]Diagnosis, strict_mode bool) *StdoutReporter {
23+
return &StdoutReporter{
24+
diagnoses: diagnosis,
25+
strict_mode: strict_mode,
26+
}
27+
}
28+
29+
func (r *StdoutReporter) Report() error {
30+
errMessages, warnMessages, ignoredMessages := []string{}, []string{}, []string{}
31+
errCount, warnCount, infoCount := 0, 0, 0
32+
unDiagnosedCount, ignoredCount := 0, 0
33+
34+
lib_names := make([]string, 0, len(r.diagnoses))
35+
for key := range r.diagnoses {
36+
lib_names = append(lib_names, key)
37+
}
38+
sort.Strings(lib_names)
39+
40+
for _, lib_name := range lib_names {
41+
diagnosis := r.diagnoses[lib_name]
42+
if diagnosis.Ignored {
43+
ignoredMessages = append(ignoredMessages, fmt.Sprintf("[info] %s (ignored):", diagnosis.Name))
44+
ignoredCount += 1
45+
infoCount += 1
46+
continue
47+
}
48+
49+
if diagnosis.Error != nil {
50+
errMessages = append(errMessages, fmt.Sprintf("[error] %s: %s", diagnosis.Name, diagnosis.Error))
51+
errCount += 1
52+
continue
53+
}
54+
55+
if !diagnosis.Diagnosed {
56+
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (unknown): %s", diagnosis.Name, diagnosis.ErrorMessage()))
57+
unDiagnosedCount += 1
58+
warnCount += 1
59+
continue
60+
}
61+
if diagnosis.Archived {
62+
errMessages = append(errMessages, fmt.Sprintf("[error] %s (archived): %s", diagnosis.Name, diagnosis.URL))
63+
errCount += 1
64+
}
65+
if !diagnosis.IsActive {
66+
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (not-maintained): %s", diagnosis.Name, diagnosis.URL))
67+
warnCount += 1
68+
}
69+
}
70+
71+
fmt.Printf("\n")
72+
if ignoredCount > 0 {
73+
fmt.Println(strings.Join(ignoredMessages, "\n"))
74+
}
75+
if warnCount > 0 {
76+
color.Yellow(strings.Join(warnMessages, "\n"))
77+
}
78+
if errCount > 0 {
79+
color.Red(strings.Join(errMessages, "\n"))
80+
}
81+
82+
color.Green(heredoc.Docf(`
83+
Diagnosis completed! %d libraries.
84+
%d error, %d warn (%d unknown), %d info (%d ignored)`,
85+
len(r.diagnoses),
86+
errCount,
87+
warnCount, unDiagnosedCount,
88+
infoCount, ignoredCount),
89+
)
90+
91+
if errCount > 0 || r.strict_mode && warnCount > 0 {
92+
return errors.New("has error")
93+
}
94+
95+
return nil
96+
}

0 commit comments

Comments
 (0)