Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion cmd/vm_sudoers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/hashicorp/cli"
"github.com/mattn/go-isatty"
"github.com/roots/trellis-cli/pkg/vm"
"github.com/roots/trellis-cli/trellis"
)
Expand Down Expand Up @@ -32,12 +34,29 @@ func (c *VmSudoersCommand) Run(args []string) int {

hostResolver := vm.NewHostsFileResolver([]string{})
cmd := hostResolver.SudoersCommand()
line := fmt.Sprintf("%%staff ALL=(root:wheel) NOPASSWD:NOSETENV: %s", strings.Join(cmd, " "))

if stdoutIsTerminal() {
c.UI.Warn("The following sudoers rule lets trellis-cli update /etc/hosts without prompting for your password.")
c.UI.Warn("")
c.UI.Warn("To install it, re-run this command and pipe the output to tee:")
c.UI.Warn("")
c.UI.Warn(" trellis vm sudoers | sudo tee /etc/sudoers.d/trellis")
c.UI.Warn("")
c.UI.Warn("Generated rule:")
c.UI.Warn("")
}

c.UI.Info(fmt.Sprintf("%%staff ALL=(root:wheel) NOPASSWD:NOSETENV: %s", strings.Join(cmd, " ")))
c.UI.Info(line)

return 0
}

func stdoutIsTerminal() bool {
fd := os.Stdout.Fd()
return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}

func (c *VmSudoersCommand) Synopsis() string {
return "Generates sudoers content for passwordless updating of /etc/hosts"
}
Expand Down
Loading