Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON output option #69

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type analyzeCommand struct {
listTargets bool
skipStaticReport bool
analyzeKnownLibraries bool
jsonOutput bool
mavenSettingsFile string
sources []string
targets []string
Expand Down Expand Up @@ -106,6 +107,11 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
log.Error(err, "failed to run analysis")
return err
}
err = analyzeCmd.CreateJSONOutput()
if err != nil {
log.Error(err, "failed to create json output file")
return err
}
err = analyzeCmd.GenerateStaticReport(cmd.Context())
if err != nil {
log.Error(err, "failed to generate static report")
Expand Down Expand Up @@ -136,6 +142,7 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
analyzeCommand.Flags().BoolVar(&analyzeCmd.analyzeKnownLibraries, "analyze-known-libraries", false, "analyze known open-source libraries")
analyzeCommand.Flags().StringVar(&analyzeCmd.mavenSettingsFile, "maven-settings", "", "path to a custom maven settings file to use")
analyzeCommand.Flags().StringVarP(&analyzeCmd.mode, "mode", "m", string(provider.FullAnalysisMode), "analysis mode. Must be one of 'full' or 'source-only'")
analyzeCommand.Flags().BoolVar(&analyzeCmd.jsonOutput, "json-output", false, "create analysis and dependency output as json")

return analyzeCommand
}
Expand Down Expand Up @@ -580,6 +587,60 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string) e
return nil
}

func (a *analyzeCommand) CreateJSONOutput() error {
if !a.jsonOutput {
return nil
}
outputPath := filepath.Join(a.output, "output.yaml")
depPath := filepath.Join(a.output, "dependencies.yaml")

data, err := ioutil.ReadFile(outputPath)
if err != nil {
return err
}
ruleOutput := &[]outputv1.RuleSet{}
err = yaml.Unmarshal(data, ruleOutput)
if err != nil {
a.log.V(1).Error(err, "failed to unmarshal output yaml")
return err
}

jsonData, err := json.MarshalIndent(ruleOutput, "", " ")
if err != nil {
a.log.V(1).Error(err, "failed to marshal output file to json")
return err
}
err = ioutil.WriteFile(filepath.Join(a.output, "output.json"), jsonData, os.ModePerm)
if err != nil {
a.log.V(1).Error(err, "failed to write json output", "dir", a.output, "file", "output.json")
return err
}

depData, err := ioutil.ReadFile(depPath)
if err != nil {
return err
}
depOutput := &[]outputv1.DepsFlatItem{}
err = yaml.Unmarshal(depData, depOutput)
if err != nil {
a.log.V(1).Error(err, "failed to unmarshal dependencies yaml")
return err
}

jsonDataDep, err := json.MarshalIndent(depOutput, "", " ")
if err != nil {
a.log.V(1).Error(err, "failed to marshal dependencies file to json")
return err
}
err = ioutil.WriteFile(filepath.Join(a.output, "dependencies.json"), jsonDataDep, os.ModePerm)
if err != nil {
a.log.V(1).Error(err, "failed to write json dependencies output", "dir", a.output, "file", "dependencies.json")
return err
}

return nil
}

func (a *analyzeCommand) GenerateStaticReport(ctx context.Context) error {
if a.skipStaticReport {
return nil
Expand Down