2026-05-13 13:51:09 +02:00
|
|
|
/*
|
|
|
|
|
Copyright © 2026 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
package es
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-29 12:42:02 +02:00
|
|
|
"bufio"
|
2026-05-13 13:51:09 +02:00
|
|
|
"bytes"
|
2026-05-18 13:56:07 +02:00
|
|
|
"context"
|
2026-05-13 13:51:09 +02:00
|
|
|
"crypto/tls"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2026-05-18 13:56:07 +02:00
|
|
|
"os/exec"
|
2026-05-13 13:51:09 +02:00
|
|
|
"slices"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"codeberg.org/scip/esctl/pkg/cfg"
|
|
|
|
|
"github.com/chzyer/readline"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
const intro = `Input format: verb path [data]"
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
post /yourindex/_ccr/pause_follow
|
|
|
|
|
put /yourindex/_settings {"number_of_replicas": 1}
|
|
|
|
|
|
|
|
|
|
You can also put multiline JSON after the path like:
|
|
|
|
|
|
|
|
|
|
put /yourindex/_settings
|
|
|
|
|
{
|
|
|
|
|
"number_of_replicas": 1
|
2026-05-13 13:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
If you do NOT supply a JSON in the first line, you need to hit ENTER
|
|
|
|
|
twice to complete.`
|
|
|
|
|
|
|
|
|
|
func Repl(conf *cfg.Config) error {
|
|
|
|
|
verbs := []string{"post", "get", "put", "delete"}
|
|
|
|
|
|
|
|
|
|
fmt.Println(intro)
|
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
|
|
reader, err := readline.NewEx(&readline.Config{
|
|
|
|
|
Prompt: "> ",
|
|
|
|
|
HistoryFile: os.Getenv("HOME") + "/.config/esctl/history",
|
|
|
|
|
HistoryLimit: 500,
|
|
|
|
|
InterruptPrompt: "^C",
|
|
|
|
|
EOFPrompt: "exit",
|
|
|
|
|
HistorySearchFold: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to initialize readline lib: %s", err)
|
|
|
|
|
}
|
2026-05-13 13:51:09 +02:00
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
for {
|
|
|
|
|
text, err := reader.Readline()
|
|
|
|
|
if err != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text = strings.TrimSpace(text)
|
2026-05-13 13:51:09 +02:00
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
if text == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parts := strings.SplitN(strings.TrimSpace(text), " ", 3)
|
|
|
|
|
if len(parts) < 2 {
|
|
|
|
|
fmt.Println("error: you need to input a verb, uri [and post data]")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !slices.Contains(verbs, strings.ToLower(parts[0])) {
|
|
|
|
|
fmt.Println("error: verb must be one of " + strings.Join(verbs, ","))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !strings.HasPrefix(parts[1], "/") {
|
|
|
|
|
parts[1] = "/" + parts[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json := ""
|
|
|
|
|
if len(parts) == 3 {
|
|
|
|
|
// put /uri {json}
|
|
|
|
|
json = parts[2]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// put /uri/<Ret> [json]
|
|
|
|
|
data, err := readJSON(json)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = CallAPI(conf, parts[0], parts[1], data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("failed to call API: %s\n", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.SetPrompt("> ")
|
2026-05-13 13:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func encodeAuth(username, password string) string {
|
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CallAPI(conf *cfg.Config, verb, path, data string) error {
|
|
|
|
|
verb = strings.ToUpper(verb)
|
|
|
|
|
|
2026-05-13 13:51:09 +02:00
|
|
|
// we're using port-forwards anyway
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest(verb, conf.DefaultCluster.Uri+path, bytes.NewBuffer([]byte(data)))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
req.Header.Add("accept", "application/json")
|
|
|
|
|
req.Header.Add("Authorization", "Basic "+encodeAuth(conf.DefaultCluster.User, conf.DefaultCluster.Pass))
|
|
|
|
|
|
|
|
|
|
// actually execute the request
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read and print response
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to read response body: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 13:56:07 +02:00
|
|
|
return prettyfiJson(conf, body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func prettyfiJson(conf *cfg.Config, raw []byte) error {
|
|
|
|
|
if conf.HaveJQ {
|
|
|
|
|
cmd := exec.CommandContext(context.Background(), "jq", "-C")
|
|
|
|
|
cmd.Stdin = bytes.NewReader(raw)
|
|
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(out.String())
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 13:51:09 +02:00
|
|
|
var pretty bytes.Buffer
|
2026-05-18 13:56:07 +02:00
|
|
|
err := json.Indent(&pretty, raw, "", "\t")
|
|
|
|
|
if err != nil {
|
2026-05-13 13:51:09 +02:00
|
|
|
return fmt.Errorf("json parse error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(pretty.String())
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
// interactively read arbitrary JSON data from STDIN, which is
|
|
|
|
|
// virtually a repl inside the primary repl
|
|
|
|
|
func readJSON(input string) (string, error) {
|
|
|
|
|
data := ""
|
2026-05-13 13:51:09 +02:00
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
if input != "" {
|
|
|
|
|
data = input
|
|
|
|
|
} else {
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
2026-05-13 13:51:09 +02:00
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
if line == "" {
|
|
|
|
|
break
|
|
|
|
|
}
|
2026-05-13 13:51:09 +02:00
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
data += line
|
2026-05-13 13:51:09 +02:00
|
|
|
}
|
2026-05-29 12:42:02 +02:00
|
|
|
}
|
2026-05-13 13:51:09 +02:00
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
if data == "" {
|
|
|
|
|
return data, nil
|
|
|
|
|
}
|
2026-05-13 13:51:09 +02:00
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
// validate
|
|
|
|
|
check := map[string]any{}
|
|
|
|
|
err := json.Unmarshal([]byte(data), &check)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("error: input data is not proper JSON: %s", err)
|
2026-05-13 13:51:09 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 12:42:02 +02:00
|
|
|
return data, nil
|
2026-05-13 13:51:09 +02:00
|
|
|
}
|