Skip to content

Commit 2cd1000

Browse files
committed
cli: better versioning
This now prints information about where the cli is installed from, and the commit hash (+ whether the tree is dirty)
1 parent 3e49e03 commit 2cd1000

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

cli/install.sh

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ function main {
3131
done
3232

3333
path=""
34+
mkdir -p "$HOME/.ntt-cli"
3435

3536
# check if there's a package.json in the parent directory, with "name": "@wormhole-foundation/ntt-cli"
3637
if [ -f "$(dirname $0)/package.json" ] && grep -q '"name": "@wormhole-foundation/ntt-cli"' "$(dirname $0)/package.json"; then
3738
path="$(dirname $0)/.."
39+
version=$(git rev-parse HEAD)
40+
dirty=$(git diff --quiet || echo "-dirty")
41+
echo "$version$dirty" > "$HOME/.ntt-cli/version"
3842
else
3943
# if branch is set, use it. otherwise use the latest tag of the form "vX.Y.Z+cli" or the 'cli' branch
4044
if [ -z "$branch" ]; then
@@ -44,7 +48,6 @@ function main {
4448
# clone to $HOME/.ntt-cli if it doesn't exist, otherwise update it
4549
echo "Cloning $REPO $branch"
4650

47-
mkdir -p "$HOME/.ntt-cli"
4851
path="$HOME/.ntt-cli/.checkout"
4952

5053
if [ ! -d "$path" ]; then
@@ -56,11 +59,24 @@ function main {
5659
git fetch origin
5760
# reset hard
5861
git reset --hard "origin/$branch"
62+
version=$(git rev-parse HEAD)
63+
dirty=$(git diff --quiet || echo "-dirty")
64+
echo "$version$dirty" > "$HOME/.ntt-cli/version"
5965
popd
6066
fi
61-
6267
fi
6368

69+
absolute_path="$(cd $path && pwd)"
70+
echo $absolute_path >> "$HOME/.ntt-cli/version"
71+
72+
# jq would be nicer but it's not portable
73+
# here we make the assumption that the file uses 2 spaces for indentation.
74+
# this is a bit fragile, but we don't want to catch further nested objects
75+
# (there might be a "version" in the scripts section, for example)
76+
version=$(cat "$path/cli/package.json" | grep '^ "version":' | cut -d '"' -f 4)
77+
echo "Installing ntt CLI version $version"
78+
echo "$version" >> "$HOME/.ntt-cli/version"
79+
6480
install_cli "$path"
6581
}
6682

cli/src/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ async function withCustomEvmDeployerScript<A>(pwd: string, then: () => Promise<A
199199
yargs(hideBin(process.argv))
200200
.wrap(Math.min(process.stdout.columns || 120, 160)) // Use terminal width, but no more than 160 characters
201201
.scriptName("ntt")
202+
.version((() => {
203+
const ver = nttVersion();
204+
if (!ver) {
205+
return "unknown";
206+
}
207+
const { version, commit, path } = ver;
208+
return `ntt v${version} (${commit} ${path})`;
209+
})())
202210
// config group of commands
203211
.command("config",
204212
"configuration commands",
@@ -1839,3 +1847,14 @@ function retryWithExponentialBackoff<T>(
18391847
};
18401848
return attempt(0);
18411849
}
1850+
1851+
function nttVersion(): { version: string, commit: string, path: string } | null {
1852+
const nttDir = `${process.env.HOME}/.ntt-cli`;
1853+
try {
1854+
const versionFile = fs.readFileSync(`${nttDir}/version`).toString().trim();
1855+
const [commit, installPath, version] = versionFile.split("\n");
1856+
return { version, commit, path: installPath };
1857+
} catch {
1858+
return null;
1859+
}
1860+
}

0 commit comments

Comments
 (0)