feat: implement backup command
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
73ebf998c7
commit
3a8296a8fe
2
TODO.md
2
TODO.md
@ -16,7 +16,7 @@
|
||||
- [ ] `abra app`
|
||||
- [x] `ls`
|
||||
- [x] `new`
|
||||
- [ ] `backup` (WIP: decentral1se)
|
||||
- [x] `backup`
|
||||
- [ ] `deploy`
|
||||
- [ ] `check`
|
||||
- [x] `version`
|
||||
|
@ -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
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user