Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions cmd/kosli/listControls.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ kosli list controls \
--archived \
--api-token yourAPIToken \
--org yourOrgName

# list controls sorted in descending name order:
kosli list controls \
--sort-direction desc \
--api-token yourAPIToken \
--org yourOrgName
`

type listControlsOptions struct {
listOptions
search string
tags []string
archived bool
search string
tags []string
archived bool
sortDirection string
}

type listControlsResponse struct {
Expand Down Expand Up @@ -90,6 +97,7 @@ func newListControlsCmd(out io.Writer) *cobra.Command {
cmd.Flags().StringVar(&o.search, "search", "", controlSearchFlag)
cmd.Flags().StringArrayVar(&o.tags, "tag", []string{}, controlTagFlag)
cmd.Flags().BoolVar(&o.archived, "archived", false, controlArchivedFlag)
cmd.Flags().StringVar(&o.sortDirection, "sort-direction", "", controlSortDirectionFlag)

return cmd
}
Expand All @@ -112,6 +120,9 @@ func (o *listControlsOptions) run(out io.Writer) error {
if o.archived {
params.Set("archived", "true")
}
if o.sortDirection != "" {
params.Set("sort_direction", o.sortDirection)
}
Comment thread
mbevc1 marked this conversation as resolved.
reqURL := base + "?" + params.Encode()

reqParams := &requests.RequestParams{
Expand Down
26 changes: 25 additions & 1 deletion cmd/kosli/listControls_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -13,7 +15,7 @@ type ListControlsCommandTestSuite struct {
acmeOrgKosliArguments string
}

func (suite *ListControlsCommandTestSuite) SetupTest() {
func (suite *ListControlsCommandTestSuite) SetupSuite() {
global = &GlobalOpts{
ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY",
Org: "docs-cmd-test-user",
Expand Down Expand Up @@ -115,6 +117,28 @@ func (suite *ListControlsCommandTestSuite) TestListControlsCmd() {
runTestCmd(suite.T(), tests)
}

// asserts both directions against each other rather than each against the
// server default (asc): if the flag were silently dropped, both runs would
// return the default order and the reversal below would fail.
func (suite *ListControlsCommandTestSuite) TestListControlsSortDirectionIsHonored() {
identifiers := func(direction string) []string {
cmd := "list controls --search list-control --sort-direction " + direction +
" --output json" + suite.defaultKosliArguments
_, combined, _, _, err := executeCommandC(cmd)
require.NoError(suite.T(), err)
var response listControlsResponse
require.NoError(suite.T(), json.Unmarshal([]byte(combined), &response))
ids := []string{}
for _, control := range response.Controls {
ids = append(ids, control["identifier"].(string))
}
return ids
}

require.Equal(suite.T(), []string{"list-control-1", "list-control-2"}, identifiers("asc"))
require.Equal(suite.T(), []string{"list-control-2", "list-control-1"}, identifiers("desc"))
}

func TestListControlsCommandTestSuite(t *testing.T) {
suite.Run(t, new(ListControlsCommandTestSuite))
}
1 change: 1 addition & 0 deletions cmd/kosli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ The ^.kosli_ignore^ will be treated as part of the artifact like any other file,
controlSearchFlag = "[optional] Only list controls whose name or identifier contains this substring (case-insensitive)."
controlTagFlag = "[optional] Filter by tag, given as 'key' or 'key:value'. Can be repeated."
controlArchivedFlag = "[optional] List archived controls instead of active ones."
controlSortDirectionFlag = "[optional] The direction to sort controls in. Valid values are: [asc, desc]. (defaults to asc)"
envNameFlag = "The Kosli environment name to assert the artifact against."
pathsWatchFlag = "[optional] Watch the filesystem for changes and report snapshots of artifacts running in specific filesystem paths to Kosli."
getAttestationFingerprintFlag = "[conditional] The fingerprint of the artifact for the attestation. Cannot be used together with --trail or --attestation-id."
Expand Down
Loading