From 70ee3f5b1ef16dd1d0fc1399352fba2070305a2b Mon Sep 17 00:00:00 2001 From: Lleyton Gray Date: Fri, 8 Mar 2024 01:32:48 -0800 Subject: [PATCH] feat: format, set-key command --- server/docs/docs.go | 15 +++++- server/docs/swagger.json | 15 +++++- server/docs/swagger.yaml | 11 ++++- server/repos.go | 2 +- subatomic-cli/cmd/create-key.go | 3 -- subatomic-cli/cmd/create-repo.go | 3 -- subatomic-cli/cmd/delete-repo.go | 3 -- subatomic-cli/cmd/pkg.go | 8 +-- subatomic-cli/cmd/repos.go | 3 -- subatomic-cli/cmd/root.go | 5 +- subatomic-cli/cmd/set-key-repo.go | 82 +++++++++++++++++++++++++++++++ subatomic-cli/cmd/upload-comps.go | 3 -- subatomic-cli/cmd/upload.go | 3 -- 13 files changed, 121 insertions(+), 35 deletions(-) create mode 100644 subatomic-cli/cmd/set-key-repo.go diff --git a/server/docs/docs.go b/server/docs/docs.go index c52dc18..0044120 100644 --- a/server/docs/docs.go +++ b/server/docs/docs.go @@ -365,12 +365,12 @@ const docTemplate = `{ "required": true }, { - "description": "options for the new repository", + "description": "options for the key to set", "name": "body", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/types.CreateRepoPayload" + "$ref": "#/definitions/types.SetKeyPayload" } } ], @@ -643,6 +643,17 @@ const docTemplate = `{ "type": "string" } } + }, + "types.SetKeyPayload": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "id": { + "type": "string" + } + } } }, "securityDefinitions": { diff --git a/server/docs/swagger.json b/server/docs/swagger.json index 951e1e9..a784bb3 100644 --- a/server/docs/swagger.json +++ b/server/docs/swagger.json @@ -358,12 +358,12 @@ "required": true }, { - "description": "options for the new repository", + "description": "options for the key to set", "name": "body", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/types.CreateRepoPayload" + "$ref": "#/definitions/types.SetKeyPayload" } } ], @@ -636,6 +636,17 @@ "type": "string" } } + }, + "types.SetKeyPayload": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "id": { + "type": "string" + } + } } }, "securityDefinitions": { diff --git a/server/docs/swagger.yaml b/server/docs/swagger.yaml index e3d5288..9d18f93 100644 --- a/server/docs/swagger.yaml +++ b/server/docs/swagger.yaml @@ -81,6 +81,13 @@ definitions: version: type: string type: object + types.SetKeyPayload: + properties: + id: + type: string + required: + - id + type: object info: contact: {} description: A modern package delivery server. @@ -335,12 +342,12 @@ paths: name: id required: true type: string - - description: options for the new repository + - description: options for the key to set in: body name: body required: true schema: - $ref: '#/definitions/types.CreateRepoPayload' + $ref: '#/definitions/types.SetKeyPayload' produces: - application/json responses: diff --git a/server/repos.go b/server/repos.go index 9288709..7b8abe2 100644 --- a/server/repos.go +++ b/server/repos.go @@ -582,7 +582,7 @@ func (router *reposRouter) getRepoKey(w http.ResponseWriter, r *http.Request) { // @Description set repo key // @Tags repos // @Param id path string true "id for the repository" -// @Param body body types.CreateRepoPayload true "options for the new repository" +// @Param body body types.SetKeyPayload true "options for the key to set" // @Produce json // @Success 204 // @Failure 404 {object} types.ErrResponse diff --git a/subatomic-cli/cmd/create-key.go b/subatomic-cli/cmd/create-key.go index a91772e..3b61ffc 100644 --- a/subatomic-cli/cmd/create-key.go +++ b/subatomic-cli/cmd/create-key.go @@ -1,6 +1,3 @@ -/* -Copyright © 2022 NAME HERE -*/ package cmd import ( diff --git a/subatomic-cli/cmd/create-repo.go b/subatomic-cli/cmd/create-repo.go index 17a6199..633cd04 100644 --- a/subatomic-cli/cmd/create-repo.go +++ b/subatomic-cli/cmd/create-repo.go @@ -1,6 +1,3 @@ -/* -Copyright © 2022 NAME HERE -*/ package cmd import ( diff --git a/subatomic-cli/cmd/delete-repo.go b/subatomic-cli/cmd/delete-repo.go index b387d37..6924d8c 100644 --- a/subatomic-cli/cmd/delete-repo.go +++ b/subatomic-cli/cmd/delete-repo.go @@ -1,6 +1,3 @@ -/* -Copyright © 2022 NAME HERE -*/ package cmd import ( diff --git a/subatomic-cli/cmd/pkg.go b/subatomic-cli/cmd/pkg.go index 8039f70..a42d9ef 100644 --- a/subatomic-cli/cmd/pkg.go +++ b/subatomic-cli/cmd/pkg.go @@ -1,15 +1,11 @@ -/* -Copyright © 2022 NAME HERE -*/ package cmd import "github.com/spf13/cobra" var pkgCmd = &cobra.Command{ - Use: "pkg [subcommand]", - Short: "Manage packages on a Subatomic server", + Use: "pkg [subcommand]", + Short: "Manage packages on a Subatomic server", Aliases: []string{"package", "packages", "p"}, - } func init() { diff --git a/subatomic-cli/cmd/repos.go b/subatomic-cli/cmd/repos.go index f3f7785..527919b 100644 --- a/subatomic-cli/cmd/repos.go +++ b/subatomic-cli/cmd/repos.go @@ -1,6 +1,3 @@ -/* -Copyright © 2022 NAME HERE -*/ package cmd import ( diff --git a/subatomic-cli/cmd/root.go b/subatomic-cli/cmd/root.go index cbe2b64..b502241 100644 --- a/subatomic-cli/cmd/root.go +++ b/subatomic-cli/cmd/root.go @@ -1,6 +1,3 @@ -/* -Copyright © 2022 Fyra Labs -*/ package cmd import ( @@ -16,7 +13,7 @@ var rootCmd = &cobra.Command{ Use: "subatomic-cli", Short: "A modern package delivery server", Long: `Subatomic is a package delivery server made for ease of use. - + This program is a CLI to interface with a subatomic server. `, // Uncomment the following line if your bare application diff --git a/subatomic-cli/cmd/set-key-repo.go b/subatomic-cli/cmd/set-key-repo.go new file mode 100644 index 0000000..c5d2131 --- /dev/null +++ b/subatomic-cli/cmd/set-key-repo.go @@ -0,0 +1,82 @@ +package cmd + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "net/http" + + "github.com/FyraLabs/subatomic/server/types" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// repoCreateCmd represents the create command +var repoSetKeyCmd = &cobra.Command{ + Use: "set-key [repo_id] [id]", + Short: "Set a key for a repo", + Args: cobra.ExactArgs(2), + + 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.SetKeyPayload{ + ID: args[1], + } + + data, err := json.Marshal(payload) + if err != nil { + return err + } + + req, err := http.NewRequest(http.MethodPut, server+"/repos/"+args[0]+"/key", 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.StatusNoContent { + 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() { + repoCmd.AddCommand(repoSetKeyCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // repoCreateCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // repoCreateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/subatomic-cli/cmd/upload-comps.go b/subatomic-cli/cmd/upload-comps.go index e7c3eb2..d148532 100644 --- a/subatomic-cli/cmd/upload-comps.go +++ b/subatomic-cli/cmd/upload-comps.go @@ -1,6 +1,3 @@ -/* -Copyright © 2022 Fyra Labs -*/ package cmd import ( diff --git a/subatomic-cli/cmd/upload.go b/subatomic-cli/cmd/upload.go index 174160a..d7fdb5f 100644 --- a/subatomic-cli/cmd/upload.go +++ b/subatomic-cli/cmd/upload.go @@ -1,6 +1,3 @@ -/* -Copyright © 2022 Fyra Labs -*/ package cmd import (