Skip to content

Commit

Permalink
fix: add get jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
artaasadi committed Sep 16, 2024
1 parent 8bd3fbc commit 3ee21e9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 95 deletions.
4 changes: 4 additions & 0 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ func init() {
GetCmd.AddCommand(jobDetailsCmd)
jobDetailsCmd.PersistentFlags().String("job-id", "", "Job ID")
jobDetailsCmd.PersistentFlags().String("job-type", "", "Job Type. Options: compliance, analytics, discovery")

GetCmd.AddCommand(jobsCmd)
jobsCmd.PersistentFlags().String("job-type", "", "Job Type. Options: compliance, analytics, discovery")
jobsCmd.PersistentFlags().String("interval", "90m", "Specify time interval like: 90m, 1h, 50 minutes, 2 hours")
}
64 changes: 12 additions & 52 deletions cmd/list/jobs.go → cmd/get/jobs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package list
package get

import (
"encoding/json"
Expand All @@ -16,8 +16,8 @@ import (
// jobsCmd represents the List Jobs command
var jobsCmd = &cobra.Command{
Use: "jobs",
Short: "Get benchmark findings summary",
Long: `Get benchmark findings summary`,
Short: "Get Jobs for job type and in the time interval",
Long: `Get Jobs for job type and in the time interval`,
RunE: func(cmd *cobra.Command, args []string) error {
client := &http.Client{}
configuration, err := config.ReadConfigFile()
Expand All @@ -32,70 +32,30 @@ var jobsCmd = &cobra.Command{

jobType := utils.ReadStringFlag(cmd, "job-type")
if jobType == "" {
fmt.Println("Error: must specify Job Type")
fmt.Println("Error: must specify Job Type. Options: compliance, analytics, discovery")
return nil
}
if strings.ToLower(jobType) != "compliance" && strings.ToLower(jobType) != "discovery" &&
strings.ToLower(jobType) != "analytics" {
fmt.Println("invalid job type")
fmt.Println("Invalid job type. Options: compliance, analytics, discovery")
return nil
}

selector := utils.ReadStringFlag(cmd, "selector")
if selector == "" {
fmt.Println("Error: must specify Selector")
return nil
}
fmt.Println(selector)
if strings.ToLower(selector) != "job_id" && strings.ToLower(selector) != "integration_info" &&
strings.ToLower(selector) != "status" && strings.ToLower(selector) != "benchmark" {
fmt.Println("invalid selector. valid values: job_id, integration, status, benchmark")
interval := utils.ReadStringFlag(cmd, "interval")
if interval == "" {
fmt.Println("Error: must specify interval like: 90m, 1h, 50 minutes, 2 hours")
return nil
}

integrationsStr, err := utils.ReadStringArrayFlag(cmd, "integration")
if err != nil {
return err
}

var integrations []types.IntegrationFilterInfo
for _, integrationStr := range integrationsStr {
integrations = append(integrations, types.ParseIntegrationInfo(integrationStr))
}

jobId, err := utils.ReadStringSliceFlag(cmd, "job-id")
if err != nil {
return err
}
status, err := utils.ReadStringSliceFlag(cmd, "status")
if err != nil {
return err
}
benchmark, err := utils.ReadStringSliceFlag(cmd, "benchmark")
if err != nil {
return err
}

requestPayload := types.ListJobsByTypeRequest{
JobType: jobType,
Selector: selector,
JobId: jobId,
IntegrationInfo: integrations,
Status: status,
Benchmark: benchmark,
}

payload, err := json.Marshal(requestPayload)
if err != nil {
return err
}
url := fmt.Sprintf("main/schedule/api/v2/jobs")
url := fmt.Sprintf("main/schedule/api/v3/jobs/interval?job_type=%s&interval=%s", jobType, interval)
request, err := request.GenerateRequest(
configuration.ApiKey,
configuration.ApiEndpoint,
"POST",
"GET",
url,
payload,
[]byte{},
)
if err != nil {
return err
Expand All @@ -117,7 +77,7 @@ var jobsCmd = &cobra.Command{
return nil
}

var jobs []types.ListJobsByTypeResponse
var jobs []types.ListJobsByTypeItem
err = json.Unmarshal(body, &jobs)
if err != nil {
return err
Expand Down
32 changes: 0 additions & 32 deletions cmd/list/list.go

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"github.com/adorigi/checkctl/cmd/list"
"os"

"github.com/adorigi/checkctl/cmd/run"
Expand Down Expand Up @@ -42,7 +41,6 @@ func init() {
rootCmd.AddCommand(configureCmd)
rootCmd.AddCommand(get.GetCmd)
rootCmd.AddCommand(run.RunCmd)
rootCmd.AddCommand(list.ListCmd)

rootCmd.PersistentFlags().String("output", "", "Output format: json/table")
// Here you will define your flags and configuration settings.
Expand Down
8 changes: 8 additions & 0 deletions pkg/types/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ type GetComplianceJobStatusResponse struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

type ListJobsByTypeItem struct {
JobId string `json:"job_id"`
JobType string `json:"job_type"`
JobStatus string `json:"job_status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
9 changes: 0 additions & 9 deletions pkg/types/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"strings"
"time"
)

type RunBenchmarkByIdRequest struct {
Expand Down Expand Up @@ -113,11 +112,3 @@ const (
JobSortOrder_ASC = "ASC"
JobSortOrder_DESC = "DESC"
)

type ListJobsByTypeResponse struct {
JobId string `json:"job_id"`
JobType string `json:"job_type"`
JobStatus string `json:"job_status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

0 comments on commit 3ee21e9

Please sign in to comment.