Skip to content

Commit

Permalink
src: implement backup
Browse files Browse the repository at this point in the history
  • Loading branch information
yuk7 committed Jul 15, 2021
1 parent b94980c commit 021a76e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package backup

import (
"os"
"os/exec"

"github.com/yuk7/wsldl/lib/utils"
)

//Execute is default run entrypoint.
func Execute(name string, args []string) {
opttar := false
optreg := true
switch len(args) {
case 0:
opttar = true
optreg = true

case 1:
switch args[0] {
case "--tar":
opttar = true
case "--reg":
optreg = true
}

default:
utils.ErrorExit(os.ErrInvalid, true, true, false)
}

if optreg {
lxguid, err := utils.WslGetUUID(name)
if err != nil {
utils.ErrorExit(err, true, true, false)
}
regexe := os.Getenv("SystemRoot") + "\\System32\\reg.exe"
regpath := "HKEY_CURRENT_USER\\" + utils.LxssBaseKey + "\\" + lxguid
_, err = exec.Command(regexe, "export", regpath, "backup.reg", "/y").Output()
if err != nil {
utils.ErrorExit(err, true, true, false)
}
}
if opttar {
wslexe := os.Getenv("SystemRoot") + "\\System32\\wsl.exe"
_, err := exec.Command(wslexe, "--export", name, "backup.tar").Output()
if err != nil {
utils.ErrorExit(err, true, true, false)
}
}
}

// ShowHelp shows help message
func ShowHelp(showTitle bool) {
if showTitle {
println("Usage:")
}
println(" backup [contents]")
println(" - `--tar`: Output backup.tar to the current directory")
println(" - `--reg`: Output settings registry file to the current directory")
}
7 changes: 6 additions & 1 deletion src/help/help.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package help

import (
"github.com/yuk7/wsldl/backup"
"github.com/yuk7/wsldl/clean"
"github.com/yuk7/wsldl/config"
"github.com/yuk7/wsldl/get"
Expand All @@ -18,7 +19,9 @@ func Execute(args []string) {
case "config", "set":
config.ShowHelp(true)
case "get":
config.ShowHelp(true)
get.ShowHelp(true)
case "backup":
backup.ShowHelp(true)
case "clean":
clean.ShowHelp(true)
case "help":
Expand All @@ -39,6 +42,8 @@ func ShowHelpAll() {
println()
get.ShowHelp(false)
println()
backup.ShowHelp(false)
println()
clean.ShowHelp(false)
println()
ShowHelp(false)
Expand Down
4 changes: 4 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"

"github.com/yuk7/wsldl/backup"
"github.com/yuk7/wsldl/clean"
"github.com/yuk7/wsldl/config"
"github.com/yuk7/wsldl/get"
Expand Down Expand Up @@ -43,6 +44,9 @@ func main() {
case "get":
get.Execute(name, os.Args[2:])

case "backup":
backup.Execute(name, os.Args[2:])

case "clean":
clean.Execute(name, os.Args[2:])

Expand Down

0 comments on commit 021a76e

Please sign in to comment.