Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POSIX shell support #56

Open
prydie opened this issue Dec 6, 2014 · 5 comments
Open

POSIX shell support #56

prydie opened this issue Dec 6, 2014 · 5 comments

Comments

@prydie
Copy link
Contributor

prydie commented Dec 6, 2014

It would be good to not have bash as a dependency and to use #!/usr/bin/env sh instead for cross platform support.

I've made some headway with this which I'll paste below but it's beyond my shell scripting skills.

#!/usr/bin/env sh

set -eu

## Functions/
usage() {
cat << EOF
SYNOPSIS

    gpm leverages the power of the go get command and the underlying version
    control systems used by it to set your Go dependencies to desired versions,
    thus allowing easily reproducible builds in your Go projects.

    A Godeps file in the root of your Go application is expected containing
    the import paths of your packages and a specific tag or commit hash
    from its version control system, an example Godeps file looks like this:

    $ cat Godeps
    # This is a comment
    github.com/nu7hatch/gotrail         v0.0.2
    github.com/replicon/fast-archiver   v1.02   #This is another comment!
    github.com/nu7hatch/gotrail         2eb79d1f03ab24bacbc32b15b75769880629a865

    gpm has a companion tool, called [gvp](https://github.com/pote/gvp) which
    provides vendoring functionalities, it alters your GOPATH so every project
    has its own isolated dependency directory, its usage is recommended.

USAGE
      $ gpm             # Same as 'install'.
      $ gpm install     # Parses the Godeps file, installs dependencies and sets
                        # them to the appropriate version.
      $ gpm version     # Outputs version information
      $ gpm help        # Prints this message
EOF
}

is_in_use() {
  [ -e "$1/.git/index.lock" -o -e "$1/.hg/store/lock"  -o -e "$1/.bzr/checkout/lock" ]
}

# Iterates over Godep file dependencies and sets
# the specified version on each of them.
set_dependencies() {
  local deps=$(sed 's/#.*//;/^\s*$/d' < $1) || echo ""

  echo "$deps" | while read package version; do
    (
      echo ">> Getting package "$package""
      go get -u -d "$package"
    ) &
  done
  wait

  echo "$deps" | while read package version; do
    (
      local pkg_path=$(echo "$package" | awk -F/ '{print $1"/"$2"/"$3}')
      local install_path="${GOPATH%%:*}/src/${pkg_path%%/...}"
      echo ">> Setting $package to version $version"
      cd $install_path
      is_in_use $install_path && wait

      [ -d .bzr ] && bzr revert   -q -r   "$version"
      [ -d .git ] && git checkout -q      "$version"
      [ -d .hg  ] && hg update    -q      "$version"
      [ -d .svn ] && svn update   -r      "$version"
    ) &
  done
  wait
  echo ">> All Done"
}
## /Functions

## Command Line Parsing
case "${1:-"install"}" in
  "version")
    echo ">> gpm v1.3.1"
    ;;
  "install")
    deps_file="${2:-"Godeps"}"
    [ -r "$deps_file" ] || (echo ">> $deps_file file does not exist." && exit 1)
    (which go > /dev/null) ||
      ( echo ">> Go is currently not installed or in your PATH" && exit 1)
    set_dependencies $deps_file
    ;;
  "help")
    usage
    ;;
  *)
    ## Support for Plugins: if command is unknown search for a gpm-command executable.
    if command -v "gpm-$1" > /dev/null
    then
      plugin=$1 &&
      shift     &&
      gpm-$plugin $@ &&
      exit
    else
      echo ">> No command 'gpm $1'"
      usage && exit 1
    fi
    ;;
esac
@prydie
Copy link
Contributor Author

prydie commented Dec 13, 2014

In light of removing the dependency on bash in the ./configure script what are your thoughts on the possibility of achieving POSIX compatibility on the mainnnn gpm shell script?

@pote
Copy link
Owner

pote commented Dec 15, 2014

I like it, and in fact I much prefer the echo | while syntax you used to what's currently in gpm, I would like to have the changes in a PR though - and maybe even granular commits? - so I can test it thoroughly and merge it if no issues pop up.

Thanks again for the hard work! I think your version is much more readable than what we currently have in addition to being more portable, so that's another big plus :)

@pote
Copy link
Owner

pote commented Jan 5, 2015

Let's take it step by step: I'll initially make the loop change when I find the time to and then we'll go over all other things that need changing for POSIX support, I'm leaving this issue open to keep track of it

@prydie
Copy link
Contributor Author

prydie commented Jan 5, 2015

Sorry for the delay in getting back to you. My undergraduate thesis (the project during which I became frustrated with the POSIX incomparability of GPM) is due today. I'll have more time over the coming weeks to contribute.

@pote
Copy link
Owner

pote commented Jan 5, 2015

Don't worry about it! We're all volunteers in this and I'm also taking a long time to respond to things lately.

Good luck with your thesis! 💃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants