Skip to content

Commit

Permalink
Merge pull request #2 from ADorigi/fix-run-jobs
Browse files Browse the repository at this point in the history
fix: add table output for run jobs
  • Loading branch information
artaasadi authored Sep 15, 2024
2 parents f0cffb1 + e616a9c commit 691c315
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 15 deletions.
12 changes: 8 additions & 4 deletions cmd/get/benchmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package get
import (
"encoding/json"
"fmt"
"github.com/adorigi/opengovernance/pkg/output/tables"
"io"
"net/http"

"github.com/adorigi/opengovernance/pkg/config"
"github.com/adorigi/opengovernance/pkg/output"
"github.com/adorigi/opengovernance/pkg/request"
"github.com/adorigi/opengovernance/pkg/types"
"github.com/adorigi/opengovernance/pkg/utils"
Expand All @@ -28,13 +28,17 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: func(cmd *cobra.Command, args []string) error {

client := &http.Client{}
configuration, err := config.ReadConfigFile()
if err != nil {
return err
}

outputFormat := utils.ReadStringFlag(cmd, "output")
if outputFormat == "" {
outputFormat = configuration.OutputFormat
}

requestPayload := types.RequestPayload{
Cursor: 1,
PerPage: int(utils.ReadIntFlag(cmd, "page-size")),
Expand Down Expand Up @@ -73,10 +77,10 @@ to quickly create a Cobra application.`,
return err
}

if configuration.OutputFormat == "table" {
if outputFormat == "table" {
rows := utils.GenerateBenchmarkRows(benchmarks)

output.PrintBenchmarksTable(rows)
tables.PrintBenchmarksTable(rows)
} else {
js, err := json.MarshalIndent(benchmarks, "", " ")
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions cmd/get/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package get
import (
"encoding/json"
"fmt"
"github.com/adorigi/opengovernance/pkg/output/tables"
"io"
"net/http"

"github.com/adorigi/opengovernance/pkg/config"
"github.com/adorigi/opengovernance/pkg/output"
"github.com/adorigi/opengovernance/pkg/request"
"github.com/adorigi/opengovernance/pkg/types"
"github.com/adorigi/opengovernance/pkg/utils"
Expand All @@ -28,13 +28,17 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: func(cmd *cobra.Command, args []string) error {

client := &http.Client{}
configuration, err := config.ReadConfigFile()
if err != nil {
return err
}

outputFormat := utils.ReadStringFlag(cmd, "output")
if outputFormat == "" {
outputFormat = configuration.OutputFormat
}

requestPayload := types.RequestPayload{
Cursor: 1,
PerPage: int(utils.ReadIntFlag(cmd, "page-size")),
Expand Down Expand Up @@ -73,10 +77,10 @@ to quickly create a Cobra application.`,
return err
}

if configuration.OutputFormat == "table" {
if outputFormat == "table" {
rows := utils.GenerateControlRows(controls)

output.PrintControlsTable(rows)
tables.PrintControlsTable(rows)
} else {
js, err := json.MarshalIndent(controls, "", " ")
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func init() {
rootCmd.AddCommand(configureCmd)
rootCmd.AddCommand(get.GetCmd)
rootCmd.AddCommand(run.RunCmd)

rootCmd.PersistentFlags().String("output", "", "Output format: json/table")
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
Expand Down
16 changes: 13 additions & 3 deletions cmd/run/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package run
import (
"encoding/json"
"fmt"
"github.com/adorigi/opengovernance/pkg/output/tables"
"github.com/adorigi/opengovernance/pkg/request"
"io"
"net/http"
Expand All @@ -19,13 +20,17 @@ var benchmarkCmd = &cobra.Command{
Short: "Run specified benchmark on given integrations",
Long: `Run specified benchmark on given integrations.`,
RunE: func(cmd *cobra.Command, args []string) error {

client := &http.Client{}
configuration, err := config.ReadConfigFile()
if err != nil {
return err
}

outputFormat := utils.ReadStringFlag(cmd, "output")
if outputFormat == "" {
outputFormat = configuration.OutputFormat
}

integrationsStr, err := utils.ReadStringArrayFlag(cmd, "integration-info")
if err != nil {
return err
Expand Down Expand Up @@ -79,8 +84,13 @@ var benchmarkCmd = &cobra.Command{
return err
}

if configuration.OutputFormat == "table" {
// TODO
if outputFormat == "table" {
rows, err := utils.GenerateComplianceJobsRows(runBenchmarkResponse)
if err != nil {
return err
}

tables.PrintComplianceJobTable(rows)
} else {
js, err := json.MarshalIndent(runBenchmarkResponse, "", " ")
if err != nil {
Expand Down
16 changes: 13 additions & 3 deletions cmd/run/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package run
import (
"encoding/json"
"fmt"
"github.com/adorigi/opengovernance/pkg/output/tables"
"github.com/adorigi/opengovernance/pkg/request"
"io"
"net/http"
Expand All @@ -19,13 +20,17 @@ var discoveryCmd = &cobra.Command{
Short: "Run specified benchmark on given integrations",
Long: `Run specified benchmark on given integrations.`,
RunE: func(cmd *cobra.Command, args []string) error {

client := &http.Client{}
configuration, err := config.ReadConfigFile()
if err != nil {
return err
}

outputFormat := utils.ReadStringFlag(cmd, "output")
if outputFormat == "" {
outputFormat = configuration.OutputFormat
}

integrationsStr, err := utils.ReadStringArrayFlag(cmd, "integration-info")
if err != nil {
return err
Expand Down Expand Up @@ -80,8 +85,13 @@ var discoveryCmd = &cobra.Command{
return err
}

if configuration.OutputFormat == "table" {
// TODO
if outputFormat == "table" {
rows, err := utils.GenerateDiscoveryJobsRows(runDiscoveryResponse)
if err != nil {
return err
}

tables.PrintDiscoveryJobsTable(rows)
} else {
js, err := json.MarshalIndent(runDiscoveryResponse, "", " ")
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package output
package tables

import (
"fmt"
Expand Down
94 changes: 94 additions & 0 deletions pkg/output/tables/schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package tables

import (
"fmt"
"os"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
)

func PrintDiscoveryJobsTable(rows [][]string) {

re := lipgloss.NewRenderer(os.Stdout)

var (
HeaderStyle = re.NewStyle().Foreground(tableColor).Bold(true).Align(lipgloss.Center)
CellStyle = re.NewStyle().Padding(0, 1)
OddRowStyle = CellStyle.Foreground(oddRowColor)
EvenRowStyle = CellStyle.Foreground(evenRowColor)
)

t := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
StyleFunc(func(row, col int) lipgloss.Style {
var style lipgloss.Style
switch {
case row == 0:
style = HeaderStyle
case row%2 == 0:
style = EvenRowStyle
default:
style = OddRowStyle
}

switch {
case col == 0:
style.Width(40)
case col == 1:
style.Width(40)
case col == 2:
style.Width(10)
case col == 3:
style.Width(40)
}
return style
}).
Headers("JOB ID", "RESOURCE TYPE", "STATUS", "INTEGRATION", "FAILURE REASON").
Rows(rows...)

fmt.Println(t)

}

func PrintComplianceJobTable(rows [][]string) {

re := lipgloss.NewRenderer(os.Stdout)

var (
HeaderStyle = re.NewStyle().Foreground(tableColor).Bold(true).Align(lipgloss.Center)
CellStyle = re.NewStyle().Padding(0, 1)
OddRowStyle = CellStyle.Foreground(oddRowColor)
EvenRowStyle = CellStyle.Foreground(evenRowColor)
)

t := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
StyleFunc(func(row, col int) lipgloss.Style {
var style lipgloss.Style
switch {
case row == 0:
style = HeaderStyle
case row%2 == 0:
style = EvenRowStyle
default:
style = OddRowStyle
}

switch {
case col == 0:
style.Width(40)
case col == 1:
style.Width(40)
case col == 2:
style.Width(10)
}
return style
}).
Headers("JOB ID", "BENCHMARK ID", "INTEGRATIONS").
Rows(rows...)

fmt.Println(t)
}
36 changes: 36 additions & 0 deletions pkg/utils/table_rows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"encoding/json"
"strconv"

"github.com/adorigi/opengovernance/pkg/types"
Expand Down Expand Up @@ -40,3 +41,38 @@ func GenerateBenchmarkRows(benchmarks []types.BenchMark) [][]string {
return rows

}

func GenerateDiscoveryJobsRows(jobs []types.RunDiscoveryResponse) ([][]string, error) {
var rows [][]string
for _, job := range jobs {
integrationInfo, err := json.Marshal(job.IntegrationInfo)
if err != nil {
return nil, err
}
row := []string{
strconv.Itoa(int(job.JobId)),
job.ResourceType,
job.Status,
string(integrationInfo),
job.FailureReason,
}
rows = append(rows, row)
}
return rows, nil
}

func GenerateComplianceJobsRows(job types.RunBenchmarkResponse) ([][]string, error) {
var rows [][]string
integrationInfo, err := json.Marshal(job.IntegrationInfo)
if err != nil {
return nil, err
}
row := []string{
strconv.Itoa(int(job.JobId)),
job.BenchmarkId,
string(integrationInfo),
}
rows = append(rows, row)

return rows, nil
}

0 comments on commit 691c315

Please sign in to comment.