-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add output query param on serve mode & refactor output logic
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
- Loading branch information
1 parent
0f88edf
commit 9642202
Showing
5 changed files
with
88 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package analysis | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
var outputFormats = map[string]func(*Analysis) ([]byte, error){ | ||
"json": (*Analysis).jsonOutput, | ||
"text": (*Analysis).textOutput, | ||
} | ||
|
||
func getOutputFormats() []string { | ||
formats := make([]string, 0, len(outputFormats)) | ||
for format := range outputFormats { | ||
formats = append(formats, format) | ||
} | ||
return formats | ||
} | ||
|
||
func (a *Analysis) PrintOutput(format string) ([]byte, error) { | ||
outputFunc, ok := outputFormats[format] | ||
if !ok { | ||
return nil, fmt.Errorf("unsupported output format: %s. Available format %s", format, strings.Join(getOutputFormats(), ",")) | ||
} | ||
return outputFunc(a) | ||
} | ||
|
||
func (a *Analysis) jsonOutput() ([]byte, error) { | ||
var problems int | ||
var status AnalysisStatus | ||
for _, result := range a.Results { | ||
problems += len(result.Error) | ||
} | ||
if problems > 0 { | ||
status = StateProblemDetected | ||
} else { | ||
status = StateOK | ||
} | ||
|
||
result := JsonOutput{ | ||
Problems: problems, | ||
Results: a.Results, | ||
Status: status, | ||
} | ||
output, err := json.MarshalIndent(result, "", " ") | ||
if err != nil { | ||
return nil, fmt.Errorf("error marshalling json: %v", err) | ||
} | ||
return output, nil | ||
} | ||
|
||
func (a *Analysis) textOutput() ([]byte, error) { | ||
var output strings.Builder | ||
output.WriteString("\n") | ||
if len(a.Results) == 0 { | ||
output.WriteString(color.GreenString("No problems detected\n")) | ||
return []byte(output.String()), nil | ||
} | ||
for n, result := range a.Results { | ||
output.WriteString(fmt.Sprintf("%s %s(%s)\n", color.CyanString("%d", n), | ||
color.YellowString(result.Name), color.CyanString(result.ParentObject))) | ||
for _, err := range result.Error { | ||
output.WriteString(fmt.Sprintf("- %s %s\n", color.RedString("Error:"), color.RedString(err.Text))) | ||
} | ||
output.WriteString(color.GreenString(result.Details + "\n")) | ||
} | ||
return []byte(output.String()), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters