feat: implement backup command
continuous-integration/drone/push Build is passing Details

This commit is contained in:
decentral1se 2021-08-28 19:10:19 +02:00
parent 73ebf998c7
commit 3a8296a8fe
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 74 additions and 3 deletions

View File

@ -16,7 +16,7 @@
- [ ] `abra app`
- [x] `ls`
- [x] `new`
- [ ] `backup` (WIP: decentral1se)
- [x] `backup`
- [ ] `deploy`
- [ ] `check`
- [x] `version`

View File

@ -1,6 +1,17 @@
package app
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/config"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -9,10 +20,70 @@ var backupAllServicesFlag = &cli.BoolFlag{
Name: "all",
Value: false,
Destination: &backupAllServices,
Aliases: []string{"a"},
Usage: "Backup all services",
}
var appBackupCommand = &cli.Command{
Name: "backup",
Flags: []cli.Flag{backupAllServicesFlag},
Name: "backup",
Flags: []cli.Flag{backupAllServicesFlag},
ArgsUsage: "<service>",
Action: func(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided"))
}
if c.Args().Get(1) != "" && backupAllServices {
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use '<service>' and '--all' together"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
abraSh := path.Join(config.ABRA_DIR, "apps", appEnv.Type, "abra.sh")
if _, err := os.Stat(abraSh); err != nil {
if os.IsNotExist(err) {
logrus.Fatalf("'%s' does not exist?", abraSh)
}
logrus.Fatal(err)
}
sourceCmd := fmt.Sprintf("source %s", abraSh)
execCmd := "abra_backup"
if !backupAllServices {
serviceName := c.Args().Get(1)
if serviceName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no service(s) target provided"))
}
execCmd = fmt.Sprintf("abra_backup_%s", serviceName)
}
bytes, err := ioutil.ReadFile(abraSh)
if err != nil {
logrus.Fatal(err)
}
if !strings.Contains(string(bytes), execCmd) {
logrus.Fatalf("%s doesn't have a '%s' function", appEnv.Type, execCmd)
}
sourceAndExec := fmt.Sprintf("%s; %s", sourceCmd, execCmd)
cmd := exec.Command("bash", "-c", sourceAndExec)
output, err := cmd.Output()
if err != nil {
logrus.Fatal(err)
}
fmt.Print(string(output))
return nil
},
}