Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #100 from bacongobbler/fix-93
Browse files Browse the repository at this point in the history
use `id` to determine current user and group
  • Loading branch information
Matthew Fisher committed Jul 11, 2018
2 parents f7f6fc8 + 0882e5c commit 51a7106
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 52 deletions.
47 changes: 0 additions & 47 deletions cmd/gofish/ensure_darwin.go

This file was deleted.

22 changes: 17 additions & 5 deletions cmd/gofish/ensure_unix.go
@@ -1,4 +1,4 @@
// +build !windows,!darwin
// +build !windows

package main

Expand All @@ -10,10 +10,22 @@ import (
)

func ensureDirectories(dirs []string) error {
curUser := os.Getenv("USER")
if curUser == "" {
return fmt.Errorf("Could not determine current user: $USER is not present in the environment")
userCmd := exec.Command("id", "-un")
userOutput, err := userCmd.Output()
if err != nil {
return err
}
// strip the newline character from the end
curUser := strings.TrimSuffix(string(userOutput), "\n")

groupCmd := exec.Command("id", "-gn", curUser)
groupOutput, err := groupCmd.Output()
if err != nil {
return err
}
// strip the newline character from the end
curGroup := strings.TrimSuffix(string(groupOutput), "\n")

fmt.Printf("The following new directories will be created:\n")
fmt.Println(strings.Join(dirs, "\n"))
for _, dir := range dirs {
Expand All @@ -27,7 +39,7 @@ func ensureDirectories(dirs []string) error {
} else if !fi.IsDir() {
return fmt.Errorf("%s must be a directory", dir)
}
cmd := exec.Command("sudo", "chown", fmt.Sprintf("%s:%s", curUser, curUser), dir)
cmd := exec.Command("sudo", "chown", fmt.Sprintf("%s:%s", curUser, curGroup), dir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand Down

0 comments on commit 51a7106

Please sign in to comment.