-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathcommon_providers.go
More file actions
97 lines (82 loc) · 2.58 KB
/
Copy pathcommon_providers.go
File metadata and controls
97 lines (82 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cmd
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
"github.com/Legit-Labs/legitify/internal/common/namespace"
"github.com/Legit-Labs/legitify/internal/common/scm_type"
"github.com/Legit-Labs/legitify/internal/context_utils"
"github.com/Legit-Labs/legitify/internal/gpt"
"github.com/Legit-Labs/legitify/internal/opa"
"github.com/Legit-Labs/legitify/internal/opa/opa_engine"
"github.com/Legit-Labs/legitify/internal/outputer"
)
func provideGenericClient(args *args) (Client, error) {
switch args.ScmType {
case scm_type.GitHub:
return provideGitHubClient(args)
case scm_type.GitLab:
return provideGitLabClient(args)
default:
return nil, fmt.Errorf("invalid scm type")
}
}
func provideOutputer(ctx context.Context, analyzeArgs *args) outputer.Outputer {
return outputer.NewOutputer(ctx, analyzeArgs.OutputFormat, analyzeArgs.OutputScheme, analyzeArgs.FailedOnly)
}
func provideOpa(analyzeArgs *args) (opa_engine.Enginer, error) {
opaEngine, err := opa.Load(analyzeArgs.PoliciesPath, analyzeArgs.ScmType)
if err != nil {
return nil, err
}
return opaEngine, nil
}
func getIgnoredPolicies(args *args) []string {
var result []string
path := args.IgnoredPolicies
if path == "" {
return result
}
readFile, err := os.Open(path)
if err != nil {
log.Println(err)
return result
}
defer readFile.Close() //nolint:errcheck
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := fileScanner.Text()
line = strings.TrimSpace(line)
result = append(result, line)
}
return result
}
func provideContext(client Client, args *args) (context.Context, error) {
ctx := context.Background()
if len(args.Organizations) != 0 {
ctx = context_utils.NewContextWithOrg(args.Organizations)
} else if len(args.Repositories) != 0 {
validated, err := validateRepositories(args.Repositories)
if err != nil {
return nil, err
}
if err = repositoriesAnalyzable(client, validated); err != nil {
return nil, err
}
ctx = context_utils.NewContextWithRepos(validated)
args.Namespaces = []namespace.Namespace{namespace.Repository}
}
ctx = context_utils.NewContextWithScorecard(ctx,
IsScorecardEnabled(args.ScorecardWhen),
IsScorecardVerbose(args.ScorecardWhen))
ctx = context_utils.NewContextWithIsCloud(ctx, args.Endpoint == "")
ctx = context_utils.NewContextWithIgnoredPolicies(ctx, getIgnoredPolicies(args))
return context_utils.NewContextWithTokenScopes(ctx, client.Scopes()), nil
}
func provideGPTAnalyzer(context context.Context, args *args) *gpt.Analyzer {
return gpt.NewAnalyzer(context, args.OpenAIToken)
}