Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup and restore #17

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion command/auth/login/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package login

import (
"context"
"fmt"
"net/url"
"time"
Expand All @@ -23,6 +24,8 @@ var Command = &cli.Command{
},
Action: func(c *cli.Context) (err error) {

ctx := context.Background()

defer func() {
if err != nil {
err = cli.Exit(fmt.Errorf("while logging in: %w", err), 1)
Expand All @@ -36,7 +39,7 @@ var Command = &cli.Command{
serverURL := c.Args().First()

loginStartResponse := &server.LoginStartResponse{}
err = client.CallAPI(serverURL, "POST", "auth/login", nil, nil, client.JSONDecoder(loginStartResponse), 200)
err = client.CallAPI(ctx, serverURL, "", "POST", "auth/login", nil, nil, client.JSONDecoder(loginStartResponse), 200)
if err != nil {
return fmt.Errorf("while starting login process: %w", err)
}
Expand All @@ -60,7 +63,9 @@ var Command = &cli.Command{

var tr server.AccessTokenResponse
err = client.CallAPI(
ctx,
serverURL,
"",
"POST", "auth/access_token",
nil,
client.JSONEncoder(
Expand Down
11 changes: 10 additions & 1 deletion command/clone/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clone

import (
"archive/tar"
"context"
"errors"
"fmt"
"io"
Expand All @@ -11,6 +12,7 @@ import (
"path/filepath"
"strings"

"github.com/draganm/kartusche/common/auth"
"github.com/draganm/kartusche/common/client"
"github.com/draganm/kartusche/config"
"github.com/urfave/cli/v2"
Expand All @@ -28,6 +30,8 @@ var Command = &cli.Command{
}
}()

ctx := context.Background()

if c.NArg() != 1 {
return errors.New("kartusche url must be provided")
}
Expand Down Expand Up @@ -108,7 +112,12 @@ var Command = &cli.Command{

}

err = client.CallAPI(baseURL.String(), "GET", u.Path, nil, nil, dumpWriter, 200)
tkn, err := auth.GetTokenForServer(baseURL.String())
if err != nil {
return fmt.Errorf("could not get token for server: %w", err)
}

err = client.CallAPI(ctx, baseURL.String(), tkn, "GET", u.Path, nil, nil, dumpWriter, 200)
if err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion command/info/dbstats/command.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dbstats

import (
"context"
"errors"
"fmt"
"os"
"path"

"github.com/draganm/kartusche/common/auth"
"github.com/draganm/kartusche/common/client"
"github.com/draganm/kartusche/common/serverurl"
"github.com/draganm/kartusche/runtime"
Expand All @@ -24,6 +26,8 @@ var Command = &cli.Command{
}
}()

ctx := context.Background()

serverBaseURL, err := serverurl.BaseServerURL("")
if err != nil {
return err
Expand All @@ -39,8 +43,13 @@ var Command = &cli.Command{
return errors.New("name of kartusche must be provided")
}

tkn, err := auth.GetTokenForServer(serverBaseURL)
if err != nil {
return fmt.Errorf("could not get token for server: %w", err)
}

dbs := &runtime.DBStats{}
err = client.CallAPI(serverBaseURL, "GET", path.Join("kartusches", name, "info", "dbstats"), nil, nil, client.JSONDecoder(&dbs), 200)
err = client.CallAPI(ctx, serverBaseURL, tkn, "GET", path.Join("kartusches", name, "info", "dbstats"), nil, nil, client.JSONDecoder(&dbs), 200)
if err != nil {
return fmt.Errorf("while starting login process: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion command/info/handlers/command.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package handlers

import (
"context"
"errors"
"fmt"
"path"

"github.com/draganm/kartusche/common/auth"
"github.com/draganm/kartusche/common/client"
"github.com/draganm/kartusche/common/serverurl"
"github.com/draganm/kartusche/server"
Expand Down Expand Up @@ -37,8 +39,13 @@ var Command = &cli.Command{
return errors.New("name of kartusche must be provided")
}

tkn, err := auth.GetTokenForServer(serverBaseURL)
if err != nil {
return fmt.Errorf("could not get token for server: %w", err)
}

handlers := []server.HandlerInfo{}
err = client.CallAPI(serverBaseURL, "GET", path.Join("kartusches", name, "info", "handlers"), nil, nil, client.JSONDecoder(&handlers), 200)
err = client.CallAPI(context.Background(), serverBaseURL, tkn, "GET", path.Join("kartusches", name, "info", "handlers"), nil, nil, client.JSONDecoder(&handlers), 200)
if err != nil {
return fmt.Errorf("while starting login process: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion command/ls/command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ls

import (
"context"
"fmt"

"github.com/draganm/kartusche/common/auth"
"github.com/draganm/kartusche/common/client"
"github.com/draganm/kartusche/common/serverurl"
"github.com/urfave/cli/v2"
Expand All @@ -24,8 +26,13 @@ var Command = &cli.Command{
return err
}

tkn, err := auth.GetTokenForServer(serverBaseURL)
if err != nil {
return fmt.Errorf("could not get token for server: %w", err)
}

kl := []string{}
err = client.CallAPI(serverBaseURL, "GET", "kartusches", nil, nil, client.JSONDecoder(&kl), 200)
err = client.CallAPI(context.Background(), serverBaseURL, tkn, "GET", "kartusches", nil, nil, client.JSONDecoder(&kl), 200)
if err != nil {
return err
}
Expand Down
10 changes: 9 additions & 1 deletion command/rm/command.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package rm

import (
"context"
"errors"
"fmt"
"path"
"strings"

"github.com/draganm/kartusche/common/auth"
"github.com/draganm/kartusche/common/client"
"github.com/draganm/kartusche/common/serverurl"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -43,7 +45,13 @@ var Command = &cli.Command{
if err != nil {
return err
}
err = client.CallAPI(serverBaseURL, "DELETE", path.Join("kartusches", name), nil, nil, nil, 204)

tkn, err := auth.GetTokenForServer(serverBaseURL)
if err != nil {
return fmt.Errorf("could not get token for server: %w", err)
}

err = client.CallAPI(context.Background(), serverBaseURL, tkn, "DELETE", path.Join("kartusches", name), nil, nil, nil, 204)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion command/update/code/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package code

import (
"archive/tar"
"context"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"

"github.com/draganm/kartusche/common/auth"
"github.com/draganm/kartusche/common/client"
"github.com/draganm/kartusche/common/paths"
"github.com/draganm/kartusche/common/serverurl"
Expand Down Expand Up @@ -42,6 +44,11 @@ var Command = &cli.Command{
return err
}

tkn, err := auth.GetTokenForServer(serverBaseURL)
if err != nil {
return fmt.Errorf("could not get token for server: %w", err)
}

tf, err := os.CreateTemp("", "")
if err != nil {
return fmt.Errorf("while creating temp file: %w", err)
Expand Down Expand Up @@ -128,7 +135,7 @@ var Command = &cli.Command{
return fmt.Errorf("while seeking tar file to beginning: %w", err)
}

err = client.CallAPI(serverBaseURL, "PATCH", path.Join("kartusches", cfg.Name, "code"), nil, func() (io.Reader, error) { return tf, nil }, nil, 204)
err = client.CallAPI(context.Background(), serverBaseURL, tkn, "PATCH", path.Join("kartusches", cfg.Name, "code"), nil, func() (io.Reader, error) { return tf, nil }, nil, 204)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion command/upload/command.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package upload

import (
"context"
"fmt"
"io"
"os"
"path"
"path/filepath"

"github.com/draganm/kartusche/common/auth"
"github.com/draganm/kartusche/common/client"
"github.com/draganm/kartusche/common/serverurl"
"github.com/draganm/kartusche/config"
Expand Down Expand Up @@ -58,14 +60,19 @@ var Command = &cli.Command{
return err
}

tkn, err := auth.GetTokenForServer(serverBaseURL)
if err != nil {
return fmt.Errorf("could not get token for server: %w", err)
}

kf, err := os.Open(kartuscheFileName)
if err != nil {
return err
}

defer kf.Close()

err = client.CallAPI(serverBaseURL, "PUT", path.Join("kartusches", cfg.Name), nil, func() (io.Reader, error) { return kf, nil }, nil, 204)
err = client.CallAPI(context.Background(), serverBaseURL, tkn, "PUT", path.Join("kartusches", cfg.Name), nil, func() (io.Reader, error) { return kf, nil }, nil, 204)
if err != nil {
return err
}
Expand Down
18 changes: 4 additions & 14 deletions common/client/api_client.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
package client

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"

"github.com/draganm/kartusche/common/auth"
)

func CallAPI(
baseURL, method, pth string,
ctx context.Context,
baseURL, tkn, method, pth string,
q url.Values,
bodyEncoder func() (io.Reader, error),
responseCallback func(io.Reader) error,
expectedStatus int,
) error {

var tkn string
if !(strings.HasPrefix(pth, "/auth/") || strings.HasPrefix(pth, "auth/")) {
var err error
tkn, err = auth.GetTokenForServer(baseURL)
if err != nil {
return err
}
}

bu, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("while parsing server base url: %w", err)
Expand All @@ -47,7 +37,7 @@ func CallAPI(
}
}

req, err := http.NewRequest(method, bu.String(), body)
req, err := http.NewRequestWithContext(ctx, method, bu.String(), body)
if err != nil {
return fmt.Errorf("while creating request: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/cbroglie/mustache v1.3.1
github.com/dop251/goja v0.0.0-20221229151140-b95230a9dbad
github.com/draganm/bolted v0.9.2
github.com/draganm/bolted v0.10.1
github.com/fsnotify/fsnotify v1.5.4
github.com/go-logr/zapr v1.2.3
github.com/gorilla/mux v1.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/dop251/goja v0.0.0-20221229151140-b95230a9dbad h1:EikyYzLzjRNW8lz9VAI
github.com/dop251/goja v0.0.0-20221229151140-b95230a9dbad/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM=
github.com/draganm/bolted v0.9.2 h1:4T9FjEZFVF1rpcjP4J5TNOAyn8ayFDFHWo9k3TLwXfM=
github.com/draganm/bolted v0.9.2/go.mod h1:JzpeZ2BmuDuMggRz3gVL+1qX2C6b8+6pTgILbrZPztw=
github.com/draganm/bolted v0.10.1 h1:SzG/e88ElhABlfrqxzDB9e+qt6Ql+TMIwLDxPR5cC1M=
github.com/draganm/bolted v0.10.1/go.mod h1:JzpeZ2BmuDuMggRz3gVL+1qX2C6b8+6pTgILbrZPztw=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
Expand Down
Loading