Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
surminus committed May 31, 2023
1 parent 5e3a7fc commit f728373
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 22 deletions.
21 changes: 19 additions & 2 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func LinkExists(path string) bool {
return false
}

// IsDirectory is an alias for DirExists
func IsDirectory(path string) bool {
return DirExists(path)
}

// DirExists returns true if the file exists, and is a directory
func DirExists(path string) bool {
if info, err := os.Stat(path); err == nil {
Expand All @@ -98,14 +103,26 @@ func DirExists(path string) bool {
return false
}

// ListFiles returns all files within a path
func ListFiles(path string) (files []string, err error) {
err = filepath.Walk(path, func(p string, f os.FileInfo, err error) error {
if err != nil {
return err
}

files = append(files, p)
return nil
})

return files, err
}

// MatchChmod returns true if the permissions of the path match
func MatchChmod(path string, perms fs.FileMode) bool {
if info, err := os.Stat(path); err == nil {
if info.Mode() == perms {
return true
}

log.Print(info.Mode().String())
} else {
log.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion resources/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (a *Apt) createApt(log *viaduct.Logger) error {
// receiveSigningKey will fetch a signing key
func (a *Apt) receiveSigningKey(log *viaduct.Logger) error {
if viaduct.FileExists(a.signingKeyPath()) {
log.Noop(a.signingKeyPath())
log.Noop("Signing key: ", a.signingKeyPath())
return nil
}

Expand Down Expand Up @@ -254,6 +254,7 @@ func (a *Apt) receiveSigningKey(log *viaduct.Logger) error {
}()
}

log.Info("Signing key: ", a.signingKeyPath())
return nil
}

Expand Down
38 changes: 32 additions & 6 deletions resources/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,41 @@ func setDirectoryPermissions(
log.Info(chmodmsg)
}

if viaduct.MatchChown(path, uid, gid) {
log.Noop(chownmsg)
} else {
err := os.Chown(path, uid, gid)
if err != nil {
// If it's a directory, recursively set ownership permissions
if viaduct.IsDirectory(path) {
chownmsg += " (recursive)"
var wasupdated bool

if files, err := viaduct.ListFiles(path); err == nil {
for _, f := range files {
if viaduct.MatchChown(f, uid, gid) {
continue
}

wasupdated = true
if err := os.Chown(f, uid, gid); err != nil {
return err
}
}
} else {
return err
}

log.Info(chownmsg)
if wasupdated {
log.Info(chownmsg)
} else {
log.Noop(chownmsg)
}
} else {
if viaduct.MatchChown(path, uid, gid) {
log.Noop(chownmsg)
} else {
if err := os.Chown(path, uid, gid); err != nil {
return err
}

log.Info(chownmsg)
}
}

return nil
Expand Down
15 changes: 2 additions & 13 deletions resources/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,7 @@ func (g *Git) createGit(log *viaduct.Logger) error {
return nil
}

var pathExists bool
if viaduct.FileExists(path) {
if !g.Ensure {
log.Noop(logmsg)
return nil
}

pathExists = true
}

if pathExists {
if viaduct.FileExists(path) && g.Ensure {
r, err := git.PlainOpen(path)
if err != nil {
return err
Expand All @@ -150,7 +140,6 @@ func (g *Git) createGit(log *viaduct.Logger) error {

// nolint:exhaustivestruct
err = w.Pull(&git.PullOptions{
// Auth: auth,
RemoteName: "origin",
Progress: os.Stdout,
ReferenceName: plumbing.ReferenceName(g.Reference),
Expand All @@ -166,7 +155,7 @@ func (g *Git) createGit(log *viaduct.Logger) error {
}
}

if !pathExists {
if !viaduct.FileExists(path) {
progress := os.Stdout

if viaduct.Config.Quiet || viaduct.Config.Silent {
Expand Down
Loading

0 comments on commit f728373

Please sign in to comment.