feat: implement backup command
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
73ebf998c7
commit
3a8296a8fe
2
TODO.md
2
TODO.md
|
@ -16,7 +16,7 @@
|
||||||
- [ ] `abra app`
|
- [ ] `abra app`
|
||||||
- [x] `ls`
|
- [x] `ls`
|
||||||
- [x] `new`
|
- [x] `new`
|
||||||
- [ ] `backup` (WIP: decentral1se)
|
- [x] `backup`
|
||||||
- [ ] `deploy`
|
- [ ] `deploy`
|
||||||
- [ ] `check`
|
- [ ] `check`
|
||||||
- [x] `version`
|
- [x] `version`
|
||||||
|
|
|
@ -1,6 +1,17 @@
|
||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
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"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,10 +20,70 @@ var backupAllServicesFlag = &cli.BoolFlag{
|
||||||
Name: "all",
|
Name: "all",
|
||||||
Value: false,
|
Value: false,
|
||||||
Destination: &backupAllServices,
|
Destination: &backupAllServices,
|
||||||
|
Aliases: []string{"a"},
|
||||||
Usage: "Backup all services",
|
Usage: "Backup all services",
|
||||||
}
|
}
|
||||||
|
|
||||||
var appBackupCommand = &cli.Command{
|
var appBackupCommand = &cli.Command{
|
||||||
Name: "backup",
|
Name: "backup",
|
||||||
Flags: []cli.Flag{backupAllServicesFlag},
|
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