aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-04-16 22:30:27 +0700
committerShulhan <ms@kilabit.info>2022-04-16 22:30:27 +0700
commit0156deaaaa0edbb93cfbf60a8cac99fc17d63a60 (patch)
tree0e373dd81bf402acc78ddc19356f78cb81d5cdb2 /cmd
parent8947cc8b6a9751b3cae3d4d22fdf3ac154f77dfd (diff)
downloadrescached-0156deaaaa0edbb93cfbf60a8cac99fc17d63a60.tar.xz
cmd/resolver: implement sub command to update environment
The env command now accept sub command "update" with argument path to the file or "-" for standard input. The content of file is formatted using JSON, the same as output of "env" command. If the content of file is valid, server will be restarted immediately.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/resolver/main.go42
-rw-r--r--cmd/resolver/resolver.go41
2 files changed, 81 insertions, 2 deletions
diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go
index 90fcc68..f2d4baf 100644
--- a/cmd/resolver/main.go
+++ b/cmd/resolver/main.go
@@ -21,6 +21,7 @@ const (
subCmdSearch = "search"
subCmdRemove = "remove"
+ subCmdUpdate = "update"
)
func main() {
@@ -29,6 +30,7 @@ func main() {
subCmd string
args []string
+ err error
optHelp bool
)
@@ -85,7 +87,29 @@ func main() {
}
case cmdEnv:
- rsol.doCmdEnv()
+ args = args[1:]
+ if len(args) == 0 {
+ rsol.doCmdEnv()
+ return
+ }
+
+ subCmd = strings.ToLower(args[0])
+ switch subCmd {
+ case subCmdUpdate:
+ args = args[1:]
+ if len(args) == 0 {
+ log.Fatalf("resolver: %s %s: missing file argument", rsol.cmd, subCmd)
+ }
+
+ err = rsol.doCmdEnvUpdate(args[0])
+ if err != nil {
+ log.Fatalf("resolver: %s", err)
+ }
+
+ default:
+ log.Printf("resolver: %s: unknown sub command: %s", rsol.cmd, subCmd)
+ os.Exit(2)
+ }
case cmdQuery:
args = args[1:]
@@ -174,6 +198,12 @@ env
Fetch the current server environment and print it as JSON format to
stdout.
+env update <path-to-file / "-">
+
+ Update the server environment from JSON formatted file.
+ If the argument is "-", the new environment is read from stdin.
+ If the environment is valid, the server will be restarted.
+
== Examples
@@ -218,5 +248,13 @@ Remove all caches in the server,
Fetch and print current server environment,
- $ resolver env`)
+ $ resolver env
+
+Update the server environment from JSON file in /tmp/env.json,
+
+ $ resolver env update /tmp/env.json
+
+Update the server environment by reading JSON from standard input,
+
+ $ cat /tmp/env.json | resolver env update -`)
}
diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go
index b7129cc..697953d 100644
--- a/cmd/resolver/resolver.go
+++ b/cmd/resolver/resolver.go
@@ -6,8 +6,10 @@ package main
import (
"encoding/json"
"fmt"
+ "io"
"log"
"math/rand"
+ "os"
"strings"
"time"
@@ -128,6 +130,45 @@ func (rsol *resolver) doCmdEnv() {
fmt.Printf("%s\n", envJson)
}
+// doCmdEnvUpdate update the server environment by reading the JSON formatted
+// environment from file or from stdin.
+func (rsol *resolver) doCmdEnvUpdate(fileOrStdin string) (err error) {
+ var (
+ resc = rsol.newRescachedClient()
+
+ env *rescached.Environment
+ envJson []byte
+ )
+
+ if fileOrStdin == "-" {
+ envJson, err = io.ReadAll(os.Stdin)
+ } else {
+ envJson, err = os.ReadFile(fileOrStdin)
+ }
+ if err != nil {
+ return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err)
+ }
+
+ err = json.Unmarshal(envJson, &env)
+ if err != nil {
+ return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err)
+ }
+
+ env, err = resc.EnvUpdate(env)
+ if err != nil {
+ return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err)
+ }
+
+ envJson, err = json.MarshalIndent(env, "", " ")
+ if err != nil {
+ return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err)
+ }
+
+ fmt.Printf("%s\n", envJson)
+
+ return nil
+}
+
func (rsol *resolver) doCmdQuery(args []string) {
var (
maxAttempts = defAttempts