Skip to content

Commit

Permalink
Merge pull request #929 from pascalbreuninger/fix/dotfiles-setup
Browse files Browse the repository at this point in the history
fix(cli): ensure dotfiles script is executable; clone dotfiles into home dir
  • Loading branch information
89luca89 authored Feb 26, 2024
2 parents f580feb + d24dde5 commit f33058e
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions cmd/agent/workspace/install_dotfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/pkg/git"
"github.com/loft-sh/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ func NewInstallDotfilesCmd(flags *flags.GlobalFlags) *cobra.Command {
// Run runs the command logic
func (cmd *InstallDotfilesCmd) Run(ctx context.Context) error {
logger := log.Default.ErrorStreamOnly()
targetDir := "dotfiles"
targetDir := filepath.Join(os.Getenv("HOME"), "dotfiles")

_, err := os.Stat(targetDir)
if err != nil {
Expand All @@ -67,11 +68,15 @@ func (cmd *InstallDotfilesCmd) Run(ctx context.Context) error {

if cmd.InstallScript != "" {
logger.Infof("Executing install script %s", cmd.InstallScript)
command := "./" + strings.TrimPrefix("./", cmd.InstallScript) + cmd.InstallScript

scriptCmd := exec.Command("./" + cmd.InstallScript)
err := ensureExecutable(command)
if err != nil {
return errors.Wrapf(err, "failed to make install script %s executable", command)
}

scriptCmd := exec.Command(command)
writer := logger.Writer(logrus.InfoLevel, false)

scriptCmd.Stdout = writer
scriptCmd.Stderr = writer

Expand All @@ -97,22 +102,15 @@ var scriptLocations = []string{
func setupDotfiles(logger log.Logger) error {
for _, command := range scriptLocations {
logger.Debugf("Trying executing %s", command)
checkCmd := exec.Command("test", "-f", command)
err := checkCmd.Run()
if err != nil {
logger.Debugf("File %s not found", command)
continue
}
writer := logger.Writer(logrus.InfoLevel, false)

chmodCmd := exec.Command("chmod", "+x", command)
err = chmodCmd.Run()
err := ensureExecutable(command)
if err != nil {
logger.Infof("Chmod of %s was unsuccessful: %v", command, err)
logger.Infof("Failed to make install script %s executable: %v", command, err)
logger.Debug("Trying next location")
continue
}

writer := logger.Writer(logrus.InfoLevel, false)

scriptCmd := exec.Command(command)
scriptCmd.Stdout = writer
scriptCmd.Stderr = writer
Expand Down Expand Up @@ -158,3 +156,19 @@ func setupDotfiles(logger log.Logger) error {

return nil
}

func ensureExecutable(path string) error {
checkCmd := exec.Command("test", "-f", path)
err := checkCmd.Run()
if err != nil {
return errors.Wrapf(err, "install script %s not found", path)
}

chmodCmd := exec.Command("chmod", "+x", path)
err = chmodCmd.Run()
if err != nil {
return errors.Wrapf(err, "failed to make install script %s executable", path)
}

return nil
}

0 comments on commit f33058e

Please sign in to comment.