-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
122 lines (109 loc) · 2.98 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cli
import (
"errors"
"log/slog"
"os"
"strings"
"github.com/livingsilver94/backee/installer"
"github.com/livingsilver94/backee/repo"
"github.com/livingsilver94/backee/repo/solver"
"github.com/livingsilver94/backee/service"
)
type keepassXC struct {
Path string `env:"KEEPASSXC_PATH" help:"KeepassXC database path."`
Password string `env:"KEEPASSXC_PASSWORD" help:"KeepassXC database password."`
}
type install struct {
Directory string `short:"C" type:"existingdir" help:"Change the base directory."`
KeepassXC keepassXC `embed:"" prefix:"keepassxc."`
PkgManager []string `name:"pkgmanager" help:"Override the package manager command for services."`
Variant string `help:"Specify the system variant."`
Services []string `arg:"" optional:"" help:"Services to install. Pass none to install all services in the base directory."`
}
const (
installedListFilename = "installed.txt"
)
func (in *install) Run() error {
if in.Directory == "" {
cwd, err := os.Getwd()
if err != nil {
return err
}
in.Directory = cwd
}
var fileList *os.File
defer func() {
fileList.Close()
}()
rep := repo.NewFSVariant(repo.NewOSFS(in.Directory), in.Variant)
srv, err := in.services(rep)
if err == nil && len(srv) == 0 {
err = errors.New("no services found")
}
if err != nil {
return err
}
ins := in.installer(rep, &fileList)
for _, s := range srv {
err := ins.Install(s)
if err != nil {
return err
}
}
return nil
}
func (in *install) services(rep repo.FS) ([]*service.Service, error) {
if len(in.PkgManager) != 0 {
service.DefaultPkgManager = in.PkgManager
}
if len(in.Services) == 0 {
return rep.AllServices()
}
services := make([]*service.Service, 0, len(in.Services))
for _, name := range in.Services {
srv, err := rep.Service(name)
if err != nil {
return nil, err
}
services = append(services, srv)
}
return services, nil
}
func (in *install) installer(rep repo.FS, fileList **os.File) installer.Installer {
var (
list installer.List
err error
)
*fileList, err = os.OpenFile(installedListFilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
slog.Error(err.Error() + "Failed opening the installation list file. Continuing without populating it")
list = installer.NewList()
} else {
list, err = installer.NewListCached(*fileList)
if err != nil {
slog.Error("Failed reading previous installation list")
}
}
opts := []installer.Option{
installer.WithCommonVars(envVars()),
installer.WithList(list),
}
if in.KeepassXC.Path != "" {
kee := solver.NewKeepassXC(in.KeepassXC.Path, in.KeepassXC.Password)
opts = append(
opts,
installer.WithVarSolvers(map[service.VarKind]repo.VarSolver{"keepassxc": kee}),
)
}
return installer.New(rep, opts...)
}
// envVars returns a map of environment variables.
func envVars() map[string]string {
env := os.Environ()
envMap := make(map[string]string, len(env))
for _, keyVal := range env {
key, val, _ := strings.Cut(keyVal, "=")
envMap[key] = val
}
return envMap
}