Skip to content

Commit

Permalink
Move main.go back to the root
Browse files Browse the repository at this point in the history
  • Loading branch information
livingsilver94 committed Oct 6, 2023
1 parent 52f0b17 commit 800cfd5
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif

.PHONY: build
build:
$(GO) build -o $(OUTPATH) -ldflags "-X github.com/livingsilver94/backee/cmd.Version=$(VERSION)" bin/log.go bin/main.go
$(GO) build -o $(OUTPATH) -ldflags "-X github.com/livingsilver94/backee/cmd.Version=$(VERSION)" log.go main.go

.PHONY: check
check:
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions bin/cli/install.go → cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"os"
"strings"

"github.com/livingsilver94/backee"
"github.com/livingsilver94/backee/installer"
"github.com/livingsilver94/backee/repo"
"github.com/livingsilver94/backee/secret"
"github.com/livingsilver94/backee/service"
)

type keepassXC struct {
Expand Down Expand Up @@ -61,15 +61,15 @@ func (in *install) Run() error {
return nil
}

func (in *install) services(rep repo.FSRepo) ([]*backee.Service, error) {
func (in *install) services(rep repo.FSRepo) ([]*service.Service, error) {
if len(in.PkgManager) != 0 {
backee.DefaultPkgManager = in.PkgManager
service.DefaultPkgManager = in.PkgManager
}

if len(in.Services) == 0 {
return rep.AllServices()
}
services := make([]*backee.Service, 0, len(in.Services))
services := make([]*service.Service, 0, len(in.Services))
for _, name := range in.Services {
srv, err := rep.Service(name)
if err != nil {
Expand Down Expand Up @@ -100,7 +100,7 @@ func (in *install) installer(rep repo.FSRepo, fileList **os.File) installer.Inst
vrs.Common = envVars()
if in.KeepassXC.Path != "" {
kee := secret.NewKeepassXC(in.KeepassXC.Path, in.KeepassXC.Password)
vrs.RegisterStore(backee.VarKind("keepassxc"), kee)
vrs.RegisterStore(service.VarKind("keepassxc"), kee)
}

return installer.New(rep, installer.WithVariables(vrs), installer.WithList(list))
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions installer/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"path/filepath"
"strings"

"github.com/livingsilver94/backee"
"github.com/livingsilver94/backee/service"
)

type FileWriter interface {
loadSource(src string) error
writeDestination(dst string) error
}

func WritePath(dst backee.FilePath, src string, wr FileWriter) error {
func WritePath(dst service.FilePath, src string, wr FileWriter) error {
err := wr.loadSource(src)
if err != nil {
return err
Expand Down Expand Up @@ -97,15 +97,15 @@ func (w *CopyWriter) writeDestination(dst string) error {
return buff.Flush()
}

func writeFiles(files map[string]backee.FilePath, baseDir string, repl Replacer, wr FileWriter) error {
func writeFiles(files map[string]service.FilePath, baseDir string, repl Replacer, wr FileWriter) error {
var resolvedDst strings.Builder
for srcFile, dstFile := range files {
err := repl.ReplaceString(dstFile.Path, &resolvedDst)
if err != nil {
return err
}
err = WritePath(
backee.FilePath{Path: resolvedDst.String(), Mode: dstFile.Mode},
service.FilePath{Path: resolvedDst.String(), Mode: dstFile.Mode},
filepath.Join(baseDir, srcFile),
wr,
)
Expand Down
30 changes: 15 additions & 15 deletions installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package installer
import (
"log/slog"

"github.com/livingsilver94/backee"
"github.com/livingsilver94/backee/repo"
"github.com/livingsilver94/backee/service"
)

type Repository interface {
DataDir(srvName string) (string, error)
LinkDir(srvName string) (string, error)
ResolveDeps(srv *backee.Service) (repo.DepGraph, error)
ResolveDeps(srv *service.Service) (repo.DepGraph, error)
}

type VarStore interface {
Expand All @@ -35,7 +35,7 @@ func New(repository Repository, options ...Option) Installer {
return i
}

func (inst *Installer) Install(srv *backee.Service) error {
func (inst *Installer) Install(srv *service.Service) error {
if srv == nil {
return nil
}
Expand All @@ -55,7 +55,7 @@ func (inst *Installer) Install(srv *backee.Service) error {
return inst.installSingle(srv)
}

func (inst *Installer) installSingle(srv *backee.Service) error {
func (inst *Installer) installSingle(srv *service.Service) error {
log := slog.Default().WithGroup(srv.Name)
if inst.list.Contains(srv.Name) {
log.Info("Already installed")
Expand All @@ -66,15 +66,15 @@ func (inst *Installer) installSingle(srv *backee.Service) error {
return err
}
repl := NewReplacer(srv.Name, inst.variables)
performers := []Performer{
Setup,
PackageInstaller,
SymlinkPerformer(inst.repository, repl),
CopyPerformer(inst.repository, repl),
Finalizer(repl),
steps := []Step{
Setup{},
OSPackages{},
NewSymlinks(inst.repository, repl),
NewCopies(inst.repository, repl),
NewFinalization(repl),
}
for _, perf := range performers {
err := perf(log, srv)
for _, step := range steps {
err := step.Run(log, srv)
if err != nil {
return err
}
Expand All @@ -83,15 +83,15 @@ func (inst *Installer) installSingle(srv *backee.Service) error {
return nil
}

func (inst *Installer) cacheVars(srv *backee.Service) error {
func (inst *Installer) cacheVars(srv *service.Service) error {
datadir, err := inst.repository.DataDir(srv.Name)
if err != nil {
return err
}
err = inst.variables.Insert(
srv.Name,
backee.VarDatadir,
backee.VarValue{Kind: backee.ClearText, Value: datadir})
service.VarDatadir,
service.VarValue{Kind: service.ClearText, Value: datadir})
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions installer/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"strings"

"github.com/livingsilver94/backee"
"github.com/livingsilver94/backee/service"
"github.com/valyala/fasttemplate"
)

Expand All @@ -28,7 +28,7 @@ func (t Replacer) Replace(r io.Reader, w io.Writer) error {
for scanner.Scan() {
_, err := fasttemplate.ExecuteFunc(
scanner.Text(),
backee.VarOpenTag, backee.VarCloseTag,
service.VarOpenTag, service.VarCloseTag,
w,
t.replaceTag,
)
Expand All @@ -42,7 +42,7 @@ func (t Replacer) Replace(r io.Reader, w io.Writer) error {
func (t Replacer) ReplaceString(s string, w io.Writer) error {
_, err := fasttemplate.ExecuteFunc(
s,
backee.VarOpenTag, backee.VarCloseTag,
service.VarOpenTag, service.VarCloseTag,
w,
t.replaceTag,
)
Expand All @@ -52,18 +52,18 @@ func (t Replacer) ReplaceString(s string, w io.Writer) error {
func (t Replacer) ReplaceStringToString(s string) (string, error) {
return fasttemplate.ExecuteFuncStringWithErr(
s,
backee.VarOpenTag, backee.VarCloseTag,
service.VarOpenTag, service.VarCloseTag,
t.replaceTag,
)
}

func (t Replacer) replaceTag(w io.Writer, varName string) (int, error) {
if val, err := t.variables.Get(t.serviceName, varName); err == nil {
// Matched a variable local to the backee.
// Matched a variable local to the service.
return w.Write([]byte(val))
}

parentName, parentVar, found := strings.Cut(varName, backee.VarParentSep)
parentName, parentVar, found := strings.Cut(varName, service.VarParentSep)
if !found {
return 0, ErrNoVariable
}
Expand Down Expand Up @@ -92,12 +92,12 @@ func greedyTagSplitter(data []byte, atEOF bool) (advance int, token []byte, err
return 0, nil, nil
}

iTag := bytes.LastIndex(data, []byte(backee.VarOpenTag))
iTag := bytes.LastIndex(data, []byte(service.VarOpenTag))
if iTag < 0 {
// No tags in this string.
return len(data), data, nil
}
if bytes.Contains(data[iTag+len(backee.VarOpenTag):], []byte(backee.VarCloseTag)) {
if bytes.Contains(data[iTag+len(service.VarOpenTag):], []byte(service.VarCloseTag)) {
// Tag is correctly closed. Return whole string.
return len(data), data, nil
}
Expand Down
6 changes: 3 additions & 3 deletions installer/replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
"testing"

"github.com/livingsilver94/backee"
"github.com/livingsilver94/backee/installer"
"github.com/livingsilver94/backee/service"
)

func TestReplaceServiceVar(t *testing.T) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestReplaceExtraVar(t *testing.T) {

func TestReplaceParentVar(t *testing.T) {
vars := createVariables("var1", "value1", "var2", "value2")
vars.Insert("parent", "var1", backee.VarValue{Kind: backee.ClearText, Value: "parentValue1"})
vars.Insert("parent", "var1", service.VarValue{Kind: service.ClearText, Value: "parentValue1"})
vars.AddParent(serviceName, "parent")
repl := installer.NewReplacer(serviceName, vars)

Expand Down Expand Up @@ -81,7 +81,7 @@ func TestReplaceReader(t *testing.T) {
s := strings.NewReader(test.in)
vars := installer.NewVariables()
for key, val := range test.vars {
vars.Insert("service", key, backee.VarValue{Kind: backee.ClearText, Value: val})
vars.Insert("service", key, service.VarValue{Kind: service.ClearText, Value: val})
}
rep := installer.NewReplacer("service", vars)
writer := &bytes.Buffer{}
Expand Down
14 changes: 7 additions & 7 deletions installer/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"path/filepath"
"strings"

"github.com/livingsilver94/backee"
"github.com/livingsilver94/backee/service"
)

type Step interface {
Run(*slog.Logger, *backee.Service) error
Run(*slog.Logger, *service.Service) error
}

type Setup struct{}

func (Setup) Run(log *slog.Logger, srv *backee.Service) error {
func (Setup) Run(log *slog.Logger, srv *service.Service) error {
if srv.Setup == nil || *srv.Setup == "" {
return nil
}
Expand All @@ -29,7 +29,7 @@ func (Setup) Run(log *slog.Logger, srv *backee.Service) error {

type OSPackages struct{}

func (OSPackages) Run(log *slog.Logger, srv *backee.Service) error {
func (OSPackages) Run(log *slog.Logger, srv *service.Service) error {
if len(srv.Packages) == 0 {
return nil
}
Expand All @@ -52,7 +52,7 @@ func NewSymlinks(repo Repository, repl Replacer) Symlinks {
}
}

func (s Symlinks) Run(log *slog.Logger, srv *backee.Service) error {
func (s Symlinks) Run(log *slog.Logger, srv *service.Service) error {
if len(srv.Links) == 0 {
return nil
}
Expand All @@ -76,7 +76,7 @@ func NewCopies(repo Repository, repl Replacer) Copies {
}
}

func (c Copies) Run(log *slog.Logger, srv *backee.Service) error {
func (c Copies) Run(log *slog.Logger, srv *service.Service) error {
if len(srv.Copies) == 0 {
return nil
}
Expand All @@ -98,7 +98,7 @@ func NewFinalization(repl Replacer) Finalization {
}
}

func (f Finalization) Run(log *slog.Logger, srv *backee.Service) error {
func (f Finalization) Run(log *slog.Logger, srv *service.Service) error {
if srv.Finalize == nil || *srv.Finalize == "" {
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions installer/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

"github.com/livingsilver94/backee"
"github.com/livingsilver94/backee/service"
)

var (
Expand All @@ -16,13 +16,13 @@ type Variables struct {
Common map[string]string

resolved map[string]value
stores map[backee.VarKind]VarStore
stores map[service.VarKind]VarStore
}

func NewVariables() Variables {
return Variables{
resolved: make(map[string]value),
stores: make(map[backee.VarKind]VarStore),
stores: make(map[service.VarKind]VarStore),
}
}

Expand All @@ -46,7 +46,7 @@ func (c Variables) AddParent(srv, parent string) error {
// Insert saves value for a service named srv under key.
// If the value is not clear text, it is resolved immediately and then cached.
// If key is already present for srv, Insert is no-op.
func (c Variables) Insert(srv, key string, value backee.VarValue) error {
func (c Variables) Insert(srv, key string, value service.VarValue) error {
switch _, err := c.Get(srv, key); err {
case ErrNoService:
c.resolved[srv] = newValue()
Expand All @@ -56,7 +56,7 @@ func (c Variables) Insert(srv, key string, value backee.VarValue) error {
return nil
}
var v string
if kind := value.Kind; kind == backee.ClearText {
if kind := value.Kind; kind == service.ClearText {
v = value.Value
} else {
store, ok := c.stores[kind]
Expand All @@ -73,7 +73,7 @@ func (c Variables) Insert(srv, key string, value backee.VarValue) error {
return nil
}

func (c Variables) InsertMany(srv string, values map[string]backee.VarValue) error {
func (c Variables) InsertMany(srv string, values map[string]service.VarValue) error {
for key, value := range values {
err := c.Insert(srv, key, value)
if err != nil {
Expand Down Expand Up @@ -113,7 +113,7 @@ func (c Variables) Length() int {
return len(c.resolved)
}

func (c Variables) RegisterStore(kind backee.VarKind, store VarStore) {
func (c Variables) RegisterStore(kind service.VarKind, store VarStore) {
c.stores[kind] = store
}

Expand Down
Loading

0 comments on commit 800cfd5

Please sign in to comment.