Skip to content

Commit a9d16b4

Browse files
committed
Walking skeleton
1 parent 1aa64fc commit a9d16b4

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

cmd/kosli/create.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func newCreateCmd(out io.Writer) *cobra.Command {
2020
newCreateEnvironmentCmd(out),
2121
newCreateFlowCmd(out),
2222
newCreatePolicyCmd(out),
23+
newCreateAttestationTypeCmd(out),
2324
)
2425
return cmd
2526
}

cmd/kosli/createAttestationType.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
8+
"github.com/kosli-dev/cli/internal/requests"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
const createAttestationTypeShortDesc = `Create or update a Kosli attestation type.`
13+
14+
const createAttestationTypeLongDesc = createAttestationTypeShortDesc + ``
15+
16+
const createAttestationTypeExample = ` `
17+
18+
type createAttestationTypeOptions struct {
19+
payload CreateAttestationTypePayload
20+
}
21+
22+
type CreateAttestationTypePayload struct {
23+
TypeName string
24+
}
25+
26+
func newCreateAttestationTypeCmd(out io.Writer) *cobra.Command {
27+
o := new(createAttestationTypeOptions)
28+
cmd := &cobra.Command{
29+
Use: "attestation-type TYPE-NAME",
30+
Short: createAttestationTypeShortDesc,
31+
Long: createAttestationTypeLongDesc,
32+
Example: createAttestationTypeExample,
33+
Args: cobra.ExactArgs(1),
34+
PreRunE: func(cmd *cobra.Command, args []string) error {
35+
err := RequireGlobalFlags(global, []string{"Org", "ApiToken"})
36+
if err != nil {
37+
return ErrorBeforePrintingUsage(cmd, err.Error())
38+
}
39+
return nil
40+
},
41+
RunE: func(cmd *cobra.Command, args []string) error {
42+
return o.run(args)
43+
},
44+
}
45+
46+
//cmd.Flags().StringVarP(&o.payload.Description, "description", "d", "", envDescriptionFlag)
47+
48+
addDryRunFlag(cmd)
49+
return cmd
50+
}
51+
52+
func (o *createAttestationTypeOptions) run(args []string) error {
53+
o.payload.TypeName = args[0]
54+
url := fmt.Sprintf("%s/api/v2/custom-attestation-types/%s", global.Host, global.Org)
55+
56+
reqParams := &requests.RequestParams{
57+
Method: http.MethodPost,
58+
URL: url,
59+
Payload: o.payload,
60+
DryRun: global.DryRun,
61+
Token: global.ApiToken,
62+
}
63+
_, err := kosliClient.Do(reqParams)
64+
if err == nil && !global.DryRun {
65+
logger.Info("foo bar fix me %s", o.payload.TypeName)
66+
}
67+
return err
68+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/suite"
8+
)
9+
10+
// Define the suite, and absorb the built-in basic suite
11+
// functionality from testify - including a T() method which
12+
// returns the current testing context
13+
type CreateAttestationTypeTestSuite struct {
14+
suite.Suite
15+
defaultKosliArguments string
16+
}
17+
18+
func (suite *CreateAttestationTypeTestSuite) SetupTest() {
19+
global = &GlobalOpts{
20+
ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY",
21+
Org: "docs-cmd-test-user",
22+
Host: "http://localhost:8001",
23+
}
24+
suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken)
25+
}
26+
27+
func (suite *CreateAttestationTypeTestSuite) TestCustomAttestationTypeCmd() {
28+
tests := []cmdTestCase{
29+
{
30+
wantError: true,
31+
name: "fails when more arguments are provided",
32+
cmd: "create attestation-type" + suite.defaultKosliArguments,
33+
golden: "Error: accepts 1 arg(s), received 0\n",
34+
},
35+
}
36+
37+
runTestCmd(suite.T(), tests)
38+
}u
39+
40+
// In order for 'go test' to run this suite, we need to create
41+
// a normal test function and pass our suite to suite.Run
42+
func TestCreateAttestationTypeTestSuite(t *testing.T) {
43+
suite.Run(t, new(CreateAttestationTypeTestSuite))
44+
}

0 commit comments

Comments
 (0)