├── .gitignore ├── LICENSE.md ├── README.md └── nitrogen /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Luca Chiricozzi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | nitrogen 2 | ========= 3 | 4 | DEPRECATION NOTICE 5 | ------------------- 6 | 7 | nitrogen has fulfilled its purpose, as a mean to provide a temporary solution 8 | to the lack of ways to manage dependencies of programs written in Go, until the 9 | release of a tool on which the community would find agreement. 10 | 11 | With the release of Go 1.6, [vendoring](https://tip.golang.org/cmd/go/#hdr-Vendor_Directories) 12 | support is no longer considered experimental and it's the default way to handle 13 | dependencies, so the use of a dependency manager outside the Go standard tools is 14 | not necessary anymore. 15 | 16 | nitrogen will be deprecated with the release of Go 1.6. Support for bug fixes 17 | will be provided till the release of Go 1.7, then nitrogen will be no 18 | longer maintained and this repository will be kept only for reference. 19 | 20 | Thanks to everyone who took part, used or sent corrections to the project. 21 | 22 | _______________________________________________________________________________ 23 | 24 | nitrogen is a simple dependency manager for Go. 25 | 26 | It is a bash script that you should distribute with your Go package. Nitrogen 27 | leverages on the git, hg, and bzr versioning systems, so you don't need to distribute 28 | the full dependecy tree with your package, the standard gopath mechanism just 29 | works well. 30 | 31 | Set up dependencies 32 | -------------------- 33 | 34 | Dependencies are listed in a file called 'deps' that is located in the 35 | same directory of nitrogen (i.e. your package root directory). Dependencies 36 | are formatted as "package version", where "package" is the name of the Go 37 | package that you need to install (e.g "golang.org/x/crypto/blowfish") and 38 | "version" is every suitable option for `git checkout`, `hg update` or 39 | `bzr revert -r`. 40 | 41 | e.g. 42 | ``` 43 | code.google.com/p/go-uuid -c 35bc42037350 44 | golang.org/x/crypto 4ed45ec682102c643324fae5dff8dab085b6c300 45 | ``` 46 | 47 | ### Hey, I just want to freeze dependecies at the current version! 48 | 49 | That's why nitrogen has a '-f' (--freeze) option. 50 | 51 | ``` 52 | nitrogen -f 53 | ``` 54 | 55 | You should find your dependencies listed in the generated 'deps' file. 56 | 57 | Install dependencies 58 | --------------------- 59 | 60 | To install a package with the required dependencies, download it with 61 | 62 | ``` 63 | go get -d PACKAGE 64 | ``` 65 | 66 | Then, from the package root, start nitrogen with '-i' (--install). 67 | 68 | ``` 69 | nitrogen -i 70 | ``` 71 | 72 | Nitrogen downloads the required dependencies and installs the package with 73 | dependencies at the specified versions, then it restores the dependencies back 74 | to the default (master branch or latest version) versions. 75 | 76 | If you want to mantain the dependencies at the version required by your 77 | package, instead launch it with '-i --no-clean' 78 | 79 | ``` 80 | nitrogen -i --no-clean 81 | ``` 82 | 83 | After you have done with the work on your package, restore the depencencies 84 | at the default versions with the '-c' (--clean) option. 85 | 86 | ``` 87 | nitrogen -c 88 | ``` 89 | 90 | License 91 | -------- 92 | 93 | nitrogen is released under [the MIT License](http://opensource.org/licenses/MIT). 94 | -------------------------------------------------------------------------------- /nitrogen: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # nitrogen, a simple dependency manager for Go. 4 | # Copyright (c) 2015, Luca Chiricozzi 5 | # Released under the MIT License. 6 | # http://opensource.org/licenses/MIT 7 | 8 | set -eu 9 | 10 | DEP_FILE="$PWD/deps" 11 | GOPATH_SRC="$GOPATH/src/" 12 | PKG_DIR=$PWD 13 | 14 | # Display usage info. 15 | function usage { 16 | echo "usage: $0 [-f | -i [--no-clean] | -c] 17 | -f | --freeze Freeze the package dependencies. 18 | -i | --install Install the package dependencies, with --no-clean, leave 19 | the repositories at the version chosen in nitrogen. 20 | -c | --clean Clean the dependencies' repositories, restoring the 21 | default versions (git checkout master or hg update every 22 | repository)." 1>&2 23 | exit 2 24 | } 25 | 26 | # Remove $GOPATH_SRC from the given path 27 | function strip_gopath { 28 | echo "${1#$GOPATH_SRC}" 29 | } 30 | 31 | # Check if a repository is a git one 32 | function is_git_repo { 33 | git rev-parse 2>/dev/null 34 | } 35 | 36 | # Check if a repository is a bzr one 37 | function is_bzr_repo { 38 | bzr root >/dev/null 2>&1 39 | } 40 | 41 | # Freeze the package dependencies 42 | function freeze { 43 | # List dependencies of the package 44 | all_deps=$(go list -f '{{join .Deps " "}}' \ 45 | | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}') 46 | 47 | # Take only the ones that are external 48 | deps=$(echo "$all_deps" | grep -v $(strip_gopath $PWD)) 49 | 50 | # Create a new temp file. 51 | new=$(mktemp /tmp/nitrogen.XXXXXXXXXX) || exit 1 52 | 53 | # Remove the new dependency file if something goes wrong. 54 | trap 'rm -f $new; exit 1' SIGHUP SIGINT SIGTERM 55 | 56 | # Append dependencies 57 | for i in $deps; do 58 | repo_dir=$(echo "$GOPATH_SRC$i") 59 | cd $repo_dir 60 | if is_git_repo $repo_dir; then 61 | repo=$(strip_gopath $(git rev-parse --show-toplevel)) 62 | ver=$(<$GOPATH_SRC$repo/.git/refs/heads/master) 63 | elif is_bzr_repo $repo_dir; then 64 | repo=$(strip_gopath $(bzr root)) 65 | ver="revno:$(bzr revno)" 66 | else 67 | repo=$(strip_gopath $(hg root)) 68 | ver="-c $(hg id -i)" 69 | fi 70 | cd $PKG_DIR 71 | echo "$repo $ver" >>$new 72 | done 73 | 74 | # If something goes wrong, terminate the copy. 75 | trap '' SIGHUP SIGINT SIGTERM 76 | cat $new | uniq >$DEP_FILE 77 | rm -f $new 78 | fmap 'echo freeze: ' $DEP_FILE 79 | } 80 | 81 | # Select a version of the repository 82 | function select_version { 83 | trap 'clean; exit 1' ERR 84 | 85 | cd "$GOPATH_SRC$1" 86 | 87 | echo "select: $1 ${*:2}" 88 | 89 | if is_git_repo $1; then 90 | git checkout -q ${*:2} 91 | elif is_bzr_repo $1; then 92 | bzr revert -q -r ${*:2} 93 | else 94 | hg update -q ${*:2} 95 | fi 96 | } 97 | 98 | # Install packages with go get 99 | function go_get { 100 | go get -d $1 101 | } 102 | 103 | # Restore a repository to the default version 104 | function clean_version { 105 | cd "$GOPATH_SRC$1" 106 | 107 | echo "clean: $1 ${*:2}" 108 | 109 | if is_git_repo $1; then 110 | git checkout -q master 111 | elif is_bzr_repo $1; then 112 | bzr revert -q 113 | else 114 | hg -q update 115 | fi 116 | } 117 | 118 | # An iterative map implementation 119 | function fmap { 120 | while read; do 121 | $1 $REPLY 122 | done <$2 123 | } 124 | 125 | # Call 'clean_version' on the given repositories 126 | function clean { 127 | trap '' SIGHUP SIGINT SIGTERM 128 | 129 | fmap clean_version $DEP_FILE 130 | } 131 | 132 | # Set up dependencies and install the package 133 | function install { 134 | # If something goes wrong, bring repositories to the 135 | # standard version. 136 | trap clean SIGHUP SIGINT SIGTERM 137 | 138 | fmap go_get $DEP_FILE 139 | 140 | fmap select_version $DEP_FILE 141 | 142 | cd $PKG_DIR 143 | echo "install: $(strip_gopath $PKG_DIR)" 144 | go install 145 | 146 | if [[ "$1" == '--no-clean' ]]; then 147 | exit 0 148 | fi 149 | 150 | # All right, clean reposiories and return 151 | clean 152 | } 153 | 154 | case "${1-''}" in 155 | -f | --freeze) freeze 156 | ;; 157 | -i | --install) install ${2-''} 158 | ;; 159 | -c | --clean) clean 160 | ;; 161 | *) usage 162 | esac 163 | --------------------------------------------------------------------------------