|
| 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 | +} |
0 commit comments