Skip to content

Commit

Permalink
feat: format, set-key command
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Mar 8, 2024
1 parent 1409413 commit 70ee3f5
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 35 deletions.
15 changes: 13 additions & 2 deletions server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
],
Expand Down Expand Up @@ -643,6 +643,17 @@ const docTemplate = `{
"type": "string"
}
}
},
"types.SetKeyPayload": {
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "string"
}
}
}
},
"securityDefinitions": {
Expand Down
15 changes: 13 additions & 2 deletions server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
],
Expand Down Expand Up @@ -636,6 +636,17 @@
"type": "string"
}
}
},
"types.SetKeyPayload": {
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "string"
}
}
}
},
"securityDefinitions": {
Expand Down
11 changes: 9 additions & 2 deletions server/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion server/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions subatomic-cli/cmd/create-key.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
Expand Down
3 changes: 0 additions & 3 deletions subatomic-cli/cmd/create-repo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
Expand Down
3 changes: 0 additions & 3 deletions subatomic-cli/cmd/delete-repo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
Expand Down
8 changes: 2 additions & 6 deletions subatomic-cli/cmd/pkg.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
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() {
Expand Down
3 changes: 0 additions & 3 deletions subatomic-cli/cmd/repos.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
Expand Down
5 changes: 1 addition & 4 deletions subatomic-cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 Fyra Labs
*/
package cmd

import (
Expand All @@ -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
Expand Down
82 changes: 82 additions & 0 deletions subatomic-cli/cmd/set-key-repo.go
Original file line number Diff line number Diff line change
@@ -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")
}
3 changes: 0 additions & 3 deletions subatomic-cli/cmd/upload-comps.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 Fyra Labs
*/
package cmd

import (
Expand Down
3 changes: 0 additions & 3 deletions subatomic-cli/cmd/upload.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 Fyra Labs
*/
package cmd

import (
Expand Down

0 comments on commit 70ee3f5

Please sign in to comment.