Skip to content

Commit

Permalink
feat: flag for on theme change commands, set file execute perm based …
Browse files Browse the repository at this point in the history
…on source perms
  • Loading branch information
PhilippHeuer committed Aug 18, 2024
1 parent 1a769cb commit 6ba90c8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 28 deletions.
25 changes: 2 additions & 23 deletions pkg/cmd/clean.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cmd

import (
"os"

"github.com/PhilippHeuer/dotfiles-cli/pkg/config"
"github.com/PhilippHeuer/dotfiles-cli/pkg/dotfiles"
"github.com/PhilippHeuer/dotfiles-cli/pkg/util"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -29,27 +28,7 @@ func cleanCmd() *cobra.Command {
}

// remove files
var managedFiles []string
for _, file := range state.ManagedFiles {
log.Debug().Str("file", file).Msg("removing file")
if dryRun {
continue
}

// check if file exists
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Trace().Str("file", file).Msg("file does not exist, already deleted")
continue
}

// delete file
removeErr := os.Remove(file)
if removeErr != nil {
managedFiles = append(managedFiles, file)
log.Warn().Str("file", file).Msg("failed to remove file")
}
}
state.ManagedFiles = managedFiles
state.ManagedFiles = dotfiles.DeleteManagedFiles(state.ManagedFiles, dryRun)

// save state
if !dryRun {
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ type ThemeConfig struct {
}

type ThemeCommand struct {
Command string `yaml:"command"`
Command string `yaml:"command"`
OnChange bool `yaml:"on_change"`
Condition string `yaml:"condition"`
}

type Dir struct {
Expand Down
28 changes: 24 additions & 4 deletions pkg/dotfiles/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package dotfiles

import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"

"github.com/PhilippHeuer/dotfiles-cli/pkg/config"
"github.com/PhilippHeuer/dotfiles-cli/pkg/util"
"github.com/adrg/xdg"
"github.com/cidverse/go-rules/pkg/expr"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -50,6 +50,7 @@ func Install(dir string, mode string, dryRun bool) error {

// theme
themeName := os.Getenv("DOTFILE_THEME")
originalThemeName := state.Theme
if themeName != "" {
state.Theme = themeName
} else {
Expand Down Expand Up @@ -194,7 +195,7 @@ func Install(dir string, mode string, dryRun bool) error {

// theme activation
if theme != nil && !dryRun {
err = activateTheme(theme)
err = activateTheme(theme, originalThemeName)
if err != nil {
log.Fatal().Err(err).Str("theme", themeName).Msg("failed to activate theme")
}
Expand All @@ -204,13 +205,32 @@ func Install(dir string, mode string, dryRun bool) error {
}

// activateTheme executes the theme activation commands, if available
func activateTheme(theme *config.ThemeConfig) error {
func activateTheme(theme *config.ThemeConfig, originalThemeName string) error {
for _, cmd := range theme.Commands {
log.Debug().Str("command", cmd.Command).Msg("executing theme command")

if cmd.Condition != "" {
match, err := expr.EvalBooleanExpression(cmd.Condition, map[string]interface{}{
"env": os.Environ(),
})
if err != nil {
log.Warn().Err(err).Str("condition", cmd.Condition).Msg("failed to evaluate theme activation command condition")
continue
}

if !match {
continue
}
}
if cmd.OnChange && originalThemeName == theme.Name {
log.Debug().Str("command", cmd.Command).Msg("command not executed, theme did not change")
continue
}

err := util.RunCommand(cmd.Command)
if err != nil {
return errors.Join(fmt.Errorf("failed to execute theme activation command: %s", cmd), err)
log.Warn().Err(err).Str("command", cmd.Command).Msg("failed to execute theme activation command")
// return errors.Join(fmt.Errorf("failed to execute theme activation command: %s", cmd), err)
}
}

Expand Down
37 changes: 37 additions & 0 deletions pkg/util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ func copyFile(source string, target string) error {
return err
}

if isExecutable(source) {
err = makeExecutableByOwner(target)
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -139,6 +146,13 @@ func copyFileWithTemplate(source string, target string, data map[string]string)
return err
}

if isExecutable(source) {
err = makeExecutableByOwner(target)
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -175,3 +189,26 @@ func createOrUpdateSymlink(source string, target string) error {

return nil
}

func isExecutable(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}

return info.Mode()&0100 != 0
}

func makeExecutableByOwner(path string) error {
info, err := os.Stat(path)
if err != nil {
return err
}

newMode := info.Mode() | 0100
if err := os.Chmod(path, newMode); err != nil {
return err
}

return nil
}

0 comments on commit 6ba90c8

Please # to comment.