Skip to content

Commit 77698ef

Browse files
committed
move profile into pkg & new config cmd
Signed-off-by: Emily McMullan <[email protected]>
1 parent 39ff60e commit 77698ef

File tree

12 files changed

+1314
-1075
lines changed

12 files changed

+1314
-1075
lines changed

Dockerfile

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,9 @@ RUN microdnf -y install git &&\
99

1010
FROM quay.io/konveyor/static-report:${VERSION} as static-report
1111

12-
# Build the manager binary
12+
# Build the manager binary
1313
FROM golang:1.23.9 as builder
1414

15-
# install sqlite headers and cross-compilation tools for CGO
16-
RUN apt-get update && apt-get install -y \
17-
sqlite3 \
18-
libsqlite3-dev \
19-
gcc \
20-
gcc-mingw-w64 \
21-
clang \
22-
git \
23-
wget \
24-
xz-utils \
25-
make \
26-
patch \
27-
cmake \
28-
libssl-dev \
29-
lzma-dev \
30-
libxml2-dev \
31-
&& rm -rf /var/lib/apt/lists/*
32-
33-
# build OSXCross for macOS cross-compilation for sqlite3
34-
RUN git clone https://github.com/tpoechtrager/osxcross /tmp/osxcross && \
35-
cd /tmp/osxcross && \
36-
wget -O tarballs/MacOSX11.3.sdk.tar.xz \
37-
"https://github.com/joseluisq/macosx-sdks/releases/download/11.3/MacOSX11.3.sdk.tar.xz" && \
38-
UNATTENDED=yes OSX_VERSION_MIN=10.12 TARGET_DIR=/usr/local/osxcross ./build.sh && \
39-
test -f /usr/local/osxcross/bin/x86_64-apple-darwin20.4-clang && \
40-
rm -rf /tmp/osxcross
41-
42-
ENV PATH="/usr/local/osxcross/bin:$PATH"
43-
4415
WORKDIR /workspace
4516
# Copy the Go Modules manifests
4617
COPY go.mod go.mod
@@ -59,29 +30,28 @@ ARG VERSION=latest
5930
ARG BUILD_COMMIT
6031
ARG IMAGE=quay.io/konveyor/kantra
6132
ARG NAME=kantra
62-
ARG GOARCH=amd64
6333
ARG JAVA_BUNDLE=/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar
6434
ARG JAVA_PROVIDER_IMG=quay.io/konveyor/java-external-provider
6535
ARG GENERIC_PROVIDER_IMG=quay.io/konveyor/generic-external-provider
6636
ARG DOTNET_PROVIDER_IMG=quay.io/konveyor/dotnet-external-provider
6737

68-
RUN CGO_ENABLED=1 GOOS=linux GOARCH=$GOARCH go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \
38+
RUN CGO_ENABLED=0 GOOS=linux go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \
6939
-X 'github.com/konveyor-ecosystem/kantra/cmd.RunnerImage=$IMAGE' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT' \
7040
-X 'github.com/konveyor-ecosystem/kantra/cmd.JavaBundlesLocation=$JAVA_BUNDLE' -X 'github.com/konveyor-ecosystem/kantra/cmd.JavaProviderImage=$JAVA_PROVIDER_IMG' \
7141
-X 'github.com/konveyor-ecosystem/kantra/cmd.DotnetProviderImage=$DOTNET_PROVIDER_IMG' \
7242
-X 'github.com/konveyor-ecosystem/kantra/cmd.GenericProviderImage=$GENERIC_PROVIDER_IMG' -X 'github.com/konveyor-ecosystem/kantra/cmd.RootCommandName=$NAME'" -a -o kantra main.go
7343

74-
RUN CGO_ENABLED=1 GOOS=darwin GOARCH=$GOARCH CC=x86_64-apple-darwin20.4-clang CXX=x86_64-apple-darwin20.4-clang++ go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \
44+
RUN CGO_ENABLED=0 GOOS=darwin go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \
7545
-X 'github.com/konveyor-ecosystem/kantra/cmd.RunnerImage=$IMAGE' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT' \
7646
-X 'github.com/konveyor-ecosystem/kantra/cmd.JavaBundlesLocation=$JAVA_BUNDLE' -X 'github.com/konveyor-ecosystem/kantra/cmd.JavaProviderImage=$JAVA_PROVIDER_IMG' \
7747
-X 'github.com/konveyor-ecosystem/kantra/cmd.DotnetProviderImage=$DOTNET_PROVIDER_IMG' \
7848
-X 'github.com/konveyor-ecosystem/kantra/cmd.GenericProviderImage=$GENERIC_PROVIDER_IMG' -X 'github.com/konveyor-ecosystem/kantra/cmd.RootCommandName=$NAME'" -a -o darwin-kantra main.go
7949

80-
RUN CGO_ENABLED=1 GOOS=windows GOARCH=$GOARCH CC=x86_64-w64-mingw32-gcc go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \
50+
RUN CGO_ENABLED=0 GOOS=windows go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \
8151
-X 'github.com/konveyor-ecosystem/kantra/cmd.RunnerImage=$IMAGE' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT' \
8252
-X 'github.com/konveyor-ecosystem/kantra/cmd.JavaBundlesLocation=$JAVA_BUNDLE' -X 'github.com/konveyor-ecosystem/kantra/cmd.JavaProviderImage=$JAVA_PROVIDER_IMG' \
8353
-X 'github.com/konveyor-ecosystem/kantra/cmd.DotnetProviderImage=$DOTNET_PROVIDER_IMG' \
84-
-X 'github.com/konveyor-ecosystem/kantra/cmd.GenericProviderImage=$GENERIC_PROVIDER_IMG' -X 'github.com/konveyor-ecosystem/kantra/cmd.RootCommandName=$NAME'" -a -o windows-kantra.exe main.go
54+
-X 'github.com/konveyor-ecosystem/kantra/cmd.GenericProviderImage=$GENERIC_PROVIDER_IMG' -X 'github.com/konveyor-ecosystem/kantra/cmd.RootCommandName=$NAME'" -a -o windows-kantra main.go
8555

8656
FROM quay.io/konveyor/analyzer-lsp:${VERSION}
8757

@@ -105,12 +75,12 @@ RUN mkdir -p /opt/rulesets /opt/rulesets/input /opt/rulesets/convert /opt/openre
10575

10676
COPY --from=builder /workspace/kantra /usr/local/bin/kantra
10777
COPY --from=builder /workspace/darwin-kantra /usr/local/bin/darwin-kantra
108-
COPY --from=builder /workspace/windows-kantra.exe /usr/local/bin/windows-kantra.exe
78+
COPY --from=builder /workspace/windows-kantra /usr/local/bin/windows-kantra
10979
COPY --from=rulesets /rulesets/default/generated /opt/rulesets
11080
COPY --from=rulesets /windup-rulesets/rules/rules-reviewed/openrewrite /opt/openrewrite
11181
COPY --from=static-report /usr/bin/js-bundle-generator /usr/local/bin
11282
COPY --from=static-report /usr/local/static-report /usr/local/static-report
11383
COPY --chmod=755 entrypoint.sh /usr/bin/entrypoint.sh
11484
COPY --chmod=755 openrewrite_entrypoint.sh /usr/bin/openrewrite_entrypoint.sh
11585

116-
ENTRYPOINT ["kantra"]
86+
ENTRYPOINT ["kantra"]

cmd/analyze.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/go-logr/logr"
2828
"github.com/konveyor-ecosystem/kantra/cmd/internal/hiddenfile"
2929
"github.com/konveyor-ecosystem/kantra/pkg/container"
30+
"github.com/konveyor-ecosystem/kantra/pkg/profile"
3031
"github.com/konveyor-ecosystem/kantra/pkg/util"
3132
outputv1 "github.com/konveyor/analyzer-lsp/output/v1/konveyor"
3233
"github.com/konveyor/analyzer-lsp/progress"
@@ -83,7 +84,7 @@ type analyzeCommand struct {
8384
disableMavenSearch bool
8485
noProgress bool
8586
overrideProviderSettings string
86-
profile string
87+
profileDir string
8788
AnalyzeCommandContext
8889
}
8990

@@ -103,7 +104,7 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
103104
!cmd.Flags().Lookup("list-targets").Changed &&
104105
!cmd.Flags().Lookup("list-providers").Changed &&
105106
!cmd.Flags().Lookup("list-languages").Changed &&
106-
!cmd.Flags().Lookup("profile").Changed {
107+
!cmd.Flags().Lookup("profile-dir").Changed {
107108
cmd.MarkFlagRequired("input")
108109
cmd.MarkFlagRequired("output")
109110
if err := cmd.ValidateRequiredFlags(); err != nil {
@@ -148,31 +149,31 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
148149
}
149150

150151
// get profile options for analysis
151-
if analyzeCmd.profile != "" {
152-
stat, err := os.Stat(analyzeCmd.profile)
152+
if analyzeCmd.profileDir != "" {
153+
stat, err := os.Stat(analyzeCmd.profileDir)
153154
if err != nil {
154-
return fmt.Errorf("failed to stat profiles directory %s: %w", analyzeCmd.profile, err)
155+
return fmt.Errorf("failed to stat profiles directory %s: %w", analyzeCmd.profileDir, err)
155156
}
156157

157158
if !stat.IsDir() {
158-
return fmt.Errorf("found profiles path %s is not a directory", analyzeCmd.profile)
159+
return fmt.Errorf("found profiles path %s is not a directory", analyzeCmd.profileDir)
159160
}
160-
profilePath := filepath.Join(analyzeCmd.profile, "profile.yaml")
161-
err = analyzeCmd.setSettingsFromProfile(profilePath, cmd)
161+
profilePath := filepath.Join(analyzeCmd.profileDir, "profile.yaml")
162+
err = analyzeCmd.applyProfileSettings(profilePath, cmd)
162163
if err != nil {
163164
analyzeCmd.log.Error(err, "failed to get settings from profile")
164165
return err
165166
}
166167
} else {
167168
// check for a single profile in default path
168-
profilesDir := filepath.Join(analyzeCmd.input, Profiles)
169-
profilePath, err := analyzeCmd.findSingleProfile(profilesDir)
169+
profilesDir := filepath.Join(analyzeCmd.input, profile.Profiles)
170+
profilePath, err := profile.FindSingleProfile(profilesDir)
170171
if err != nil {
171172
analyzeCmd.log.Error(err, "did not find valid profile in default path")
172173
}
173174
if profilePath != "" {
174175
analyzeCmd.log.Info("using found profile", "profile", profilePath)
175-
err = analyzeCmd.setSettingsFromProfile(profilePath, cmd)
176+
err = analyzeCmd.applyProfileSettings(profilePath, cmd)
176177
if err != nil {
177178
analyzeCmd.log.Error(err, "failed to get settings from profile")
178179
return err
@@ -345,7 +346,7 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
345346
analyzeCommand.Flags().BoolVar(&analyzeCmd.disableMavenSearch, "disable-maven-search", false, "disable maven search for dependencies")
346347
analyzeCommand.Flags().BoolVar(&analyzeCmd.noProgress, "no-progress", false, "disable progress reporting (useful for scripting)")
347348
analyzeCommand.Flags().StringVar(&analyzeCmd.overrideProviderSettings, "override-provider-settings", "", "override provider settings with custom provider config file")
348-
analyzeCommand.Flags().StringVar(&analyzeCmd.profile, "profile", "", "path to an analysis profile")
349+
analyzeCommand.Flags().StringVar(&analyzeCmd.profileDir, "profile-dir", "", "path to a directory containing analysis profiles")
349350
return analyzeCommand
350351
}
351352

@@ -1616,3 +1617,32 @@ func listLanguages(languages []model.Language, input string) error {
16161617
}
16171618
return nil
16181619
}
1620+
1621+
func (a *analyzeCommand) createProfileSettings() *profile.ProfileSettings {
1622+
return &profile.ProfileSettings{
1623+
Input: a.input,
1624+
Mode: a.mode,
1625+
AnalyzeKnownLibraries: a.analyzeKnownLibraries,
1626+
IncidentSelector: a.incidentSelector,
1627+
LabelSelector: a.labelSelector,
1628+
Rules: a.rules,
1629+
EnableDefaultRulesets: a.enableDefaultRulesets,
1630+
}
1631+
}
1632+
1633+
func (a *analyzeCommand) applyProfileSettings(profilePath string, cmd *cobra.Command) error {
1634+
settings := a.createProfileSettings()
1635+
err := profile.SetSettingsFromProfile(profilePath, cmd, settings)
1636+
if err != nil {
1637+
return err
1638+
}
1639+
a.input = settings.Input
1640+
a.mode = settings.Mode
1641+
a.analyzeKnownLibraries = settings.AnalyzeKnownLibraries
1642+
a.incidentSelector = settings.IncidentSelector
1643+
a.labelSelector = settings.LabelSelector
1644+
a.rules = settings.Rules
1645+
a.enableDefaultRulesets = settings.EnableDefaultRulesets
1646+
1647+
return nil
1648+
}

cmd/config.go renamed to cmd/config/config.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package config
22

33
import (
44
"archive/tar"
@@ -15,7 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/go-logr/logr"
18-
"github.com/konveyor/tackle2-hub/api"
18+
"github.com/konveyor-ecosystem/kantra/pkg/profile"
1919
"github.com/spf13/cobra"
2020
"gopkg.in/yaml.v2"
2121
)
@@ -59,7 +59,7 @@ func NewConfigCmd(log logr.Logger) *cobra.Command {
5959
return nil
6060
},
6161
RunE: func(cmd *cobra.Command, args []string) error {
62-
if val, err := cmd.Flags().GetUint32(logLevelFlag); err == nil {
62+
if val, err := cmd.Flags().GetUint32("log-level"); err == nil {
6363
configCmd.logLevel = &val
6464
}
6565

@@ -93,7 +93,7 @@ func (c *configCommand) Validate(ctx context.Context) error {
9393
if !stat.IsDir() {
9494
return fmt.Errorf("application path for profile %s is not a directory", c.listProfiles)
9595
}
96-
profilesDir := filepath.Join(c.listProfiles, Profiles)
96+
profilesDir := filepath.Join(c.listProfiles, profile.Profiles)
9797
if _, err := os.Stat(profilesDir); os.IsNotExist(err) {
9898
return err
9999
}
@@ -108,7 +108,7 @@ func (c *configCommand) ListProfiles() error {
108108
c.log.Info("application path not provided")
109109
return nil
110110
}
111-
profilesDir := filepath.Join(c.listProfiles, Profiles)
111+
profilesDir := filepath.Join(c.listProfiles, profile.Profiles)
112112
entries, err := os.ReadDir(profilesDir)
113113
if err != nil {
114114
return err
@@ -148,7 +148,7 @@ func NewSyncCmd(log logr.Logger) *cobra.Command {
148148
return nil
149149
},
150150
RunE: func(cmd *cobra.Command, args []string) error {
151-
if val, err := cmd.Flags().GetUint32(logLevelFlag); err == nil {
151+
if val, err := cmd.Flags().GetUint32("log-level"); err == nil {
152152
syncCmd.logLevel = &val
153153
}
154154
if insecure, err := cmd.Parent().PersistentFlags().GetBool("insecure"); err == nil {
@@ -159,7 +159,7 @@ func NewSyncCmd(log logr.Logger) *cobra.Command {
159159
log.Error(err, "failed to get applications from Hub")
160160
return err
161161
}
162-
profiles, err := syncCmd.getProfilesFromHubApplicaton(int(application.Resource.ID))
162+
profiles, err := syncCmd.getProfilesFromHubApplication(int(application.ID))
163163
if err != nil {
164164
return err
165165
}
@@ -177,15 +177,18 @@ func NewSyncCmd(log logr.Logger) *cobra.Command {
177177

178178
syncCommand.Flags().StringVar(&syncCmd.url, "url", "", "url of the remote application repository")
179179
syncCommand.MarkFlagRequired("url")
180-
syncCommand.Flags().StringVar(&syncCmd.applicationPath, "application-path", "", "path to the local application to download Hub profiles")
181-
syncCommand.MarkFlagRequired("application-path")
180+
syncCommand.Flags().StringVar(&syncCmd.applicationPath, "application-path", "", "path to the local application for Hub profiles. Default is the current directory")
182181

183182
return syncCommand
184183
}
185184

186185
func (s *syncCommand) Validate(ctx context.Context) error {
187186
if s.applicationPath == "" {
188-
return fmt.Errorf("application path is required")
187+
defaultPath, err := s.setDefaultApplicationPath()
188+
if err != nil {
189+
return err
190+
}
191+
s.applicationPath = defaultPath
189192
}
190193
stat, err := os.Stat(s.applicationPath)
191194
if err != nil {
@@ -198,6 +201,14 @@ func (s *syncCommand) Validate(ctx context.Context) error {
198201
return nil
199202
}
200203

204+
func (s *syncCommand) setDefaultApplicationPath() (string, error) {
205+
currentDir, err := os.Getwd()
206+
if err != nil {
207+
return "", err
208+
}
209+
return currentDir, nil
210+
}
211+
201212
func (s *syncCommand) getHubClient() (*hubClient, error) {
202213
var err error
203214
if s.hubClient == nil {
@@ -311,46 +322,46 @@ func (s *syncCommand) checkAuthentication() error {
311322
return nil
312323
}
313324

314-
func (s *syncCommand) getApplicationFromHub(urlRepo string) (api.Application, error) {
325+
func (s *syncCommand) getApplicationFromHub(urlRepo string) (profile.Application, error) {
315326
if err := s.checkAuthentication(); err != nil {
316-
return api.Application{}, err
327+
return profile.Application{}, err
317328
}
318329
hubClient, err := s.getHubClient()
319330
if err != nil {
320-
return api.Application{}, err
331+
return profile.Application{}, err
321332
}
322333
path := fmt.Sprintf("/applications?filter=repository.url='%s'", urlRepo)
323334

324335
resp, err := hubClient.doRequest(path, "application/json", s.log)
325336
if err != nil {
326-
return api.Application{}, err
337+
return profile.Application{}, err
327338
}
328339
body, err := hubClient.readResponseBody(resp)
329340
if err != nil {
330-
return api.Application{}, err
341+
return profile.Application{}, err
331342
}
332343

333344
apps, err := parseApplicationsFromHub(string(body))
334345
if err != nil {
335-
return api.Application{}, err
346+
return profile.Application{}, err
336347
}
337348
if len(apps) == 0 {
338-
return api.Application{}, fmt.Errorf("no applications found in Hub for URL: %s", urlRepo)
349+
return profile.Application{}, fmt.Errorf("no applications found in Hub for URL: %s", urlRepo)
339350

340351
// TODO handle multiple applications later
341352
} else if len(apps) > 1 {
342-
return api.Application{}, fmt.Errorf("multiple applications found in Hub for URL: %s", urlRepo)
353+
return profile.Application{}, fmt.Errorf("multiple applications found in Hub for URL: %s", urlRepo)
343354
}
344-
var application api.Application
345-
if apps[0].Repository.URL == s.url {
355+
var application profile.Application
356+
if apps[0].Repository != nil && apps[0].Repository.URL == s.url {
346357
application = apps[0]
347358
}
348359

349360
return application, nil
350361
}
351362

352-
func parseApplicationsFromHub(jsonData string) ([]api.Application, error) {
353-
var apps []api.Application
363+
func parseApplicationsFromHub(jsonData string) ([]profile.Application, error) {
364+
var apps []profile.Application
354365

355366
err := json.Unmarshal([]byte(jsonData), &apps)
356367
if err != nil {
@@ -360,7 +371,7 @@ func parseApplicationsFromHub(jsonData string) ([]api.Application, error) {
360371
return apps, nil
361372
}
362373

363-
func (s *syncCommand) getProfilesFromHubApplicaton(appID int) ([]api.AnalysisProfile, error) {
374+
func (s *syncCommand) getProfilesFromHubApplication(appID int) ([]profile.AnalysisProfile, error) {
364375
hubClient, err := s.getHubClient()
365376
if err != nil {
366377
return nil, err
@@ -374,7 +385,7 @@ func (s *syncCommand) getProfilesFromHubApplicaton(appID int) ([]api.AnalysisPro
374385
if err != nil {
375386
return nil, err
376387
}
377-
profiles := []api.AnalysisProfile{}
388+
profiles := []profile.AnalysisProfile{}
378389
if err := yaml.Unmarshal(body, &profiles); err != nil {
379390
return nil, err
380391
}
@@ -388,8 +399,8 @@ func (s *syncCommand) downloadProfileBundle(profileID int) error {
388399
return err
389400
}
390401
filename := fmt.Sprintf("profile-%d.tar", profileID)
391-
downloadPath := filepath.Join(s.applicationPath, Profiles, filename)
392-
err = os.MkdirAll(filepath.Join(s.applicationPath, Profiles), 0755)
402+
downloadPath := filepath.Join(s.applicationPath, profile.Profiles, filename)
403+
err = os.MkdirAll(filepath.Join(s.applicationPath, profile.Profiles), 0755)
393404
if err != nil {
394405
return err
395406
}

0 commit comments

Comments
 (0)