Giter Club home page Giter Club logo

Comments (18)

silverwind avatar silverwind commented on June 11, 2024

Does adding '-i' to the arguments in the last lines do anything?

Looks like we've forgot to include -i in sp.sync 😟

from shell-path.

max-mapper avatar max-mapper commented on June 11, 2024

@silverwind nope, but sudo -Hiu max env can access my PATH:

screen shot 2015-10-27 at 10 41 29 am

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

sudo -Hiu max env

Interesting. Is that the minimum amount of arguments needed for it to work? Not sure we can or should use sudo just to get PATH.

from shell-path.

max-mapper avatar max-mapper commented on June 11, 2024

Im not sure familiar with that command, but it doesn't actually require sudo permissions to run

from shell-path.

max-mapper avatar max-mapper commented on June 11, 2024

from man sudo:


       -H          The -H (HOME) option sets the HOME environment variable to the homedir of the target user (root by default) as specified in passwd(5).  The
                   default handling of the HOME environment variable depends on sudoers(5) settings.  By default, sudo will set HOME if env_reset or
                   always_set_home are set, or if set_home is set and the -s option is specified on the command line.

       -i [command]
                   The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the target user as a login shell.  This means that
                   login-specific resource files such as .profile or .login will be read by the shell.  If a command is specified, it is passed to the shell for
                   execution.  Otherwise, an interactive shell is executed.  sudo attempts to change to that user's home directory before running the shell.  It
                   also initializes the environment, leaving DISPLAY and TERM unchanged, setting HOME, MAIL, SHELL, USER, LOGNAME, and PATH, as well as the
                   contents of /etc/environment on Linux and AIX systems.  All other environment variables are removed.

       -u user     The -u (user) option causes sudo to run the specified command as a user other than root.  To specify a uid instead of a user name, use #uid.
                   When running commands as a uid, many shells require that the '#' be escaped with a backslash ('\').  Note that if the targetpw Defaults option
                   is set (see sudoers(5)) it is not possible to run commands with a uid not listed in the password database.

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

If it doesn't require actual elevation, that's fine. I didn't even know sudo had such functionality. Some of that might be OS X specific. Thanks for finding this!

from shell-path.

max-mapper avatar max-mapper commented on June 11, 2024

oh yea echo $PATH works too. you'd just need to run whoami first to get the username to use (or use some other method)

screen shot 2015-10-27 at 10 50 13 am

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

Awesome! This looks to works well on OS X:

child_process.execFileSync('sudo', ['-Hiu', process.env.USER, 'echo $PATH']).toString()

Looks like sudo on Linux supports these flags too, thought my shell seems to have some issues, probably needs some escaping (I use zsh on both Linux and OS X):

> child_process.execFileSync('sudo', ['-Hiu', process.env.USER, 'echo $PATH']).toString()
zsh:1: no such file or directory: echo /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
Error: Command failed: sudo -Hiu silverwind echo $PATH

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

Might as well parse it out of env so it works on both Linux and OS X:

/PATH=(.+)\n/.exec(require('child_process').execFileSync('sudo', ['-Hiu', process.env.USER, 'env']).toString() || [])[1]

Edit: I see slightly different results using both methods on Ubuntu:

> /PATH=(.+)\n/.exec(require('child_process').execFileSync('sudo', ['-Hiu', process.env.USER, 'env']).toString() || [])[1].trim()
'/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin'
> child_process.execFileSync('/bin/sh', ['-i', '-c', 'echo $PATH']).toString().trim()
'/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/bin/X11:/usr/games'
> process.env.PATH
'/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/bin/X11:/usr/games'

I'll play in Electron to find out which method works best.

from shell-path.

davej avatar davej commented on June 11, 2024

Cross-referencing with sindresorhus/fix-path#4
Looks to be the same issue.

from shell-path.

maaarghk avatar maaarghk commented on June 11, 2024

I'm seeing this as well. I'm using zsh but it's in process.env.SHELL so I don't think that should be the issue. Attached is some debugging.

screen shot 2015-10-29 at 15 35 50

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

@maaarghk your issue with the sync method is fixed by 2e8c3f3, I'll push a release with that.

from shell-path.

maaarghk avatar maaarghk commented on June 11, 2024

I was literally just about to come here and say that I noticed that missing flag :) Thanks!

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

Looks @sindresorhus has to publish 1.0.3, Seems like I'm missing npm access on this repo.

from shell-path.

sindresorhus avatar sindresorhus commented on June 11, 2024

@silverwind Published, and you do now.

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

👍

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

Updated master to use both methods of obtaining the path and returning the longest path found out of all methods. Had a weird issue with execFileSync on Linux where

child_process.execFileSync('sudo', ['-Hiu', process.env.USER, 'echo $PATH'], {encoding: 'utf8'})

did fail with a zsh:1: no such file or directory. Switching to

child_process.execSync('sudo -Hiu' + process.env.USER + ' echo $PATH', {encoding: 'utf8'})

remedied that. May be a node bug.

from shell-path.

silverwind avatar silverwind commented on June 11, 2024

Released 1.0.4 with this.

from shell-path.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.