Skip to content

Commit 02147ad

Browse files
committed
Tidy up tests and documentation
1 parent 702a0f2 commit 02147ad

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

cmd/kosli/getAttestation.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,34 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14-
const getAttestationShortDesc = `Get attestation by name from a specified trail or artifact. `
14+
const getAttestationShortDesc = `Get an attestation using its name or id. `
1515

1616
const getAttestationLongDesc = getAttestationShortDesc + `
17-
You can get an attestation from a trail or artifact using its name. The attestation name should be given
18-
WITHOUT dot-notation.
19-
20-
To get an attestation from a trail, specify the trail name using the --trail flag.
21-
To get an attestation from an artifact, specify the artifact fingerprint using the --fingerprint flag.
22-
23-
In both cases the flow must also be specified using the --flow flag.
2417
18+
You can get an attestation from a trail or artifact using its name. The attestation name should be given
19+
WITHOUT dot-notation.
20+
To get an attestation from a trail, specify the trail name using the ^--trail^ flag.
21+
To get an attestation from an artifact, specify the artifact fingerprint using the ^--fingerprint^ flag.
22+
These flags cannot be used together. In both cases the flow must also be specified using the ^--flow^ flag.
2523
If there are multiple attestations with the same name on the trail or artifact, a list of all will be returned.
24+
25+
You can also get an attestation by its id using the ^--attestation-id^ flag. This cannot be used with the attestation name,
26+
or any of the ^--flow^, ^--trail^ or ^--fingerprint^ flags.
2627
`
2728

2829
const getAttestationExample = `
29-
# get an attestation from a trail (requires the --trail flag)
30+
# get an attestation by name from a trail (requires the --trail flag)
3031
kosli get attestation attestationName \
3132
--flow flowName \
3233
--trail trailName
3334
34-
# get an attestation from an artifact
35+
# get an attestation by name from an artifact
3536
kosli get attestation attestationName \
3637
--flow flowName \
3738
--fingerprint fingerprint
39+
40+
# get an attestation by its id
41+
kosli get attestation --attestation-id attestationID
3842
`
3943

4044
type getAttestationOptions struct {
@@ -92,13 +96,12 @@ func newGetAttestationCmd(out io.Writer) *cobra.Command {
9296
return fmt.Errorf("--flow, --trail, and --fingerprint flags cannot be used with --attestation-id")
9397
}
9498
} else {
95-
err := RequireFlags(cmd, []string{"flow"})
96-
if err != nil {
97-
logger.Error("failed to configure required flags: %v", err)
99+
if o.flow == "" {
100+
return fmt.Errorf("--flow is required when using ATTESTATION-NAME")
98101
}
99102
err = MuXRequiredFlags(cmd, []string{"trail", "fingerprint"}, true)
100103
if err != nil {
101-
return err
104+
return fmt.Errorf("%s when using ATTESTATION-NAME", err)
102105
}
103106
}
104107
return nil
@@ -109,10 +112,10 @@ func newGetAttestationCmd(out io.Writer) *cobra.Command {
109112
}
110113

111114
cmd.Flags().StringVarP(&o.output, "output", "o", "table", outputFlag)
112-
cmd.Flags().StringVarP(&o.flow, "flow", "f", "", flowNameFlag)
113-
cmd.Flags().StringVarP(&o.trail, "trail", "t", "", getAttestationTrailFlag)
115+
cmd.Flags().StringVarP(&o.flow, "flow", "f", "", getAttestationFlowNameFlag)
116+
cmd.Flags().StringVarP(&o.trail, "trail", "t", "", getAttestationTrailNameFlag)
114117
cmd.Flags().StringVarP(&o.fingerprint, "fingerprint", "F", "", getAttestationFingerprintFlag)
115-
cmd.Flags().StringVar(&o.attestationID, "attestation-id", "", "The unique identifier of the attestation to retrieve.")
118+
cmd.Flags().StringVar(&o.attestationID, "attestation-id", "", attestationIDFlag)
116119

117120
return cmd
118121
}

cmd/kosli/getAttestation_test.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,101 +55,107 @@ func (suite *GetAttestationCommandTestSuite) TestGetAttestationCmd() {
5555
tests := []cmdTestCase{
5656
{
5757
wantError: false,
58-
name: "if no attestation found, say so",
58+
name: "01 if no attestation found when getting by name, say so",
5959
cmd: fmt.Sprintf(`get attestation non-existent-attestation --flow %s --trail %s %s`, suite.flowName, suite.trailName, suite.defaultKosliArguments),
6060
golden: "No attestations found.\n",
6161
},
6262
{
6363
wantError: false,
64-
name: "if no attestation found return empty list in json format",
64+
name: "02 if no attestation found when getting by name return empty list in json format",
6565
cmd: fmt.Sprintf(`get attestation non-existent-attestation --flow %s --trail %s %s --output json`, suite.flowName, suite.trailName, suite.defaultKosliArguments),
6666
goldenJson: []jsonCheck{{"", "[]"}},
6767
},
6868
{
6969
wantError: true,
70-
name: "providing more than one argument fails",
70+
name: "03 providing more than one argument fails",
7171
cmd: fmt.Sprintf(`get attestation first-attestation second-attestation --flow %s --trail %s %s`, suite.flowName, suite.trailName, suite.defaultKosliArguments),
7272
golden: "Error: accepts at most 1 arg(s), received 2\n",
7373
},
7474
{
7575
wantError: true,
76-
name: "missing --flow fails when ATTESTATION-NAME is provided",
76+
name: "04 missing --flow fails when ATTESTATION-NAME is provided",
7777
cmd: fmt.Sprintf(`get attestation first-artifact-attestation --trail %s %s`, suite.trailName, suite.defaultKosliArguments),
78-
golden: "Error: required flag(s) \"flow\" not set\n",
78+
golden: "Error: --flow is required when using ATTESTATION-NAME\n",
7979
},
8080
{
8181
wantError: true,
82-
name: "missing --api-token fails",
82+
name: "05 missing --api-token fails",
8383
cmd: fmt.Sprintf(`get attestation first-artifact-attestation --flow %s --org orgX`, suite.flowName),
8484
golden: "Error: --api-token is not set\nUsage: kosli get attestation [ATTESTATION-NAME] [flags]\n",
8585
},
8686
{
87-
name: "getting an existing trail attestation works",
87+
name: "06 getting an existing trail attestation works",
8888
cmd: fmt.Sprintf(`get attestation first-trail-attestation --flow %s --trail %s %s`, suite.flowName, suite.trailName, suite.defaultKosliArguments),
8989
},
9090
{
91-
name: "getting an existing trail attestation with --output json works",
91+
name: "07 getting an existing trail attestation with --output json works",
9292
cmd: fmt.Sprintf(`get attestation first-trail-attestation --flow %s --trail %s --output json %s`, suite.flowName, suite.trailName, suite.defaultKosliArguments),
9393
goldenJson: []jsonCheck{{"", "non-empty"}},
9494
},
9595
{
96-
name: "getting an existing artifact attestation works",
96+
name: "08 getting an existing artifact attestation works",
9797
cmd: fmt.Sprintf(`get attestation first-artifact-attestation --flow %s --fingerprint %s %s`, suite.flowName, suite.fingerprint, suite.defaultKosliArguments),
9898
},
9999
{
100-
name: "getting an existing artifact attestation with --output json works",
100+
name: "09 getting an existing artifact attestation with --output json works",
101101
cmd: fmt.Sprintf(`get attestation first-artifact-attestation --flow %s --fingerprint %s --output json %s`, suite.flowName, suite.fingerprint, suite.defaultKosliArguments),
102102
goldenJson: []jsonCheck{{"", "non-empty"}},
103103
},
104104
{
105105
wantError: true,
106-
name: "missing both trail and fingerprint fails",
106+
name: "10 missing both trail and fingerprint fails if ATTESTATION-NAME provided",
107107
cmd: fmt.Sprintf(`get attestation first-artifact-attestation --flow %s %s`, suite.flowName, suite.defaultKosliArguments),
108-
golden: "Error: at least one of --trail, --fingerprint is required\n",
108+
golden: "Error: at least one of --trail, --fingerprint is required when using ATTESTATION-NAME\n",
109109
},
110110
{
111111
wantError: true,
112-
name: "providing both trail and fingerprint fails",
112+
name: "11 providing both trail and fingerprint fails",
113113
cmd: fmt.Sprintf(`get attestation first-artifact-attestation --flow %s --trail %s --fingerprint %s %s`, suite.flowName, suite.trailName, suite.fingerprint, suite.defaultKosliArguments),
114-
golden: "Error: only one of --trail, --fingerprint is allowed\n",
114+
golden: "Error: only one of --trail, --fingerprint is allowed when using ATTESTATION-NAME\n",
115115
},
116116
{
117-
name: "can get an attestation from its id",
117+
name: "12 can get an attestation from its id",
118118
cmd: fmt.Sprintf(`get attestation --attestation-id %s %s`, suite.attestationId, suite.defaultKosliArguments),
119119
},
120+
{
121+
wantError: false,
122+
name: "13 if no attestation found when getting by name, say so",
123+
cmd: fmt.Sprintf(`get attestation --attestation-id %s %s`, "non-existent-attestation-id", suite.defaultKosliArguments),
124+
golden: "No attestations found.\n",
125+
},
120126
{
121127
wantError: false,
122-
name: "if no attestation found when getting by id return empty list in json format",
128+
name: "14 if no attestation found when getting by id return empty list in json format",
123129
cmd: fmt.Sprintf(`get attestation --attestation-id %s --output json %s`, "non-existent-attestation-id", suite.defaultKosliArguments),
124130
goldenJson: []jsonCheck{{"data", "length:0"}},
125131
},
126132
{
127133
wantError: true,
128-
name: "providing both attestation id and attestation name fails",
134+
name: "15 providing both attestation id and attestation name fails",
129135
cmd: fmt.Sprintf(`get attestation %s --attestation-id %s %s`, "first-artifact-attestation", suite.attestationId, suite.defaultKosliArguments),
130136
golden: "Error: --attestation-id cannot be used when ATTESTATION-NAME is provided\n",
131137
},
132138
{
133139
wantError: true,
134-
name: "providing both attestation id and trail fails",
140+
name: "16 providing both attestation id and trail fails",
135141
cmd: fmt.Sprintf(`get attestation --attestation-id %s --trail %s %s`, suite.attestationId, suite.trailName, suite.defaultKosliArguments),
136142
golden: "Error: --flow, --trail, and --fingerprint flags cannot be used with --attestation-id\n",
137143
},
138144
{
139145
wantError: true,
140-
name: "providing both attestation id and fingerprint fails",
146+
name: "17 providing both attestation id and fingerprint fails",
141147
cmd: fmt.Sprintf(`get attestation --attestation-id %s --fingerprint %s %s`, suite.attestationId, suite.fingerprint, suite.defaultKosliArguments),
142148
golden: "Error: --flow, --trail, and --fingerprint flags cannot be used with --attestation-id\n",
143149
},
144150
{
145151
wantError: true,
146-
name: "providing both attestation id and flow fails",
152+
name: "18 providing both attestation id and flow fails",
147153
cmd: fmt.Sprintf(`get attestation --attestation-id %s --flow %s %s`, suite.attestationId, suite.flowName, suite.defaultKosliArguments),
148154
golden: "Error: --flow, --trail, and --fingerprint flags cannot be used with --attestation-id\n",
149155
},
150156
{
151157
wantError: true,
152-
name: "providing neither attestation id or flow fails",
158+
name: "19 providing neither attestation id or flow fails",
153159
cmd: fmt.Sprintf(`get attestation %s`, suite.defaultKosliArguments),
154160
golden: "Error: one of ATTESTATION-NAME argument or --attestation-id flag is required\n",
155161
},

cmd/kosli/root.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,10 @@ The ^.kosli_ignore^ will be treated as part of the artifact like any other file,
254254
attestationTypeJqFlag = "[optional] The attestation type evaluation JQ rules."
255255
envNameFlag = "The Kosli environment name to assert the artifact against."
256256
pathsWatchFlag = "[optional] Watch the filesystem for changes and report snapshots of artifacts running in specific filesystem paths to Kosli."
257-
getAttestationFingerprintFlag = "[conditional] The fingerprint of the artifact for the attestation. Cannot be used together with --trail."
258-
getAttestationTrailFlag = "[conditional] The name of the Kosli trail for the attestation. Cannot be used together with --fingerprint."
257+
getAttestationFingerprintFlag = "[conditional] The fingerprint of the artifact for the attestation. Cannot be used together with --trail or --attestation-id."
258+
getAttestationTrailNameFlag = "[conditional] The name of the Kosli trail for the attestation. Cannot be used together with --fingerprint or --attestation-id."
259+
getAttestationFlowNameFlag = "[conditional] The name of the Kosli flow for the attestation. Required if ATTESTATION-NAME provided. Cannot be used together with --attestation-id."
260+
attestationIDFlag = "[conditional] The unique identifier of the attestation to retrieve. Cannot be used together with ATTESTATION-NAME."
259261
)
260262

261263
var global *GlobalOpts

0 commit comments

Comments
 (0)