mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-25 16:28:20 +00:00
![James Mills](/assets/img/avatar_default.png)
Co-authored-by: James Mills <1290234+prologic@users.noreply.github.com> Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/180
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var requestCmd = &cobra.Command{
|
|
Aliases: []string{"req"},
|
|
Use: "request <method> <endpoint> [<file>|-]",
|
|
Short: "Makes a signed request to an API endpoint on a Salty Broker",
|
|
Long: `This command creates and makes a signed request to an API endpoint
|
|
on a Salty Broker (an instance of saltyd). A valid HTTP method such as GET, PUT, POST, DELETE or HEAD
|
|
is provided as the first argument and a valid API endpoint path such as /api/v1/blob -- The 3rd argument
|
|
is optional and if supplifed must either be a path to a filename containing the payload of the request,
|
|
- for reading from stdin, or if omitted will read from stdin.
|
|
|
|
This is mostly useful in debugging and development of the Salty Broker API and Client.
|
|
|
|
NOTE: This is only spported on a Salty Broker.`,
|
|
Args: cobra.MinimumNArgs(2),
|
|
PreRunE: initializeClient,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
request(args[:])
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(requestCmd)
|
|
}
|
|
|
|
func request(args []string) {
|
|
var (
|
|
body []byte
|
|
err error
|
|
)
|
|
|
|
method := args[0]
|
|
endpoint := args[1]
|
|
|
|
if len(args) == 3 {
|
|
if args[2] == "-" {
|
|
body, err = io.ReadAll(os.Stdin)
|
|
} else {
|
|
body, err = os.ReadFile(args[2])
|
|
}
|
|
} else {
|
|
body = nil
|
|
}
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error reading request payload: %s", err)
|
|
os.Exit(2)
|
|
}
|
|
|
|
data, err := cli.Request(method, endpoint, body)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error making request: %s\n", err)
|
|
os.Exit(2)
|
|
}
|
|
|
|
fmt.Println(string(data))
|
|
}
|