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
28 changes: 27 additions & 1 deletion cmd/kosli/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path"
"path/filepath"
"strings"
"unicode"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
Expand Down Expand Up @@ -180,6 +181,7 @@ func KosliGenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(st
// Note: The contents of the title lines could also contain < and > characters which will
// be lost if simply embedded in a md ## section.
buf.WriteString("## Examples Use Cases\n\n")
buf.WriteString("These examples all assume that the flags `--api-token`, `--org`, `--host`, (and `--flow`, `--trail` when required), are set/provided. \n\n")

// Some non-title lines contain a # character, (eg in a snappish) so we have to
// split on newlines first and then only split on # in the first position
Expand Down Expand Up @@ -226,12 +228,36 @@ func hashTitledExamples(lines []string) [][]string {
result = append(result, example) // See result[1:] at end
example = make([]string, 0)
}
example = append(example, line)
if !isSetWithEnvVar(line) {
example = append(example, choppedLineContinuation(line))
}
}
result = append(result, example)
return result[1:]
}

func isSetWithEnvVar(line string) bool {
trimmed_line := strings.TrimSpace(line)
if strings.HasPrefix(trimmed_line, "--api-token ") {
return true
} else if strings.HasPrefix(trimmed_line, "--host ") {
return true
} else if strings.HasPrefix(trimmed_line, "--org ") {
return true
} else if strings.HasPrefix(trimmed_line, "--flow ") {
return true
} else if strings.HasPrefix(trimmed_line, "--trail ") {
return true
} else {
return false
}
}

func choppedLineContinuation(line string) string {
trimmed_line := strings.TrimRightFunc(line, unicode.IsSpace)
return strings.TrimSuffix(trimmed_line, "\\")
}

func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
flags := cmd.NonInheritedFlags()
flags.SetOutput(buf)
Expand Down
10 changes: 10 additions & 0 deletions cmd/kosli/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ type DocsCommandTestSuite struct {
}

func (suite *DocsCommandTestSuite) TestDocsCmd() {
// If this test fails, a simple way to retrieve a new generated master is to:
// - add an import for fmt
// - uncomment the fmt.Printf() call below
// - comment out the line defer os.RemoveAll(tempDirName)
// Then:
// - make test_integration_single TARGET=TestDocsCommandTestSuite
// will tell you where the new snyk.md master file lives.
// Then copy it to ./cmd/kosli/testdata/output/docs/
// and undo the changes above.
global = &GlobalOpts{}
tempDirName, err := os.MkdirTemp("", "generatedDocs")
//fmt.Printf("tempDirName :%s:\n\n\n\n\n", tempDirName)
require.NoError(suite.Suite.T(), err)
defer os.RemoveAll(tempDirName)

Expand Down
72 changes: 25 additions & 47 deletions cmd/kosli/testdata/output/docs/snyk.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,85 +62,63 @@ snyk [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]

## Examples Use Cases

These examples all assume that the flags `--api-token`, `--org`, `--host`, (and `--flow`, `--trail` when required), are set/provided.

**report a snyk attestation about a pre-built docker artifact (kosli calculates the fingerprint)**

```shell
kosli attest snyk yourDockerImageName \
--artifact-type docker \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--scan-results yourSnykSARIFScanResults \
--api-token yourAPIToken \
--org yourOrgName
kosli attest snyk yourDockerImageName
--artifact-type docker
--name yourAttestationName
--scan-results yourSnykSARIFScanResults

```

**report a snyk attestation about a pre-built docker artifact (you provide the fingerprint)**

```shell
kosli attest snyk \
--fingerprint yourDockerImageFingerprint \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--scan-results yourSnykSARIFScanResults \
--api-token yourAPIToken \
--org yourOrgName
kosli attest snyk
--fingerprint yourDockerImageFingerprint
--name yourAttestationName
--scan-results yourSnykSARIFScanResults

```

**report a snyk attestation about a trail**

```shell
kosli attest snyk \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--scan-results yourSnykSARIFScanResults \
--api-token yourAPIToken \
--org yourOrgName
kosli attest snyk
--name yourAttestationName
--scan-results yourSnykSARIFScanResults

```

**report a snyk attestation about an artifact which has not been reported yet in a trail**

```shell
kosli attest snyk \
--name yourTemplateArtifactName.yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--commit yourArtifactGitCommit \
--scan-results yourSnykSARIFScanResults \
--api-token yourAPIToken \
--org yourOrgName
kosli attest snyk
--name yourTemplateArtifactName.yourAttestationName
--commit yourArtifactGitCommit
--scan-results yourSnykSARIFScanResults

```

**report a snyk attestation about a trail with an attachment**

```shell
kosli attest snyk \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--scan-results yourSnykSARIFScanResults \
--attachments yourEvidencePathName \
--api-token yourAPIToken \
--org yourOrgName
kosli attest snyk
--name yourAttestationName
--scan-results yourSnykSARIFScanResults
--attachments yourEvidencePathName

```

**report a snyk attestation about a trail without uploading the snyk results file**

```shell
kosli attest snyk \
--name yourAttestationName \
--flow yourFlowName \
--trail yourTrailName \
--scan-results yourSnykSARIFScanResults \
--upload-results=false \
--api-token yourAPIToken \
--org yourOrgName
kosli attest snyk
--name yourAttestationName
--scan-results yourSnykSARIFScanResults
--upload-results=false
```