Skip to content

Commit

Permalink
Add teedy-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
MattHodge committed May 21, 2021
1 parent 49c9781 commit bdd429c
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# Backup test dir
backup_test/
.env_backup
14 changes: 12 additions & 2 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ local_resource("go mod tidy",
trigger_mode = TRIGGER_MODE_MANUAL,
)

local_resource("clear + backup",
cmd = "rm -rf test-bak/ && go run main.go && tree test-bak/",
local_resource("teedy-cli backup",
cmd = "source .env_backup && rm -rf backup_test/ && go run cmd/teedy-cli/main.go backup --url 'https://teedy.hdgbo.xyz' --destinationpath './backup_test' && tree backup_test/",
auto_init = False,
trigger_mode = TRIGGER_MODE_MANUAL,
)

local_resource("teedy-cli restore",
cmd = "go run cmd/teedy-cli/main.go restore --url 'http://localhost:8080' --sourcepath './backup_test'",
auto_init = False,
trigger_mode = TRIGGER_MODE_MANUAL,
env = {
"TEEDY_USERNAME": "admin",
"TEEDY_PASSWORD": "superSecure"
},
)
98 changes: 98 additions & 0 deletions cmd/teedy-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"fmt"
"log"
"net/url"
"os"

"github.com/MattHodge/go-teedy/backup"
"github.com/MattHodge/go-teedy/restore"
"github.com/MattHodge/go-teedy/teedy"
"github.com/alexflint/go-arg"
)

type BackupCmd struct {
DestinationPath string `arg:"-d,required" placeholder:"DST" help:"Path to backup to"`
}

type RestoreCmd struct {
SourcePath string `arg:"-s,required" placeholder:"SRC" help:"Path to restore from"`
}

type args struct {
Backup *BackupCmd `arg:"subcommand:backup"`
Restore *RestoreCmd `arg:"subcommand:restore"`
URL string `arg:"-u,required" help:"Teedy Server URL"`
}

func (a *args) Description() string {
return "teedy-cli allows you to backup from and restore to a https://teedy.io/ server.\n"
}

func main() {
var args args
arg.MustParse(&args)

// validate params
CheckURL(args.URL)
username := GetEnvMustExist("TEEDY_USERNAME")
password := GetEnvMustExist("TEEDY_PASSWORD")

client, err := teedy.NewClient(args.URL, username, password)

if err != nil {
log.Fatalf("can't get teedy client: %v", err)
}

switch {
case args.Backup != nil:
fmt.Printf("Running backup of %s to '%s'\n", args.URL, args.Backup.DestinationPath)
backupClient := backup.NewBackupClient(client, args.Backup.DestinationPath)

err = backupClient.Tags()

if err != nil {
log.Fatalf("unable to backup tags: %v", err)
}

err = backupClient.Documents()

if err != nil {
log.Fatalf("unable to backup documents: %v", err)
}
case args.Restore != nil:
fmt.Printf("Running restore of '%s' to '%s'\n", args.Restore.SourcePath, args.URL)

restoreClient := restore.NewRestoreClient(client, args.Restore.SourcePath)
err = restoreClient.Tags()

if err != nil {
log.Fatalf("unable to restore tags: %v", err)
}

_, err = restoreClient.Documents()

if err != nil {
log.Fatalf("unable to restore docs: %v", err)
}
}
}

func GetEnvMustExist(key string) string {
envvar := os.Getenv(key)

if envvar == "" {
log.Fatalf("Unable to load value for '%s' from environment.", key)
}

return envvar
}

func CheckURL(uri string) {
_, err := url.ParseRequestURI(uri)

if err != nil {
log.Fatalf("URL '%s' is not valid", uri)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/1set/gut v0.0.0-20201117175203-a82363231997
github.com/alexflint/go-arg v1.4.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-resty/resty/v2 v2.6.0
github.com/jarcoal/httpmock v1.0.8
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/1set/gut v0.0.0-20201117175203-a82363231997 h1:za2jSkE1Rx56hTzBko3ZZ4gA/nq+rA/jVovWuAF4jyo=
github.com/1set/gut v0.0.0-20201117175203-a82363231997/go.mod h1:DpCCAL0dgBMQdiqPUIIRpdU9zNcIZwJjW+L/8Mb30mw=
github.com/alexflint/go-arg v1.4.1 h1:QX73/uQKvH/veFcp6RQY4RFyB4fPGSloi3VJSBE61Uo=
github.com/alexflint/go-arg v1.4.1/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -11,6 +15,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
Expand Down
Binary file added restore/testdata/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bdd429c

Please sign in to comment.