Skip to content

Commit

Permalink
feat: create key CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Mar 8, 2024
1 parent ae21e98 commit 1409413
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 61 deletions.
26 changes: 13 additions & 13 deletions server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/main.createKeyPayload"
"$ref": "#/definitions/types.CreateKeyPayload"
}
}
],
"responses": {
"200": {
"description": "OK"
"201": {
"description": "Created"
},
"400": {
"description": "Bad Request",
Expand Down Expand Up @@ -522,13 +522,8 @@ const docTemplate = `{
}
},
"definitions": {
"main.createKeyPayload": {
"main.fullKeyResponse": {
"type": "object",
"required": [
"email",
"id",
"name"
],
"properties": {
"email": {
"type": "string"
Expand All @@ -538,11 +533,19 @@ const docTemplate = `{
},
"name": {
"type": "string"
},
"public_key": {
"type": "string"
}
}
},
"main.fullKeyResponse": {
"types.CreateKeyPayload": {
"type": "object",
"required": [
"email",
"id",
"name"
],
"properties": {
"email": {
"type": "string"
Expand All @@ -552,9 +555,6 @@ const docTemplate = `{
},
"name": {
"type": "string"
},
"public_key": {
"type": "string"
}
}
},
Expand Down
26 changes: 13 additions & 13 deletions server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/main.createKeyPayload"
"$ref": "#/definitions/types.CreateKeyPayload"
}
}
],
"responses": {
"200": {
"description": "OK"
"201": {
"description": "Created"
},
"400": {
"description": "Bad Request",
Expand Down Expand Up @@ -515,13 +515,8 @@
}
},
"definitions": {
"main.createKeyPayload": {
"main.fullKeyResponse": {
"type": "object",
"required": [
"email",
"id",
"name"
],
"properties": {
"email": {
"type": "string"
Expand All @@ -531,11 +526,19 @@
},
"name": {
"type": "string"
},
"public_key": {
"type": "string"
}
}
},
"main.fullKeyResponse": {
"types.CreateKeyPayload": {
"type": "object",
"required": [
"email",
"id",
"name"
],
"properties": {
"email": {
"type": "string"
Expand All @@ -545,9 +548,6 @@
},
"name": {
"type": "string"
},
"public_key": {
"type": "string"
}
}
},
Expand Down
22 changes: 11 additions & 11 deletions server/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
basePath: /
definitions:
main.createKeyPayload:
main.fullKeyResponse:
properties:
email:
type: string
id:
type: string
name:
type: string
required:
- email
- id
- name
public_key:
type: string
type: object
main.fullKeyResponse:
types.CreateKeyPayload:
properties:
email:
type: string
id:
type: string
name:
type: string
public_key:
type: string
required:
- email
- id
- name
type: object
types.CreateRepoPayload:
properties:
Expand Down Expand Up @@ -115,10 +115,10 @@ paths:
name: body
required: true
schema:
$ref: '#/definitions/main.createKeyPayload'
$ref: '#/definitions/types.CreateKeyPayload'
responses:
"200":
description: OK
"201":
description: Created
"400":
description: Bad Request
schema:
Expand Down
27 changes: 8 additions & 19 deletions server/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,19 @@ func (router *keysRouter) getKeys(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, res)
}

type createKeyPayload struct {
ID string `json:"id" validate:"required,hostname"`
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
}

func (u *createKeyPayload) Bind(r *http.Request) error {
return validate.Struct(u)
}

// createKey godoc
//
// @Summary Create a new key
// @Description create key
// @Tags keys
// @Accept json
// @Param body body createKeyPayload true "options for the new key"
// @Success 200
// @Param body body types.CreateKeyPayload true "options for the new key"
// @Success 201
// @Failure 400 {object} types.ErrResponse
// @Failure 409 {object} types.ErrResponse
// @Router /keys [post]
func (router *keysRouter) createKey(w http.ResponseWriter, r *http.Request) {
payload := &createKeyPayload{}
payload := &types.CreateKeyPayload{}

if err := render.Bind(r, payload); err != nil {
render.Render(w, r, types.ErrInvalidRequest(err))
Expand All @@ -96,7 +86,7 @@ func (router *keysRouter) createKey(w http.ResponseWriter, r *http.Request) {
panic(err)
}

key, err := router.database.SigningKey.Create().
_, err = router.database.SigningKey.Create().
SetID(payload.ID).
SetPublicKey(armoredPublicKey).
SetPrivateKey(armoredPrivateKey).
Expand All @@ -108,11 +98,10 @@ func (router *keysRouter) createKey(w http.ResponseWriter, r *http.Request) {
panic(err)
}

render.JSON(w, r, &types.KeyResponse{
ID: key.ID,
Name: key.Name,
Email: key.Email,
})
w.WriteHeader(http.StatusCreated)
if _, err := w.Write(nil); err != nil {
panic(err)
}
}

type fullKeyResponse struct {
Expand Down
11 changes: 11 additions & 0 deletions server/types/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ func (u *CreateRepoPayload) Bind(r *http.Request) error {
return validate.Struct(u)
}

type CreateKeyPayload struct {
ID string `json:"id" validate:"required,hostname"`
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
}

func (u *CreateKeyPayload) Bind(r *http.Request) error {
validate := r.Context().Value(ValidateContextKey{}).(*validator.Validate)
return validate.Struct(u)
}

type SetKeyPayload struct {
ID string `json:"id" validate:"required,hostname"`
}
Expand Down
87 changes: 87 additions & 0 deletions subatomic-cli/cmd/create-key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/FyraLabs/subatomic/server/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// keyCreateCmd represents the create command
var keyCreateCmd = &cobra.Command{
Use: "create [id] [name] [email]",
Short: "Create a new key",
Args: cobra.ExactArgs(3),

RunE: func(cmd *cobra.Command, args []string) error {
server := viper.GetString("server")
token := viper.GetString("token")

if server == "" {
return errors.New("server must be defined")
}

if token == "" {
return errors.New("token must be defined")
}

payload := types.CreateKeyPayload{
ID: args[0],
Name: args[1],
Email: args[2],
}

data, err := json.Marshal(payload)
if err != nil {
return err
}

req, err := http.NewRequest(http.MethodPost, server+"/keys", bytes.NewReader(data))
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+token)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return err
}

if res.StatusCode != http.StatusCreated {
var serverError types.ErrResponse
if err := json.NewDecoder(res.Body).Decode(&serverError); err != nil {
return err
}

return fmt.Errorf("API returned error: %s", serverError.ErrorText)
}

return nil
},
}

func init() {
keysCmd.AddCommand(keyCreateCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// keyCreateCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// keyCreateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
10 changes: 5 additions & 5 deletions subatomic-cli/cmd/create.go → subatomic-cli/cmd/create-repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/spf13/viper"
)

// createCmd represents the create command
var createCmd = &cobra.Command{
// repoCreateCmd represents the create command
var repoCreateCmd = &cobra.Command{
Use: "create [id] [type]",
Short: "Create a new repo",
Args: cobra.ExactArgs(2),
Expand Down Expand Up @@ -72,15 +72,15 @@ var createCmd = &cobra.Command{
}

func init() {
repoCmd.AddCommand(createCmd)
repoCmd.AddCommand(repoCreateCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
// repoCreateCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
// repoCreateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

0 comments on commit 1409413

Please sign in to comment.