Skip to content

Commit

Permalink
consolidate unwanted dirs provisionenr into dir provisioner
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Dec 27, 2023
1 parent 27e9163 commit 65b5bd9
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 57 deletions.
3 changes: 1 addition & 2 deletions base/fup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Config struct {
Binaries []entity.Binary `yaml:"binary"`
Cargo []CargoPkg `yaml:"rust"`
DnfRepos []entity.DnfRepo `yaml:"dnf_repo"`
EnsureDirs []string `yaml:"dir"`
EnsureDirs []entity.Dir `yaml:"dir"`
EnsureLines []LineInFile `yaml:"line"`
Flatpak entity.Flatpak `yaml:"flatpak"`
GithubUserKey UserKey `yaml:"github_key"`
Expand All @@ -44,7 +44,6 @@ type Config struct {
Tasks []Task `yaml:"task"`
Templates []Template `yaml:"template"`
UserInGroup UserInGroupSpec `yaml:"user_group"`
UnwantedDirs []string `yaml:"rmdir"`
}

func (c Config) IsRemote() bool {
Expand Down
2 changes: 1 addition & 1 deletion common/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func EnsureLinesInFile(file string, lines []string) error {

file = internal.ExpandUser(file)
dir, _ := path.Split(file)
if err := internal.EnsureDir(dir); err != nil {
if err := internal.EnsureDirExists(dir); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion entity/aptrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (AptRepo) ensureKeyFile(keyUrl, keyRingFile string) error {
}

func (a AptRepo) Install() error {
err := internal.EnsureDir(keyRingsDir)
err := internal.EnsureDirExists(keyRingsDir)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions entity/dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package entity

type Dir struct {
Absent bool `yaml:"absent"`
Name string `yaml:"name"`
}
12 changes: 11 additions & 1 deletion internal/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ package internal

import "os"

func EnsureDir(dir string) error {
func EnsureDirAbsent(dir string) error {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
return nil
} else if err != nil {
return err
}
return os.RemoveAll(dir)
}

func EnsureDirExists(dir string) error {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
err = os.MkdirAll(dir, 0744)
Expand Down
35 changes: 30 additions & 5 deletions provision/ensuredirs.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
package provision

import (
"errors"
"fmt"

"github.com/femnad/fup/base"
"github.com/femnad/fup/entity"
"github.com/femnad/fup/internal"
)

func doEnsureDirs(dirs []string) error {
func ensureDirAbsent(dir entity.Dir) error {
dirName := internal.ExpandUser(dir.Name)
if err := internal.EnsureDirAbsent(dirName); err != nil {
return fmt.Errorf("error removing directory %s: %v", dirName, err)
}

return nil
}

func ensureDirExist(dir entity.Dir) error {
dirName := internal.ExpandUser(dir.Name)
if err := internal.EnsureDirExists(dirName); err != nil {
return fmt.Errorf("error creating directory %s: %v", dirName, err)
}

return nil
}

func doEnsureDirs(dirs []entity.Dir) error {
var errs []error

for _, dir := range dirs {
dir = internal.ExpandUser(dir)
if err := internal.EnsureDir(dir); err != nil {
return fmt.Errorf("error ensuring directory %s: %v", dir, err)
var err error
if dir.Absent {
err = ensureDirAbsent(dir)
} else {
err = ensureDirExist(dir)
}
errs = append(errs, err)
}

return nil
return errors.Join(errs...)
}

func ensureDirs(config base.Config) error {
Expand Down
7 changes: 0 additions & 7 deletions provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func NewProvisioner(cfg base.Config, filter []string) (Provisioner, error) {
{"line", p.ensureLines},
{"flatpak", p.flatpakInstall},
{"snap", p.snapInstall},
{"rmdir", p.unwantedDirs},
{"group", p.userInGroup},
{"post", p.runPostFlightTasks},
}
Expand Down Expand Up @@ -242,12 +241,6 @@ func (p Provisioner) sshClone() error {
return sshClone(p.Config)
}

func (p Provisioner) unwantedDirs() error {
internal.Log.Noticef("Removing unwanted dirs")

return removeUnwantedDirs(p.Config)
}

func (p Provisioner) userInGroup() error {
internal.Log.Noticef("Ensuring user is in desired groups")

Expand Down
2 changes: 1 addition & 1 deletion provision/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func persist(s base.Service) error {
serviceFilePath := getServiceFilePath(s)
if !s.System {
dir, _ := path.Split(serviceFilePath)
if err := internal.EnsureDir(dir); err != nil {
if err := internal.EnsureDirExists(dir); err != nil {
return err
}
}
Expand Down
37 changes: 0 additions & 37 deletions provision/unwanteddirs.go

This file was deleted.

2 changes: 1 addition & 1 deletion provision/userkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ensureUserKeys(user string) error {

keyFile := internal.ExpandUser(authorizedKeysFile)
dir, _ := path.Split(keyFile)
if err = internal.EnsureDir(dir); err != nil {
if err = internal.EnsureDirExists(dir); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion remote/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func Download(url, target string) error {
}

dir, _ := path.Split(target)
if err = internal.EnsureDir(dir); err != nil {
if err = internal.EnsureDirExists(dir); err != nil {
return err
}

Expand Down

0 comments on commit 65b5bd9

Please sign in to comment.