From 98b549dd93af72ce2c00eb385b4e80446cb73998 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 20 Jul 2023 14:25:18 -0500 Subject: [PATCH 01/69] add process for fuelup deprecation --- PROCESS.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 PROCESS.md diff --git a/PROCESS.md b/PROCESS.md new file mode 100644 index 000000000..2ba14c8f8 --- /dev/null +++ b/PROCESS.md @@ -0,0 +1,9 @@ +# Process for achieving completion of issue 457 + +1. In `fuelup-init.sh` use `check_cmd` for `nix`, if it is installed for the user there is no need to run the installation script. + - find a way to amend their `conf.nix` file to include the fuel binary cache from cachix and the necessary unstable features +2. If `nix` isn't present, run the full `fuel.nix` install script. +3. Possible conflict for the user may be that the binaries produced by the `fuel.nix` flake won't be stored at `/fuelup/bin/`. + - find out if this will be a problem and if a post install script will be sufficient for moving the installed binaries managed by fuelup +4. Map `fuel.nix` flake commands to the fuelup CLI commands already available. +5. Run tests to ensure all features work as intended. From 185335d8b41e6b9245fb2b7464d7beaa7d49b84d Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 20 Jul 2023 15:08:44 -0500 Subject: [PATCH 02/69] wip add check_cmd nix --- fuelup-init.sh | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 4630b09be..97d9f7154 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -17,6 +17,14 @@ main() { check_cargo_bin forc-lsp check_cargo_bin fuel-core + if check_cmd nix; then + # check if conf.nix/config.nix/configuration.nix exists, if not ask user permission to create one + # if it exists or user grants permission to create one, check if nix-command and flakes features enabled + # and fuel.nix cachix is linked, otherwise write to file + else + # run full fuel.nix install script + fi + get_architecture || return 1 local _arch="$RETVAL" assert_nz "$_arch" "arch" @@ -162,13 +170,7 @@ To use the toolchain, you will have to configure your PATH, which tells your mac If permitted, fuelup-init will configure your PATH for you by running the following: - echo "export PATH="\$HOME/.fuelup/bin:\$PATH"" >> $SHELL_PROFILE - -Would you like fuelup-init to modify your PATH variable for you? (N/y) -EOF -} - -add_path_message() { + echo "export PATH="\$HOME/.fuelup/bin:\$PATH"" >check_cmd cat 1>&2 < Date: Thu, 20 Jul 2023 15:11:19 -0500 Subject: [PATCH 03/69] fix accidental change --- fuelup-init.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 97d9f7154..640458f3a 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -170,7 +170,12 @@ To use the toolchain, you will have to configure your PATH, which tells your mac If permitted, fuelup-init will configure your PATH for you by running the following: - echo "export PATH="\$HOME/.fuelup/bin:\$PATH"" >check_cmd + echo "export PATH="\$HOME/.fuelup/bin:\$PATH"" >> $SHELL_PROFILE +Would you like fuelup-init to modify your PATH variable for you? (N/y) +EOF +} + +add_path_message() { cat 1>&2 < Date: Thu, 20 Jul 2023 15:13:08 -0500 Subject: [PATCH 04/69] fix spacing --- fuelup-init.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/fuelup-init.sh b/fuelup-init.sh index 640458f3a..102d01f03 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -171,6 +171,7 @@ To use the toolchain, you will have to configure your PATH, which tells your mac If permitted, fuelup-init will configure your PATH for you by running the following: echo "export PATH="\$HOME/.fuelup/bin:\$PATH"" >> $SHELL_PROFILE + Would you like fuelup-init to modify your PATH variable for you? (N/y) EOF } From fa4b52b8aeea2e6d5ed3b3baab40c02e75666f27 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 18:34:29 -0500 Subject: [PATCH 05/69] test CI & nix --- .github/workflows/ci.yml | 32 ++++++++++++++++---------------- fuelup-init.sh | 6 ++++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 68a416372..88ce1e6c2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,7 +49,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Attempt to install fuelup through fuelup-init.sh - run: ./fuelup-init.sh + run: ./fuelup-init.sh && nix --version cargo-clippy: needs: cancel-previous-runs @@ -59,7 +59,7 @@ jobs: - name: Install toolchain uses: dtolnay/rust-toolchain@stable - + - uses: Swatinem/rust-cache@v1 - name: Check Clippy Linter @@ -73,10 +73,10 @@ jobs: - name: Install toolchain uses: dtolnay/rust-toolchain@stable - + - name: Check Formatting run: cargo fmt --all -- --check - + cargo-test-workspace: needs: cancel-previous-runs runs-on: ubuntu-latest @@ -85,10 +85,10 @@ jobs: - name: Install toolchain uses: dtolnay/rust-toolchain@stable - + - name: Run tests run: cargo test --locked --workspace - + lint-toml-files: needs: cancel-previous-runs runs-on: ubuntu-latest @@ -99,7 +99,7 @@ jobs: - name: Install toolchain uses: dtolnay/rust-toolchain@stable - + - name: Install Cargo.toml linter uses: baptiste0928/cargo-install@v1 with: @@ -269,16 +269,16 @@ jobs: # Re-generate the channel TOML file - name: Rebuild channel with updated components run: | - mkdir -p ${{ env.LATEST_CHANNEL_DIR }} - CHANNEL_TOML="channel-fuel-latest.toml" + mkdir -p ${{ env.LATEST_CHANNEL_DIR }} + CHANNEL_TOML="channel-fuel-latest.toml" - FORC_VERSION=$(grep -A1 '\[pkg.forc\]' ./gh-pages/channel-fuel-latest.toml | cut -d "\"" -f2) - FUEL_CORE_VERSION=$(grep -A1 '\[pkg.fuel-core\]' ./gh-pages/channel-fuel-latest.toml | cut -d "\"" -f2) + FORC_VERSION=$(grep -A1 '\[pkg.forc\]' ./gh-pages/channel-fuel-latest.toml | cut -d "\"" -f2) + FUEL_CORE_VERSION=$(grep -A1 '\[pkg.fuel-core\]' ./gh-pages/channel-fuel-latest.toml | cut -d "\"" -f2) - PUBLISHED_DATE=$(date +'%Y-%m-%d') - build-channel $CHANNEL_TOML $PUBLISHED_DATE --github-run-id $GITHUB_RUN_ID forc=$FORC_VERSION fuel-core=$FUEL_CORE_VERSION + PUBLISHED_DATE=$(date +'%Y-%m-%d') + build-channel $CHANNEL_TOML $PUBLISHED_DATE --github-run-id $GITHUB_RUN_ID forc=$FORC_VERSION fuel-core=$FUEL_CORE_VERSION - cp $CHANNEL_TOML ${{ env.LATEST_CHANNEL_DIR }} + cp $CHANNEL_TOML ${{ env.LATEST_CHANNEL_DIR }} - name: Deploy latest channel if: ${{ env.LATEST_COMPATIBLE_FORC && env.LATEST_COMPATIBLE_FUEL_CORE }} @@ -288,8 +288,8 @@ jobs: publish_dir: ${{ env.LATEST_CHANNEL_DIR }} keep_files: true destination_dir: ./ - user_name: 'github-actions[bot]' - user_email: 'github-actions[bot]@users.noreply.github.com' + user_name: "github-actions[bot]" + user_email: "github-actions[bot]@users.noreply.github.com" post-release-checks: name: Do post-release checks diff --git a/fuelup-init.sh b/fuelup-init.sh index 102d01f03..bc44a9e63 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -10,6 +10,7 @@ main() { need_cmd mkdir need_cmd rm need_cmd rmdir + need_cmd grep check_cargo_bin forc check_cargo_bin forc-fmt @@ -21,8 +22,9 @@ main() { # check if conf.nix/config.nix/configuration.nix exists, if not ask user permission to create one # if it exists or user grants permission to create one, check if nix-command and flakes features enabled # and fuel.nix cachix is linked, otherwise write to file + true else - # run full fuel.nix install script + curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" fi get_architecture || return 1 @@ -164,7 +166,7 @@ main() { preinstall_confirmation() { cat 1>&2 < Date: Mon, 24 Jul 2023 18:36:28 -0500 Subject: [PATCH 06/69] fix legacy wrap --- fuelup-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index bc44a9e63..2ccf2b549 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -166,7 +166,7 @@ main() { preinstall_confirmation() { cat 1>&2 < Date: Mon, 24 Jul 2023 18:55:45 -0500 Subject: [PATCH 07/69] wrap curl command in function --- fuelup-init.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 2ccf2b549..5bc8dbe84 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -24,7 +24,7 @@ main() { # and fuel.nix cachix is linked, otherwise write to file true else - curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" + run_fuel_nix_install_script fi get_architecture || return 1 @@ -166,7 +166,7 @@ main() { preinstall_confirmation() { cat 1>&2 < Date: Mon, 24 Jul 2023 18:58:23 -0500 Subject: [PATCH 08/69] test check_cmd nix --- fuelup-init.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 5bc8dbe84..37e4a566b 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -18,12 +18,7 @@ main() { check_cargo_bin forc-lsp check_cargo_bin fuel-core - if check_cmd nix; then - # check if conf.nix/config.nix/configuration.nix exists, if not ask user permission to create one - # if it exists or user grants permission to create one, check if nix-command and flakes features enabled - # and fuel.nix cachix is linked, otherwise write to file - true - else + if ! check_cmd nix; then run_fuel_nix_install_script fi From 47fa38db8060b9370b09e69ccd6de53f34e66348 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 19:00:29 -0500 Subject: [PATCH 09/69] rm nix -v from ci check --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88ce1e6c2..17c134d7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,7 +49,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Attempt to install fuelup through fuelup-init.sh - run: ./fuelup-init.sh && nix --version + run: ./fuelup-init.sh cargo-clippy: needs: cancel-previous-runs From e6a521597181f26b26cab6403616409792114c84 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 19:11:42 -0500 Subject: [PATCH 10/69] wrap curl cmd, add nix -v back to ci --- .github/workflows/ci.yml | 2 +- fuelup-init.sh | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 17c134d7e..88ce1e6c2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,7 +49,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Attempt to install fuelup through fuelup-init.sh - run: ./fuelup-init.sh + run: ./fuelup-init.sh && nix --version cargo-clippy: needs: cancel-previous-runs diff --git a/fuelup-init.sh b/fuelup-init.sh index 37e4a566b..d7a56c4ba 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -10,7 +10,6 @@ main() { need_cmd mkdir need_cmd rm need_cmd rmdir - need_cmd grep check_cargo_bin forc check_cargo_bin forc-fmt @@ -18,7 +17,12 @@ main() { check_cargo_bin forc-lsp check_cargo_bin fuel-core - if ! check_cmd nix; then + if check_cmd nix; then + # check if conf.nix/config.nix/configuration.nix exists, if not ask user permission to create one + # if it exists or user grants permission to create one, check if nix-command and flakes features enabled + # and fuel.nix cachix is linked, otherwise write to file + true + else run_fuel_nix_install_script fi @@ -336,7 +340,7 @@ downloader() { } run_fuel_nix_install_script() { - curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" + $(curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=") } # Check if curl supports the --retry flag, then pass it to the curl invocation. From d159bab86399432db9542e27dc4b6254f7266d30 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 19:15:21 -0500 Subject: [PATCH 11/69] test fuel nix fn --- fuelup-init.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fuelup-init.sh b/fuelup-init.sh index d7a56c4ba..abc5d9d93 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -21,6 +21,7 @@ main() { # check if conf.nix/config.nix/configuration.nix exists, if not ask user permission to create one # if it exists or user grants permission to create one, check if nix-command and flakes features enabled # and fuel.nix cachix is linked, otherwise write to file + echo "found nix" true else run_fuel_nix_install_script @@ -340,7 +341,9 @@ downloader() { } run_fuel_nix_install_script() { + echo "trying to install nix" $(curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=") + need_cmd nix } # Check if curl supports the --retry flag, then pass it to the curl invocation. From ee77239794fe5c50bf767de52aa97bc53319160e Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 19:18:58 -0500 Subject: [PATCH 12/69] add --no-confirm to accept default nix installation --- fuelup-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index abc5d9d93..7cac04788 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -342,7 +342,7 @@ downloader() { run_fuel_nix_install_script() { echo "trying to install nix" - $(curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=") + curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 --no-confirm | sh -s -- install --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" need_cmd nix } From 8f18c7b0b6543b56b3e083c21648542ce205a669 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 19:20:32 -0500 Subject: [PATCH 13/69] add --no-confirm to accept default nix installation v2 --- fuelup-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 7cac04788..190b13901 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -342,7 +342,7 @@ downloader() { run_fuel_nix_install_script() { echo "trying to install nix" - curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 --no-confirm | sh -s -- install --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" + curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" need_cmd nix } From e75eca17ed7ade1d38bf2967671b053961930e12 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 19:22:35 -0500 Subject: [PATCH 14/69] add init nix after install --- fuelup-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 190b13901..666a24a72 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -342,7 +342,7 @@ downloader() { run_fuel_nix_install_script() { echo "trying to install nix" - curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" + curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8= && . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" need_cmd nix } From bc193397986b0567bb10b721161f5b066bb9983f Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 19:26:59 -0500 Subject: [PATCH 15/69] try running daemon on new line --- fuelup-init.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 666a24a72..7d4c42d1a 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -342,7 +342,8 @@ downloader() { run_fuel_nix_install_script() { echo "trying to install nix" - curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8= && . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" + curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" + $(. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh) need_cmd nix } From ce74266f337eab036388b9eceb88a6cdd13fd2ef Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 19:29:01 -0500 Subject: [PATCH 16/69] try direct use --- fuelup-init.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 7d4c42d1a..c2d3fc5ec 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -341,9 +341,9 @@ downloader() { } run_fuel_nix_install_script() { - echo "trying to install nix" + echo "installing nix via fuel.nix install script..." curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" - $(. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh) + . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh need_cmd nix } From e88169eee0fdd3d7f384e438495225c9f52c801a Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 20:17:50 -0500 Subject: [PATCH 17/69] try opening new shell --- fuelup-init.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index c2d3fc5ec..ee4583a5d 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -341,9 +341,12 @@ downloader() { } run_fuel_nix_install_script() { + # Get the name of the current shell + shell=$(ps -p $$ -o comm= | tr -d '-') echo "installing nix via fuel.nix install script..." curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" - . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh + # Start a new shell of the same type + $shell need_cmd nix } From fce2e226ce5290f6060c4b6552272443d3800654 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 20:19:04 -0500 Subject: [PATCH 18/69] rm need_cmd nix --- fuelup-init.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index ee4583a5d..68295fcdd 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -347,7 +347,6 @@ run_fuel_nix_install_script() { curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" # Start a new shell of the same type $shell - need_cmd nix } # Check if curl supports the --retry flag, then pass it to the curl invocation. From 0c96b8301281341072b4cf7dc78796c8bdb168b4 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 20:22:00 -0500 Subject: [PATCH 19/69] remove failing nix check for now --- .github/workflows/ci.yml | 2 +- fuelup-init.sh | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88ce1e6c2..17c134d7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,7 +49,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Attempt to install fuelup through fuelup-init.sh - run: ./fuelup-init.sh && nix --version + run: ./fuelup-init.sh cargo-clippy: needs: cancel-previous-runs diff --git a/fuelup-init.sh b/fuelup-init.sh index 68295fcdd..66f17e9e9 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -341,12 +341,8 @@ downloader() { } run_fuel_nix_install_script() { - # Get the name of the current shell - shell=$(ps -p $$ -o comm= | tr -d '-') - echo "installing nix via fuel.nix install script..." + echo "running fuel.nix install script..." curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix/tag/v0.9.0 | sh -s -- install --no-confirm --extra-conf "extra-substituters = https://fuellabs.cachix.org" --extra-conf "extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8=" - # Start a new shell of the same type - $shell } # Check if curl supports the --retry flag, then pass it to the curl invocation. From 575c1ce721d8586e7b949d52bd893392ab43b664 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 21:26:45 -0500 Subject: [PATCH 20/69] handle found nix --- fuelup-init.sh | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 66f17e9e9..82c7dfb05 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -2,6 +2,7 @@ set -e FUELUP_DIR=${FUELUP_DIR-"$HOME/.fuelup"} +readonly extraSubs="extra-substituters = https://fuellabs.cachix.org" main() { need_cmd git @@ -17,12 +18,9 @@ main() { check_cargo_bin forc-lsp check_cargo_bin fuel-core - if check_cmd nix; then - # check if conf.nix/config.nix/configuration.nix exists, if not ask user permission to create one - # if it exists or user grants permission to create one, check if nix-command and flakes features enabled - # and fuel.nix cachix is linked, otherwise write to file - echo "found nix" - true + local _found_nix=false + if ! check_cmd nix; then + _found_nix=true else run_fuel_nix_install_script fi @@ -160,6 +158,10 @@ main() { add_path_message fi + if ["$_found_nix" = true]; then + found_nix_message + fi + return "$_retval" } @@ -193,6 +195,20 @@ fish_add_path ~/.fuelup/bin EOF } +found_nix_message() { + cat 1>&2 < Date: Mon, 24 Jul 2023 21:28:09 -0500 Subject: [PATCH 21/69] fix spacing --- fuelup-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 82c7dfb05..a62e2a96e 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -158,7 +158,7 @@ main() { add_path_message fi - if ["$_found_nix" = true]; then + if [ "$_found_nix" = true ]; then found_nix_message fi From 3d4ef1e58008bc3c354efdf015e940c17dbd8af1 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 21:29:31 -0500 Subject: [PATCH 22/69] rm unused readonly --- fuelup-init.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index a62e2a96e..0c44ad17c 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -2,7 +2,6 @@ set -e FUELUP_DIR=${FUELUP_DIR-"$HOME/.fuelup"} -readonly extraSubs="extra-substituters = https://fuellabs.cachix.org" main() { need_cmd git From 3002d80977ac0d9dc7b001bf4fd0b08dead05a3a Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 21:31:20 -0500 Subject: [PATCH 23/69] fix check_cmd nix --- fuelup-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 0c44ad17c..74a37ee27 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -18,7 +18,7 @@ main() { check_cargo_bin fuel-core local _found_nix=false - if ! check_cmd nix; then + if check_cmd nix; then _found_nix=true else run_fuel_nix_install_script From dc0ccf6df0a6ec2fba796bf05cf4730ca892d796 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 21:34:40 -0500 Subject: [PATCH 24/69] add links for configuring nix --- fuelup-init.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 74a37ee27..0b029540a 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -203,8 +203,8 @@ Certain nix features must be enabled for fuelup to work. Please follow the instructions below to finish your setup, as fuelup will not amend nix config files for you at this time. -- Nix package manager: -- NixOS: +- Nix : https://github.com/FuelLabs/fuel.nix/blob/master/book/src/nix-setup.md#configuring-an-existing-nix-installation +- NixOS : https://github.com/FuelLabs/fuel.nix/blob/master/book/src/nix-setup.md#configuring-nixos EOF } From 9810ad5650c00d023ed5c357a34228965e937a26 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 21:37:51 -0500 Subject: [PATCH 25/69] make pre configured nix install a warning --- fuelup-init.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 0b029540a..005786e4d 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -158,6 +158,7 @@ main() { fi if [ "$_found_nix" = true ]; then + warn "Found pre-configured nix installation." found_nix_message fi @@ -197,8 +198,6 @@ EOF found_nix_message() { cat 1>&2 < Date: Mon, 24 Jul 2023 21:38:27 -0500 Subject: [PATCH 26/69] add important to msg --- fuelup-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 005786e4d..59723e3d3 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -198,7 +198,7 @@ EOF found_nix_message() { cat 1>&2 < Date: Mon, 24 Jul 2023 23:11:47 -0500 Subject: [PATCH 27/69] wip install toolchain cmd --- src/commands/toolchain.rs | 16 +++- src/ops/fuelup_toolchain/install.rs | 111 +++++++++++++++------------- 2 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/commands/toolchain.rs b/src/commands/toolchain.rs index c40bea2cc..758485d54 100644 --- a/src/commands/toolchain.rs +++ b/src/commands/toolchain.rs @@ -22,9 +22,23 @@ pub enum ToolchainCommand { #[derive(Debug, Parser)] pub struct InstallCommand { - /// Toolchain name [possible values: latest, beta-1, beta-2, beta-3, nightly] + /// Toolchain name [possible values: latest, beta-1, beta-2, beta-3, beta-4-rc, nightly] pub name: String, } +impl InstallCommand { + pub(crate) fn nix_suffix(&self) -> Result<&str> { + let suffix = match self.name.as_str() { + "latest" => "#fuel", + "nightly" => "#fuel-nightly", + "beta-1" | "beta1" => "#fuel-beta-1", + "beta-2" | "beta2" => "#fuel-beta-2", + "beta-3" | "beta3" => "#fuel-beta-3", + "beta-4-rc" | "beta-4rc" | "beta4rc" => "#fuel-beta-4-rc", + _ => bail!("available toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") + }; + Ok(suffix) + } +} #[derive(Debug, Parser)] pub struct NewCommand { diff --git a/src/ops/fuelup_toolchain/install.rs b/src/ops/fuelup_toolchain/install.rs index d86fd7e2d..99776faf0 100644 --- a/src/ops/fuelup_toolchain/install.rs +++ b/src/ops/fuelup_toolchain/install.rs @@ -1,63 +1,68 @@ -use crate::path::{settings_file, warn_existing_fuel_executables}; -use crate::settings::SettingsFile; -use crate::toolchain::{DistToolchainDescription, Toolchain}; -use crate::{channel::Channel, commands::toolchain::InstallCommand}; +use crate::commands::toolchain::InstallCommand; use anyhow::{bail, Result}; -use std::fmt::Write; -use std::str::FromStr; -use tracing::{error, info}; +use std::process::Command; + +const NIX_CMD: &str = "nix"; +const PROFILE_INSTALL: &[&str; 2] = &["profile", "install"]; +const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; pub fn install(command: InstallCommand) -> Result<()> { - let InstallCommand { name } = command; + if let Err(err) = Command::new(NIX_CMD) + .args(PROFILE_INSTALL) + .arg(format!("{FUEL_NIX_LINK}{}", command.nix_suffix()?)) + .output() + { + bail!("failed to install {} toolchain: {err}", command.name) + } - let description = DistToolchainDescription::from_str(&name)?; + // let description = DistToolchainDescription::from_str(&name)?; - let settings_file = settings_file(); - if !settings_file.exists() { - let settings = SettingsFile::new(settings_file); - settings.with_mut(|s| { - s.default_toolchain = Some(description.to_string()); - Ok(()) - })?; - } + // let settings_file = settings_file(); + // if !settings_file.exists() { + // let settings = SettingsFile::new(settings_file); + // settings.with_mut(|s| { + // s.default_toolchain = Some(description.to_string()); + // Ok(()) + // })?; + // } - let mut errored_bins = String::new(); - let mut installed_bins = String::new(); - - warn_existing_fuel_executables()?; - - let toolchain = Toolchain::from_path(&description.to_string()); - let cfgs = if let Ok(channel) = Channel::from_dist_channel(&description) { - channel.build_download_configs() - } else { - bail!("Could not build download configs from channel") - }; - - info!( - "Downloading: {}", - cfgs.iter() - .map(|c| c.name.clone() + " ") - .collect::() - ); - - for cfg in cfgs { - match toolchain.add_component(cfg) { - Ok(cfg) => writeln!(installed_bins, "- {} {}", cfg.name, cfg.version)?, - Err(e) => writeln!(errored_bins, "- {e}")?, - }; - } + // let mut errored_bins = String::new(); + // let mut installed_bins = String::new(); + + // warn_existing_fuel_executables()?; + + // let toolchain = Toolchain::from_path(&description.to_string()); + // let cfgs = if let Ok(channel) = Channel::from_dist_channel(&description) { + // channel.build_download_configs() + // } else { + // bail!("Could not build download configs from channel") + // }; + + // info!( + // "Downloading: {}", + // cfgs.iter() + // .map(|c| c.name.clone() + " ") + // .collect::() + // ); + + // for cfg in cfgs { + // match toolchain.add_component(cfg) { + // Ok(cfg) => writeln!(installed_bins, "- {} {}", cfg.name, cfg.version)?, + // Err(e) => writeln!(errored_bins, "- {e}")?, + // }; + // } - if errored_bins.is_empty() { - info!("\nInstalled:\n{}", installed_bins); - info!("\nThe Fuel toolchain is installed and up to date"); - } else if installed_bins.is_empty() { - error!("\nfuelup failed to install:\n{}", errored_bins) - } else { - info!( - "\nThe Fuel toolchain is partially installed.\nfuelup failed to install: {}", - errored_bins - ); - }; + // if errored_bins.is_empty() { + // info!("\nInstalled:\n{}", installed_bins); + // info!("\nThe Fuel toolchain is installed and up to date"); + // } else if installed_bins.is_empty() { + // error!("\nfuelup failed to install:\n{}", errored_bins) + // } else { + // info!( + // "\nThe Fuel toolchain is partially installed.\nfuelup failed to install: {}", + // errored_bins + // ); + // }; Ok(()) } From e181e6c7fa2f736bbc1e3dcb4fbaa7cfe69188bc Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 23:38:06 -0500 Subject: [PATCH 28/69] wip list cmd --- PROCESS.md | 9 --- src/ops/fuelup_component/list.rs | 115 +++++++++++++++------------- src/ops/fuelup_toolchain/install.rs | 4 +- 3 files changed, 63 insertions(+), 65 deletions(-) delete mode 100644 PROCESS.md diff --git a/PROCESS.md b/PROCESS.md deleted file mode 100644 index 2ba14c8f8..000000000 --- a/PROCESS.md +++ /dev/null @@ -1,9 +0,0 @@ -# Process for achieving completion of issue 457 - -1. In `fuelup-init.sh` use `check_cmd` for `nix`, if it is installed for the user there is no need to run the installation script. - - find a way to amend their `conf.nix` file to include the fuel binary cache from cachix and the necessary unstable features -2. If `nix` isn't present, run the full `fuel.nix` install script. -3. Possible conflict for the user may be that the binaries produced by the `fuel.nix` flake won't be stored at `/fuelup/bin/`. - - find out if this will be a problem and if a post install script will be sufficient for moving the installed binaries managed by fuelup -4. Map `fuel.nix` flake commands to the fuelup CLI commands already available. -5. Run tests to ensure all features work as intended. diff --git a/src/ops/fuelup_component/list.rs b/src/ops/fuelup_component/list.rs index fab8dad26..db225ddde 100644 --- a/src/ops/fuelup_component/list.rs +++ b/src/ops/fuelup_component/list.rs @@ -1,10 +1,12 @@ +use crate::ops::fuelup_toolchain::install::NIX_CMD; use crate::{ commands::component::ListCommand, download::get_latest_version, fmt::bold, toolchain::Toolchain, }; -use anyhow::Result; +use anyhow::{bail, Result}; use component::Components; use semver::Version; use std::io::Write; +use std::process::Command; use tracing::info; fn format_installed_component_info( @@ -34,68 +36,73 @@ fn format_forc_default_plugins(plugin_executables: Vec) -> String { ) } +const PROFILE_LIST: &[&str; 2] = &["profile", "list"]; pub fn list(_command: ListCommand) -> Result<()> { - let toolchain = Toolchain::from_settings()?; + if let Err(err) = Command::new(NIX_CMD).args(PROFILE_LIST).output() { + bail!("failed to show installed binaries for profile: {err}") + } - // use write! instead of writeln! here to prevent this from printing first. - bold(|s| write!(s, "{}", toolchain.name)); + // let toolchain = Toolchain::from_settings()?; - let mut installed_components_summary = String::from("\nInstalled:\n"); - let mut available_components_summary = String::from("Installable:\n"); + // // use write! instead of writeln! here to prevent this from printing first. + // bold(|s| write!(s, "{}", toolchain.name)); - let components = Components::collect_publishables()?; - for component in components { - let latest_version = get_latest_version(&component.name).map_or_else( - |_| String::from("failed to get latest version"), - |v| v.to_string(), - ); - if toolchain.has_component(&component.name) { - let exec_path = toolchain.bin_path.join(&component.name); + // let mut installed_components_summary = String::from("\nInstalled:\n"); + // let mut available_components_summary = String::from("Installable:\n"); - let current_version = if let Ok(o) = std::process::Command::new(exec_path) - .arg("--version") - .output() - { - let output = String::from_utf8_lossy(&o.stdout).into_owned(); - output.split_whitespace().last().map_or_else( - || None, - |v| Version::parse(v).map_or_else(|_| None, |v| Some(v.to_string())), - ) - } else { - None - }; + // let components = Components::collect_publishables()?; + // for component in components { + // let latest_version = get_latest_version(&component.name).map_or_else( + // |_| String::from("failed to get latest version"), + // |v| v.to_string(), + // ); + // if toolchain.has_component(&component.name) { + // let exec_path = toolchain.bin_path.join(&component.name); - let version_info = match Some(&latest_version) == current_version.as_ref() { - true => "up-to-date".to_string(), - false => format!("latest: {}", &latest_version), - }; + // let current_version = if let Ok(o) = std::process::Command::new(exec_path) + // .arg("--version") + // .output() + // { + // let output = String::from_utf8_lossy(&o.stdout).into_owned(); + // output.split_whitespace().last().map_or_else( + // || None, + // |v| Version::parse(v).map_or_else(|_| None, |v| Some(v.to_string())), + // ) + // } else { + // None + // }; - installed_components_summary.push_str(&format_installed_component_info( - &component.name, - current_version, - &version_info, - )); + // let version_info = match Some(&latest_version) == current_version.as_ref() { + // true => "up-to-date".to_string(), + // false => format!("latest: {}", &latest_version), + // }; - if component.name == component::FORC { - installed_components_summary - .push_str(&format_forc_default_plugins(component.executables)) - } - } else { - available_components_summary.push_str(&format_installable_component_info( - &component.name, - &latest_version, - )); + // installed_components_summary.push_str(&format_installed_component_info( + // &component.name, + // current_version, + // &version_info, + // )); - if component.name == component::FORC { - available_components_summary - .push_str(&format_forc_default_plugins(component.executables)) - } - } - } - info!( - "{}\n{}", - installed_components_summary, available_components_summary - ); + // if component.name == component::FORC { + // installed_components_summary + // .push_str(&format_forc_default_plugins(component.executables)) + // } + // } else { + // available_components_summary.push_str(&format_installable_component_info( + // &component.name, + // &latest_version, + // )); + + // if component.name == component::FORC { + // available_components_summary + // .push_str(&format_forc_default_plugins(component.executables)) + // } + // } + // } + // info!( + // "{}\n{}", + // installed_components_summary, available_components_summary + // ); Ok(()) } diff --git a/src/ops/fuelup_toolchain/install.rs b/src/ops/fuelup_toolchain/install.rs index 99776faf0..0543645e5 100644 --- a/src/ops/fuelup_toolchain/install.rs +++ b/src/ops/fuelup_toolchain/install.rs @@ -2,9 +2,9 @@ use crate::commands::toolchain::InstallCommand; use anyhow::{bail, Result}; use std::process::Command; -const NIX_CMD: &str = "nix"; +pub(crate) const NIX_CMD: &str = "nix"; const PROFILE_INSTALL: &[&str; 2] = &["profile", "install"]; -const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; +pub(crate) const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; pub fn install(command: InstallCommand) -> Result<()> { if let Err(err) = Command::new(NIX_CMD) From f2b32c630a930c4a1cb84ce1831c915dd7b53a28 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 24 Jul 2023 23:49:02 -0500 Subject: [PATCH 29/69] mv msg --- fuelup-init.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 59723e3d3..809b22115 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -158,7 +158,6 @@ main() { fi if [ "$_found_nix" = true ]; then - warn "Found pre-configured nix installation." found_nix_message fi @@ -198,6 +197,8 @@ EOF found_nix_message() { cat 1>&2 < Date: Tue, 25 Jul 2023 01:08:15 -0500 Subject: [PATCH 30/69] fix fuelup component list --- src/ops/fuelup_component/list.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ops/fuelup_component/list.rs b/src/ops/fuelup_component/list.rs index db225ddde..fe01ac836 100644 --- a/src/ops/fuelup_component/list.rs +++ b/src/ops/fuelup_component/list.rs @@ -38,8 +38,9 @@ fn format_forc_default_plugins(plugin_executables: Vec) -> String { const PROFILE_LIST: &[&str; 2] = &["profile", "list"]; pub fn list(_command: ListCommand) -> Result<()> { - if let Err(err) = Command::new(NIX_CMD).args(PROFILE_LIST).output() { - bail!("failed to show installed binaries for profile: {err}") + match Command::new(NIX_CMD).args(PROFILE_LIST).output() { + Ok(output) => info!("{:#?}", std::str::from_utf8(&output.stdout)?), + Err(err) => bail!("failed to show installed binaries for profile: {err}"), } // let toolchain = Toolchain::from_settings()?; From 02141edfca20d2ab08c328d987699c8dc9c37c6f Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 26 Jul 2023 16:59:50 -0500 Subject: [PATCH 31/69] update install info and list formatting --- src/ops/fuelup_component/list.rs | 81 ++++++----------------------- src/ops/fuelup_toolchain/install.rs | 5 +- 2 files changed, 19 insertions(+), 67 deletions(-) diff --git a/src/ops/fuelup_component/list.rs b/src/ops/fuelup_component/list.rs index fe01ac836..fa4736a1c 100644 --- a/src/ops/fuelup_component/list.rs +++ b/src/ops/fuelup_component/list.rs @@ -9,7 +9,7 @@ use std::io::Write; use std::process::Command; use tracing::info; -fn format_installed_component_info( +fn _format_installed_component_info( name: &str, version: Option, version_info: &str, @@ -21,11 +21,11 @@ fn format_installed_component_info( } } -fn format_installable_component_info(name: &str, latest_version: &str) -> String { +fn _format_installable_component_info(name: &str, latest_version: &str) -> String { format!(" {name} (latest: {latest_version})\n") } -fn format_forc_default_plugins(plugin_executables: Vec) -> String { +fn _format_forc_default_plugins(plugin_executables: Vec) -> String { format!( " - {}\n", plugin_executables @@ -36,74 +36,23 @@ fn format_forc_default_plugins(plugin_executables: Vec) -> String { ) } +// todo: format output for listed components, currently shows toolchains as well which +// needs to be changed const PROFILE_LIST: &[&str; 2] = &["profile", "list"]; pub fn list(_command: ListCommand) -> Result<()> { match Command::new(NIX_CMD).args(PROFILE_LIST).output() { - Ok(output) => info!("{:#?}", std::str::from_utf8(&output.stdout)?), + Ok(output) => { + let output_strs = std::str::from_utf8(&output.stdout)? + .split(' ') + .filter(|s| s.contains("/nix/store")) + .map(|s| s.trim()) + .collect::>(); + for s in output_strs { + info!("{:#?}", s) + } + } Err(err) => bail!("failed to show installed binaries for profile: {err}"), } - // let toolchain = Toolchain::from_settings()?; - - // // use write! instead of writeln! here to prevent this from printing first. - // bold(|s| write!(s, "{}", toolchain.name)); - - // let mut installed_components_summary = String::from("\nInstalled:\n"); - // let mut available_components_summary = String::from("Installable:\n"); - - // let components = Components::collect_publishables()?; - // for component in components { - // let latest_version = get_latest_version(&component.name).map_or_else( - // |_| String::from("failed to get latest version"), - // |v| v.to_string(), - // ); - // if toolchain.has_component(&component.name) { - // let exec_path = toolchain.bin_path.join(&component.name); - - // let current_version = if let Ok(o) = std::process::Command::new(exec_path) - // .arg("--version") - // .output() - // { - // let output = String::from_utf8_lossy(&o.stdout).into_owned(); - // output.split_whitespace().last().map_or_else( - // || None, - // |v| Version::parse(v).map_or_else(|_| None, |v| Some(v.to_string())), - // ) - // } else { - // None - // }; - - // let version_info = match Some(&latest_version) == current_version.as_ref() { - // true => "up-to-date".to_string(), - // false => format!("latest: {}", &latest_version), - // }; - - // installed_components_summary.push_str(&format_installed_component_info( - // &component.name, - // current_version, - // &version_info, - // )); - - // if component.name == component::FORC { - // installed_components_summary - // .push_str(&format_forc_default_plugins(component.executables)) - // } - // } else { - // available_components_summary.push_str(&format_installable_component_info( - // &component.name, - // &latest_version, - // )); - - // if component.name == component::FORC { - // available_components_summary - // .push_str(&format_forc_default_plugins(component.executables)) - // } - // } - // } - // info!( - // "{}\n{}", - // installed_components_summary, available_components_summary - // ); - Ok(()) } diff --git a/src/ops/fuelup_toolchain/install.rs b/src/ops/fuelup_toolchain/install.rs index 0543645e5..c571be588 100644 --- a/src/ops/fuelup_toolchain/install.rs +++ b/src/ops/fuelup_toolchain/install.rs @@ -1,15 +1,18 @@ use crate::commands::toolchain::InstallCommand; use anyhow::{bail, Result}; use std::process::Command; +use tracing::info; pub(crate) const NIX_CMD: &str = "nix"; const PROFILE_INSTALL: &[&str; 2] = &["profile", "install"]; pub(crate) const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; pub fn install(command: InstallCommand) -> Result<()> { + let nix_suffix = command.nix_suffix()?; + info!("downloading and installing {command:?} toolchain, if this is the first time it may take a while..."); if let Err(err) = Command::new(NIX_CMD) .args(PROFILE_INSTALL) - .arg(format!("{FUEL_NIX_LINK}{}", command.nix_suffix()?)) + .arg(format!("{FUEL_NIX_LINK}{}", nix_suffix)) .output() { bail!("failed to install {} toolchain: {err}", command.name) From 9daf7b8eda38fa7a397d92ae7b50f62487705e04 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Sun, 30 Jul 2023 19:08:27 -0500 Subject: [PATCH 32/69] fix installing toolchain msg --- src/ops/fuelup_toolchain/install.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ops/fuelup_toolchain/install.rs b/src/ops/fuelup_toolchain/install.rs index c571be588..d697a94e0 100644 --- a/src/ops/fuelup_toolchain/install.rs +++ b/src/ops/fuelup_toolchain/install.rs @@ -9,7 +9,10 @@ pub(crate) const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; pub fn install(command: InstallCommand) -> Result<()> { let nix_suffix = command.nix_suffix()?; - info!("downloading and installing {command:?} toolchain, if this is the first time it may take a while..."); + info!( + "downloading and installing {} toolchain, if this is the first time it may take a while...", + command.name + ); if let Err(err) = Command::new(NIX_CMD) .args(PROFILE_INSTALL) .arg(format!("{FUEL_NIX_LINK}{}", nix_suffix)) From 903604faf82f6836b8685b2223e66a80c5e30646 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 31 Jul 2023 01:10:38 -0500 Subject: [PATCH 33/69] add temp shell command --- src/commands/component.rs | 2 +- src/commands/mod.rs | 1 + src/commands/shell.rs | 36 +++++++++++++++++++ src/commands/toolchain.rs | 37 ++++++++++++------- src/fuelup_cli.rs | 13 ++++--- src/ops/fuelup_component/list.rs | 2 -- src/ops/fuelup_toolchain/install.rs | 55 ++--------------------------- 7 files changed, 71 insertions(+), 75 deletions(-) create mode 100644 src/commands/shell.rs diff --git a/src/commands/component.rs b/src/commands/component.rs index 2bf0518a2..35d09651e 100644 --- a/src/commands/component.rs +++ b/src/commands/component.rs @@ -26,7 +26,7 @@ pub struct RemoveCommand { } #[derive(Debug, Parser)] -pub struct ListCommand {} +pub struct ListCommand; pub fn exec(command: ComponentCommand) -> Result<()> { match command { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 643631f39..fe4edad04 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3,6 +3,7 @@ pub mod completions; pub mod component; pub mod default; pub mod fuelup; +pub mod shell; pub mod show; pub mod toolchain; pub mod update; diff --git a/src/commands/shell.rs b/src/commands/shell.rs new file mode 100644 index 000000000..8aef8b0f6 --- /dev/null +++ b/src/commands/shell.rs @@ -0,0 +1,36 @@ +use anyhow::{bail, Result}; +use clap::Parser; +use std::process::Command; +use tracing::info; +use std::io::{self, Write}; + +use crate::ops::fuelup_toolchain::install::{FUEL_NIX_LINK, NIX_CMD}; + +use super::toolchain::{NeedsNix, NixName}; + + +#[derive(Debug, Parser)] +pub struct ShellCommand { + /// Open a new bash development shell with specified toolchain. + pub toolchain: String, +} +impl NeedsNix for ShellCommand { + fn get_toolchain(&self) -> &str { + self.toolchain.as_str() + } +} +impl NixName for ShellCommand {} +const SHELL: &str = "shell"; +pub fn exec(command: ShellCommand) -> Result<()> { + info!( + "starting new bash shell with {} toolchain available on $PATH...", + command.toolchain + ); + let shell_cmd = format!("{NIX_CMD} {SHELL} {}", command.toolchain_link()?); + if let Ok(mut child) = Command::new(NIX_CMD).arg(SHELL).arg(command.toolchain_link()?) + .spawn() { + child.wait()?; + } + + Ok(()) +} diff --git a/src/commands/toolchain.rs b/src/commands/toolchain.rs index 758485d54..bb1a41a3f 100644 --- a/src/commands/toolchain.rs +++ b/src/commands/toolchain.rs @@ -2,6 +2,7 @@ use anyhow::{bail, Result}; use clap::Parser; use crate::ops::fuelup_toolchain::install::install; +use crate::ops::fuelup_toolchain::install::FUEL_NIX_LINK; use crate::ops::fuelup_toolchain::list_revisions::list_revisions; use crate::ops::fuelup_toolchain::new::new; use crate::ops::fuelup_toolchain::uninstall::uninstall; @@ -19,26 +20,38 @@ pub enum ToolchainCommand { /// Fetch the list of published `latest` toolchains, starting from the most recent ListRevisions(ListRevisionsCommand), } +pub trait NeedsNix { + fn get_toolchain(&self) -> &str; +} +pub trait NixName: NeedsNix { + fn nix_suffix(&self) -> Result<&str> { + let suffix = match self.get_toolchain() { + "latest" => "fuel", + "nightly" => "fuel-nightly", + "beta-1" | "beta1" => "fuel-beta-1", + "beta-2" | "beta2" => "fuel-beta-2", + "beta-3" | "beta3" => "fuel-beta-3", + "beta-4-rc" | "beta-4rc" | "beta4rc" => "fuel-beta-4-rc", + _ => bail!("available toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") + }; + Ok(suffix) + } + fn toolchain_link(&self) -> Result { + Ok(format!("{FUEL_NIX_LINK}#{}", self.nix_suffix()?)) + } +} #[derive(Debug, Parser)] pub struct InstallCommand { /// Toolchain name [possible values: latest, beta-1, beta-2, beta-3, beta-4-rc, nightly] pub name: String, } -impl InstallCommand { - pub(crate) fn nix_suffix(&self) -> Result<&str> { - let suffix = match self.name.as_str() { - "latest" => "#fuel", - "nightly" => "#fuel-nightly", - "beta-1" | "beta1" => "#fuel-beta-1", - "beta-2" | "beta2" => "#fuel-beta-2", - "beta-3" | "beta3" => "#fuel-beta-3", - "beta-4-rc" | "beta-4rc" | "beta4rc" => "#fuel-beta-4-rc", - _ => bail!("available toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") - }; - Ok(suffix) +impl NeedsNix for InstallCommand { + fn get_toolchain(&self) -> &str { + self.name.as_str() } } +impl NixName for InstallCommand {} #[derive(Debug, Parser)] pub struct NewCommand { diff --git a/src/fuelup_cli.rs b/src/fuelup_cli.rs index efa512698..b84bc20be 100644 --- a/src/fuelup_cli.rs +++ b/src/fuelup_cli.rs @@ -1,6 +1,7 @@ use anyhow::Result; use clap::Parser; +use crate::commands::shell::{self, ShellCommand}; use crate::commands::show::ShowCommand; use crate::commands::{check, completions, component, default, fuelup, show, toolchain, update}; @@ -23,13 +24,10 @@ pub struct Cli { enum Commands { /// Check for updates to Fuel toolchains and fuelup Check(CheckCommand), - /// Generate shell completions - Completions(CompletionsCommand), - /// Add or remove components from the currently active toolchain - #[clap(subcommand)] - Component(ComponentCommand), /// Set default toolchain Default_(DefaultCommand), + /// Opens a new bash shell instace with specified toolchain available on `$PATH` + Shell(ShellCommand), /// Manage your fuelup installation. #[clap(name = "self", subcommand)] Fuelup(FuelupCommand), @@ -47,8 +45,9 @@ pub fn fuelup_cli() -> Result<()> { match cli.command { Commands::Check(command) => check::exec(command), - Commands::Completions(command) => completions::exec(command), - Commands::Component(command) => component::exec(command), + Commands::Shell(command) => shell::exec(command), + // Commands::Completions(command) => completions::exec(command), + // Commands::Component(command) => component::exec(command), Commands::Default_(command) => default::exec(command), Commands::Fuelup(command) => match command { FuelupCommand::Update => fuelup::exec(), diff --git a/src/ops/fuelup_component/list.rs b/src/ops/fuelup_component/list.rs index fa4736a1c..8ec630437 100644 --- a/src/ops/fuelup_component/list.rs +++ b/src/ops/fuelup_component/list.rs @@ -36,8 +36,6 @@ fn _format_forc_default_plugins(plugin_executables: Vec) -> String { ) } -// todo: format output for listed components, currently shows toolchains as well which -// needs to be changed const PROFILE_LIST: &[&str; 2] = &["profile", "list"]; pub fn list(_command: ListCommand) -> Result<()> { match Command::new(NIX_CMD).args(PROFILE_LIST).output() { diff --git a/src/ops/fuelup_toolchain/install.rs b/src/ops/fuelup_toolchain/install.rs index d697a94e0..b42ce5bdb 100644 --- a/src/ops/fuelup_toolchain/install.rs +++ b/src/ops/fuelup_toolchain/install.rs @@ -1,4 +1,4 @@ -use crate::commands::toolchain::InstallCommand; +use crate::commands::toolchain::{InstallCommand, NixName}; use anyhow::{bail, Result}; use std::process::Command; use tracing::info; @@ -8,67 +8,16 @@ const PROFILE_INSTALL: &[&str; 2] = &["profile", "install"]; pub(crate) const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; pub fn install(command: InstallCommand) -> Result<()> { - let nix_suffix = command.nix_suffix()?; info!( "downloading and installing {} toolchain, if this is the first time it may take a while...", command.name ); if let Err(err) = Command::new(NIX_CMD) .args(PROFILE_INSTALL) - .arg(format!("{FUEL_NIX_LINK}{}", nix_suffix)) + .arg(command.toolchain_link()?) .output() { bail!("failed to install {} toolchain: {err}", command.name) } - - // let description = DistToolchainDescription::from_str(&name)?; - - // let settings_file = settings_file(); - // if !settings_file.exists() { - // let settings = SettingsFile::new(settings_file); - // settings.with_mut(|s| { - // s.default_toolchain = Some(description.to_string()); - // Ok(()) - // })?; - // } - - // let mut errored_bins = String::new(); - // let mut installed_bins = String::new(); - - // warn_existing_fuel_executables()?; - - // let toolchain = Toolchain::from_path(&description.to_string()); - // let cfgs = if let Ok(channel) = Channel::from_dist_channel(&description) { - // channel.build_download_configs() - // } else { - // bail!("Could not build download configs from channel") - // }; - - // info!( - // "Downloading: {}", - // cfgs.iter() - // .map(|c| c.name.clone() + " ") - // .collect::() - // ); - - // for cfg in cfgs { - // match toolchain.add_component(cfg) { - // Ok(cfg) => writeln!(installed_bins, "- {} {}", cfg.name, cfg.version)?, - // Err(e) => writeln!(errored_bins, "- {e}")?, - // }; - // } - - // if errored_bins.is_empty() { - // info!("\nInstalled:\n{}", installed_bins); - // info!("\nThe Fuel toolchain is installed and up to date"); - // } else if installed_bins.is_empty() { - // error!("\nfuelup failed to install:\n{}", errored_bins) - // } else { - // info!( - // "\nThe Fuel toolchain is partially installed.\nfuelup failed to install: {}", - // errored_bins - // ); - // }; - Ok(()) } From 6063fa327fe11dfc1f1c9e9d0596223600fb5a59 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Sun, 6 Aug 2023 21:53:52 -0500 Subject: [PATCH 34/69] add priority to install method --- src/commands/shell.rs | 19 +++++++------- src/commands/toolchain.rs | 1 + src/ops/fuelup_toolchain/install.rs | 40 ++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/commands/shell.rs b/src/commands/shell.rs index 8aef8b0f6..ca7105331 100644 --- a/src/commands/shell.rs +++ b/src/commands/shell.rs @@ -1,14 +1,13 @@ use anyhow::{bail, Result}; use clap::Parser; +use std::io::{self, Write}; use std::process::Command; use tracing::info; -use std::io::{self, Write}; -use crate::ops::fuelup_toolchain::install::{FUEL_NIX_LINK, NIX_CMD}; +use crate::ops::fuelup_toolchain::install::NIX_CMD; use super::toolchain::{NeedsNix, NixName}; - #[derive(Debug, Parser)] pub struct ShellCommand { /// Open a new bash development shell with specified toolchain. @@ -26,11 +25,13 @@ pub fn exec(command: ShellCommand) -> Result<()> { "starting new bash shell with {} toolchain available on $PATH...", command.toolchain ); - let shell_cmd = format!("{NIX_CMD} {SHELL} {}", command.toolchain_link()?); - if let Ok(mut child) = Command::new(NIX_CMD).arg(SHELL).arg(command.toolchain_link()?) - .spawn() { - child.wait()?; - } - + if let Ok(mut child) = Command::new(NIX_CMD) + .arg(SHELL) + .arg(command.toolchain_link()?) + .spawn() + { + child.wait()?; + } + Ok(()) } diff --git a/src/commands/toolchain.rs b/src/commands/toolchain.rs index bb1a41a3f..60bcf1623 100644 --- a/src/commands/toolchain.rs +++ b/src/commands/toolchain.rs @@ -45,6 +45,7 @@ pub trait NixName: NeedsNix { pub struct InstallCommand { /// Toolchain name [possible values: latest, beta-1, beta-2, beta-3, beta-4-rc, nightly] pub name: String, + pub priority: Option, } impl NeedsNix for InstallCommand { fn get_toolchain(&self) -> &str { diff --git a/src/ops/fuelup_toolchain/install.rs b/src/ops/fuelup_toolchain/install.rs index b42ce5bdb..ff074a438 100644 --- a/src/ops/fuelup_toolchain/install.rs +++ b/src/ops/fuelup_toolchain/install.rs @@ -1,10 +1,11 @@ use crate::commands::toolchain::{InstallCommand, NixName}; -use anyhow::{bail, Result}; +use anyhow::{anyhow, Result}; use std::process::Command; use tracing::info; pub(crate) const NIX_CMD: &str = "nix"; const PROFILE_INSTALL: &[&str; 2] = &["profile", "install"]; +const PRIORITY: &str = "--priority"; pub(crate) const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; pub fn install(command: InstallCommand) -> Result<()> { @@ -12,12 +13,37 @@ pub fn install(command: InstallCommand) -> Result<()> { "downloading and installing {} toolchain, if this is the first time it may take a while...", command.name ); - if let Err(err) = Command::new(NIX_CMD) - .args(PROFILE_INSTALL) - .arg(command.toolchain_link()?) - .output() - { - bail!("failed to install {} toolchain: {err}", command.name) + let (output, priority) = if let Some(ref priority) = command.priority { + let output = Command::new(NIX_CMD) + .args(PROFILE_INSTALL) + .arg(command.toolchain_link()?) + .arg(PRIORITY) + .arg(priority) + .output() + .map_err(|err| anyhow!("failed to install {} toolchain: {err}", command.name))?; + (output, Some(priority)) + } else { + let output = Command::new(NIX_CMD) + .args(PROFILE_INSTALL) + .arg(command.toolchain_link()?) + .output() + .map_err(|err| anyhow!("failed to install {} toolchain: {err}", command.name))?; + (output, None) + }; + if !output.stdout.is_empty() { + info!("{}", String::from_utf8_lossy(&output.stdout)); } + if !output.stderr.is_empty() { + info!("{}", String::from_utf8_lossy(&output.stderr)); + } + + if priority.is_some() { + info!( + "successfully added {} with priority {}", + command.name, + priority.unwrap() + ); + } + Ok(()) } From f3b973cd16d47c4af606df98c0e2ce79c0404d1d Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 7 Aug 2023 04:05:17 -0500 Subject: [PATCH 35/69] refactor nix commands --- src/commands/mod.rs | 2 +- src/commands/nix/install.rs | 90 +++++++++++++++ src/commands/nix/link_generator.rs | 169 ++++++++++++++++++++++++++++ src/commands/nix/list.rs | 19 ++++ src/commands/nix/mod.rs | 66 +++++++++++ src/commands/nix/remove.rs | 21 ++++ src/commands/nix/shell.rs | 21 ++++ src/commands/nix/upgrade.rs | 30 +++++ src/commands/shell.rs | 37 ------ src/commands/toolchain.rs | 28 ----- src/fuelup_cli.rs | 24 ++-- src/ops/fuelup_component/list.rs | 79 ++++++++++--- src/ops/fuelup_toolchain/install.rs | 90 ++++++++------- 13 files changed, 546 insertions(+), 130 deletions(-) create mode 100644 src/commands/nix/install.rs create mode 100644 src/commands/nix/link_generator.rs create mode 100644 src/commands/nix/list.rs create mode 100644 src/commands/nix/mod.rs create mode 100644 src/commands/nix/remove.rs create mode 100644 src/commands/nix/shell.rs create mode 100644 src/commands/nix/upgrade.rs delete mode 100644 src/commands/shell.rs diff --git a/src/commands/mod.rs b/src/commands/mod.rs index fe4edad04..77359b444 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3,7 +3,7 @@ pub mod completions; pub mod component; pub mod default; pub mod fuelup; -pub mod shell; +pub mod nix; pub mod show; pub mod toolchain; pub mod update; diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs new file mode 100644 index 000000000..c86757e03 --- /dev/null +++ b/src/commands/nix/install.rs @@ -0,0 +1,90 @@ +use crate::commands::nix::{ + link_generator::{NeedsNix, NixName}, + nix_info, NIX_CMD, PRIORITY_FLAG, PROFILE_INSTALL_ARGS, +}; +use anyhow::{anyhow, bail, Result}; +use clap::Parser; +use std::process::Command; +use tracing::info; + +#[derive(Debug, Parser)] +pub struct NixInstallCommand { + /// Toolchain or component + pub name: String, + pub priority: Option, +} + +pub fn nix_install(command: NixInstallCommand) -> Result<()> { + let (output, priority) = if command.is_toolchain() { + info!( + "downloading and installing fuel {} toolchain, this may take a while...", + command.name + ); + let (output, priority) = if let Some(ref priority) = command.priority { + let output = Command::new(NIX_CMD) + .args(PROFILE_INSTALL_ARGS) + .arg(command.toolchain_link()?) + .arg(PRIORITY_FLAG) + .arg(priority) + .output() + .map_err(|err| { + anyhow!("failed to install fuel {} toolchain: {err}", command.name) + })?; + (output, Some(priority)) + } else { + let output = Command::new(NIX_CMD) + .args(PROFILE_INSTALL_ARGS) + .arg(command.toolchain_link()?) + .output() + .map_err(|err| { + anyhow!("failed to install fuel {} toolchain: {err}", command.name) + })?; + (output, None) + }; + (output, priority) + } else if command.is_component() { + info!( + "downloading and installing {} component, this may take a while...", + command.name + ); + let (output, priority) = if let Some(ref priority) = command.priority { + let output = Command::new(NIX_CMD) + .args(PROFILE_INSTALL_ARGS) + .arg(command.component_link()?) + .arg(PRIORITY_FLAG) + .arg(priority) + .output() + .map_err(|err| anyhow!("failed to install {} component: {err}", command.name))?; + (output, Some(priority)) + } else { + let output = Command::new(NIX_CMD) + .args(PROFILE_INSTALL_ARGS) + .arg(command.component_link()?) + .output() + .map_err(|err| anyhow!("failed to install {} component: {err}", command.name))?; + (output, None) + }; + (output, priority) + } else { + bail!( + "available distrubuted components:\n -fuel-core\n -fuel-core-client\n -fuel-indexer\n -forc\n -forc-client\n -forc-doc\n -forc-explore\n -forc-fmt\n -forc-index\n -forc-lsp\n -forc-tx\n -forc-wallet\n -sway-vim\n +available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc + +please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" + ) + }; + + nix_info!(output); + + if priority.is_some() && output.stderr.is_empty() { + info!( + "successfully added {} with priority {}", + command.name, + priority.unwrap() + ); + } else { + info!("successfully added {}", command.name,); + } + + Ok(()) +} diff --git a/src/commands/nix/link_generator.rs b/src/commands/nix/link_generator.rs new file mode 100644 index 000000000..baf105602 --- /dev/null +++ b/src/commands/nix/link_generator.rs @@ -0,0 +1,169 @@ +use super::{install::NixInstallCommand, FUEL_NIX_LINK}; +use anyhow::{bail, Result}; + +/// Handles getting toolchain or component information to translate +/// into fuel.nix flake links and info presented to the user. +pub(crate) trait NeedsNix { + /// the name of a toolchain or component + fn name(&self) -> &str; + fn get_toolchain(&self) -> Result { + FuelToolchain::from_str(self.name()) + } + fn get_component(&self) -> Result<(FuelComponent, FuelToolchain)> { + FuelComponent::from_str(self.name()) + } + fn is_toolchain(&self) -> bool { + FuelToolchain::from_str(self.name()).is_ok() + } + fn is_component(&self) -> bool { + FuelComponent::from_str(self.name()).is_ok() + } +} +/// Create toolchain and component links for the fuel.nix flake. +pub(crate) trait NixName: NeedsNix { + fn nix_toolchain_suffix(&self) -> Result<&str> { + Ok(match self.get_toolchain()? { + FuelToolchain::Latest => "fuel", + FuelToolchain::Nightly => "fuel-nightly", + FuelToolchain::Beta1 => "fuel-beta-1", + FuelToolchain::Beta2 => "fuel-beta-2", + FuelToolchain::Beta3 => "fuel-beta-3", + FuelToolchain::Beta4rc => "fuel-beta-4-rc", + }) + } + fn nix_component_suffix(&self) -> Result<(&str, &str)> { + let (comp, tool) = self.get_component()?; + let comp = match comp { + FuelComponent::FuelCore => "fuel-core", + FuelComponent::FuelCoreClient => "fuel-core-client", + FuelComponent::FuelIndexer => "fuel-indexer", + FuelComponent::Forc => "forc", + FuelComponent::ForcClient => "forc-client", + FuelComponent::ForcDoc => "forc-doc", + FuelComponent::ForcExplore => "forc-explore", + FuelComponent::ForcFmt => "forc-fmt", + FuelComponent::ForcIndex => "forc-index", + FuelComponent::ForcLsp => "forc-lsp", + FuelComponent::ForcTx => "forc-tx", + FuelComponent::ForcWallet => "forc-wallet", + FuelComponent::SwayVim => "sway-vim", + }; + let tool = match tool { + FuelToolchain::Latest => "", + FuelToolchain::Nightly => "-nightly", + FuelToolchain::Beta1 => "-beta-1", + FuelToolchain::Beta2 => "-beta-2", + FuelToolchain::Beta3 => "-beta-3", + FuelToolchain::Beta4rc => "-beta-4-rc", + }; + Ok((comp, tool)) + } + fn toolchain_link(&self) -> Result { + Ok(format!("{FUEL_NIX_LINK}#{}", self.nix_toolchain_suffix()?)) + } + fn component_link(&self) -> Result { + let (comp, tool) = self.nix_component_suffix()?; + Ok(format!("{FUEL_NIX_LINK}#{}{}", comp, tool)) + } +} + +impl NeedsNix for NixInstallCommand { + fn name(&self) -> &str { + self.name.as_str() + } +} +// impl NeedsNix for NixShellCommand { +// fn name(&self) -> &str { +// self.name.as_str() +// } +// } +impl NixName for NixInstallCommand {} +// impl NixName for NixShellCommand {} + +#[derive(Eq, PartialEq, Debug)] +pub(crate) enum FuelToolchain { + Latest, + Nightly, + Beta1, + Beta2, + Beta3, + Beta4rc, +} + +impl FuelToolchain { + fn from_str(s: &str) -> Result { + Ok(match s.to_lowercase().as_str() { + "latest" => Self::Latest, + "nightly" => Self::Nightly, + "beta-1" | "beta1" => Self::Beta1, + "beta-2" | "beta2" => Self::Beta2, + "beta-3" | "beta3" => Self::Beta3, + "beta-4-rc" | "beta-4rc" | "beta4rc" => Self::Beta4rc, + _ => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") + }) + } + fn is_latest(&self) -> bool { + *self == FuelToolchain::Latest + } +} +pub(crate) enum FuelComponent { + FuelCore, + FuelCoreClient, + FuelIndexer, + Forc, + ForcClient, + ForcDoc, + ForcExplore, + ForcFmt, + ForcIndex, + ForcLsp, + ForcTx, + ForcWallet, + SwayVim, +} +impl FuelComponent { + fn from_str(s: &str) -> Result<(Self, FuelToolchain)> { + let (comp, tool) = split_at_toolchain(s.to_lowercase())?; + // remove the excess '-' between the comp and toolchain vers + let comp = if !tool.is_latest() { + let mut comp = comp.chars(); + comp.next_back(); + comp.collect::() + } else { + comp + }; + Ok((match comp.as_str() { + "fuel-core" => Self::FuelCore, + "fuel-core-client" => Self::FuelCoreClient, + "fuel-indexer" => Self::FuelIndexer, + "forc" => Self::Forc, + "forc-client" => Self::ForcClient, + "forc-doc" => Self::ForcDoc, + "forc-explore" => Self::ForcExplore, + "forc-fmt" => Self::ForcFmt, + "forc-index" => Self::ForcIndex, + "forc-lsp" => Self::ForcLsp, + "forc-tx" => Self::ForcTx, + "forc-wallet" => Self::ForcWallet, + "sway-vim" => Self::SwayVim, + _ => bail!( + "available distrubuted components:\n -fuel-core\n -fuel-core-client\n -fuel-indexer\n -forc\n -forc-client\n -forc-doc\n -forc-explore\n -forc-fmt\n -forc-index\n -forc-lsp\n -forc-tx\n -forc-wallet\n -sway-vim\n +available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc + +please form a valid component, like so: fuel-core-beta-3" + ) + }, tool)) + } +} +fn split_at_toolchain(s: String) -> Result<(String, FuelToolchain)> { + let (comp, tool) = if let Some(index) = s.find("beta") { + let (comp, tool) = s.split_at(index); + (comp.into(), FuelToolchain::from_str(tool)?) + } else if let Some(index) = s.find("nightly") { + let (comp, tool) = s.split_at(index); + (comp.into(), FuelToolchain::from_str(tool)?) + } else { + (s, FuelToolchain::Latest) + }; + Ok((comp, tool)) +} diff --git a/src/commands/nix/list.rs b/src/commands/nix/list.rs new file mode 100644 index 000000000..7bd2a7977 --- /dev/null +++ b/src/commands/nix/list.rs @@ -0,0 +1,19 @@ +use super::{NIX_CMD, PROFILE_LIST_ARGS}; +use crate::commands::nix::nix_info; +use anyhow::{bail, Result}; +use clap::Parser; +use std::process::Command; +use tracing::info; + +#[derive(Debug, Parser)] +pub struct NixListCommand; + +pub fn nix_list(_command: NixListCommand) -> Result<()> { + match Command::new(NIX_CMD).args(PROFILE_LIST_ARGS).output() { + Ok(output) => { + nix_info!(output); + Ok(()) + } + Err(err) => bail!("failed to show installed binaries for profile: {err}"), + } +} diff --git a/src/commands/nix/mod.rs b/src/commands/nix/mod.rs new file mode 100644 index 000000000..b5a0fa831 --- /dev/null +++ b/src/commands/nix/mod.rs @@ -0,0 +1,66 @@ +use self::{ + install::{nix_install, NixInstallCommand}, + list::{nix_list, NixListCommand}, + remove::{nix_remove, NixRemoveCommand}, + upgrade::{nix_upgrade, NixUpgradeCommand}, +}; +use anyhow::Result; +use clap::Parser; + +mod install; +mod link_generator; +mod list; +mod remove; +mod shell; +mod upgrade; + +macro_rules! nix_info { + ($output:expr) => { + if !$output.stdout.is_empty() { + info!("{}", String::from_utf8_lossy(&$output.stdout)); + } + if !$output.stderr.is_empty() { + info!("{}", String::from_utf8_lossy(&$output.stderr)); + } + }; +} +pub(crate) use nix_info; + +pub(crate) const NIX_CMD: &str = "nix"; +// pub(crate) const SHELL_ARG: &str = "shell"; +pub(crate) const PROFILE_ARG: &str = "profile"; +pub(crate) const UNLOCKED_FLAKE_REF: &str = ".*"; +pub(crate) const PROFILE_INSTALL_ARGS: &[&str; 2] = &[PROFILE_ARG, "install"]; +pub(crate) const PROFILE_LIST_ARGS: &[&str; 2] = &[PROFILE_ARG, "list"]; +pub(crate) const PROFILE_REMOVE_ARGS: &[&str; 2] = &[PROFILE_ARG, "remove"]; +pub(crate) const PROFILE_UPGRADE_ARGS: &[&str; 2] = &[PROFILE_ARG, "upgrade"]; +pub(crate) const PRIORITY_FLAG: &str = "--priority"; +pub(crate) const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; + +#[derive(Debug, Parser)] +pub enum NixCommand { + /// Install a distributable toolchain or component. + Install(NixInstallCommand), + /// Uninstall a toolchain or component by providing its index, + /// unlocked attribute path or nix store path. + Remove(NixRemoveCommand), + /// Upgrade installed packages by index or unlocked attribute path + /// with the latest version of the fuel.nix flake. Upgrades all + /// installed packages if no index or path is provided. + Upgrade(NixUpgradeCommand), + // /// Opens a new bash shell instace with specified toolchain or component available on `$PATH`. + // Shell(NixShellCommand), + /// Lists the installed packages by index, unlocked attribute path, + /// locked attribute path and nix store path, respectively. + List(NixListCommand), +} + +pub fn exec(command: NixCommand) -> Result<()> { + match command { + NixCommand::Install(command) => nix_install(command), + NixCommand::Remove(command) => nix_remove(command), + NixCommand::Upgrade(command) => nix_upgrade(command), + // NixCommand::Shell(command) => nix_shell(command), + NixCommand::List(_command) => nix_list(_command), + } +} diff --git a/src/commands/nix/remove.rs b/src/commands/nix/remove.rs new file mode 100644 index 000000000..805142e8a --- /dev/null +++ b/src/commands/nix/remove.rs @@ -0,0 +1,21 @@ +use crate::commands::nix::{nix_info, NIX_CMD, PROFILE_REMOVE_ARGS}; +use anyhow::Result; +use clap::Parser; +use std::process::Command; +use tracing::info; + +#[derive(Debug, Parser)] +pub struct NixRemoveCommand { + pub pkg: String, +} + +pub fn nix_remove(command: NixRemoveCommand) -> Result<()> { + let output = Command::new(NIX_CMD) + .args(PROFILE_REMOVE_ARGS) + .arg(command.pkg) + .output()?; + + nix_info!(output); + + Ok(()) +} diff --git a/src/commands/nix/shell.rs b/src/commands/nix/shell.rs new file mode 100644 index 000000000..228b8be06 --- /dev/null +++ b/src/commands/nix/shell.rs @@ -0,0 +1,21 @@ +// #[derive(Debug, Parser)] +// pub struct NixShellCommand { +// /// Open a new bash development shell with specified toolchain or component. +// pub name: String, +// } + +// pub fn nix_shell(command: NixShellCommand) -> Result<()> { +// info!( +// "starting new bash shell with fuel {} toolchain available on $PATH...", +// command.name +// ); +// if let Ok(mut child) = Command::new(NIX_CMD) +// .arg(SHELL_ARG) +// .arg(command.toolchain_link()?) +// .spawn() +// { +// child.wait()?; +// } + +// Ok(()) +// } diff --git a/src/commands/nix/upgrade.rs b/src/commands/nix/upgrade.rs new file mode 100644 index 000000000..f78abff6b --- /dev/null +++ b/src/commands/nix/upgrade.rs @@ -0,0 +1,30 @@ +use crate::commands::nix::{nix_info, NIX_CMD, PROFILE_UPGRADE_ARGS, UNLOCKED_FLAKE_REF}; +use anyhow::Result; +use clap::Parser; +use std::process::Command; +use tracing::info; + +#[derive(Debug, Parser)] +pub struct NixUpgradeCommand { + pub pkg: Option, +} + +pub fn nix_upgrade(command: NixUpgradeCommand) -> Result<()> { + let output = if let Some(pkg) = command.pkg { + info!("upgrading installed fuel.nix package, this may take a while..."); + Command::new(NIX_CMD) + .args(PROFILE_UPGRADE_ARGS) + .arg(pkg) + .output()? + } else { + info!("upgrading installed fuel.nix packages, this may take a while..."); + Command::new(NIX_CMD) + .args(PROFILE_UPGRADE_ARGS) + .arg(UNLOCKED_FLAKE_REF) + .output()? + }; + + nix_info!(output); + + Ok(()) +} diff --git a/src/commands/shell.rs b/src/commands/shell.rs deleted file mode 100644 index ca7105331..000000000 --- a/src/commands/shell.rs +++ /dev/null @@ -1,37 +0,0 @@ -use anyhow::{bail, Result}; -use clap::Parser; -use std::io::{self, Write}; -use std::process::Command; -use tracing::info; - -use crate::ops::fuelup_toolchain::install::NIX_CMD; - -use super::toolchain::{NeedsNix, NixName}; - -#[derive(Debug, Parser)] -pub struct ShellCommand { - /// Open a new bash development shell with specified toolchain. - pub toolchain: String, -} -impl NeedsNix for ShellCommand { - fn get_toolchain(&self) -> &str { - self.toolchain.as_str() - } -} -impl NixName for ShellCommand {} -const SHELL: &str = "shell"; -pub fn exec(command: ShellCommand) -> Result<()> { - info!( - "starting new bash shell with {} toolchain available on $PATH...", - command.toolchain - ); - if let Ok(mut child) = Command::new(NIX_CMD) - .arg(SHELL) - .arg(command.toolchain_link()?) - .spawn() - { - child.wait()?; - } - - Ok(()) -} diff --git a/src/commands/toolchain.rs b/src/commands/toolchain.rs index 60bcf1623..34d578d34 100644 --- a/src/commands/toolchain.rs +++ b/src/commands/toolchain.rs @@ -2,7 +2,6 @@ use anyhow::{bail, Result}; use clap::Parser; use crate::ops::fuelup_toolchain::install::install; -use crate::ops::fuelup_toolchain::install::FUEL_NIX_LINK; use crate::ops::fuelup_toolchain::list_revisions::list_revisions; use crate::ops::fuelup_toolchain::new::new; use crate::ops::fuelup_toolchain::uninstall::uninstall; @@ -20,39 +19,12 @@ pub enum ToolchainCommand { /// Fetch the list of published `latest` toolchains, starting from the most recent ListRevisions(ListRevisionsCommand), } -pub trait NeedsNix { - fn get_toolchain(&self) -> &str; -} -pub trait NixName: NeedsNix { - fn nix_suffix(&self) -> Result<&str> { - let suffix = match self.get_toolchain() { - "latest" => "fuel", - "nightly" => "fuel-nightly", - "beta-1" | "beta1" => "fuel-beta-1", - "beta-2" | "beta2" => "fuel-beta-2", - "beta-3" | "beta3" => "fuel-beta-3", - "beta-4-rc" | "beta-4rc" | "beta4rc" => "fuel-beta-4-rc", - _ => bail!("available toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") - }; - Ok(suffix) - } - fn toolchain_link(&self) -> Result { - Ok(format!("{FUEL_NIX_LINK}#{}", self.nix_suffix()?)) - } -} #[derive(Debug, Parser)] pub struct InstallCommand { /// Toolchain name [possible values: latest, beta-1, beta-2, beta-3, beta-4-rc, nightly] pub name: String, - pub priority: Option, -} -impl NeedsNix for InstallCommand { - fn get_toolchain(&self) -> &str { - self.name.as_str() - } } -impl NixName for InstallCommand {} #[derive(Debug, Parser)] pub struct NewCommand { diff --git a/src/fuelup_cli.rs b/src/fuelup_cli.rs index b84bc20be..b6f39f5c2 100644 --- a/src/fuelup_cli.rs +++ b/src/fuelup_cli.rs @@ -1,17 +1,16 @@ use anyhow::Result; use clap::Parser; -use crate::commands::shell::{self, ShellCommand}; -use crate::commands::show::ShowCommand; -use crate::commands::{check, completions, component, default, fuelup, show, toolchain, update}; - use crate::commands::check::CheckCommand; use crate::commands::completions::CompletionsCommand; use crate::commands::component::ComponentCommand; use crate::commands::default::DefaultCommand; use crate::commands::fuelup::FuelupCommand; +use crate::commands::nix::{self, NixCommand}; +use crate::commands::show::ShowCommand; use crate::commands::toolchain::ToolchainCommand; use crate::commands::update::UpdateCommand; +use crate::commands::{check, completions, component, default, fuelup, show, toolchain, update}; #[derive(Debug, Parser)] #[clap(name = "fuelup", about = "Fuel Toolchain Manager", version)] @@ -24,10 +23,13 @@ pub struct Cli { enum Commands { /// Check for updates to Fuel toolchains and fuelup Check(CheckCommand), + /// Generate shell completions + Completions(CompletionsCommand), + /// Add or remove components from the currently active toolchain + #[clap(subcommand)] + Component(ComponentCommand), /// Set default toolchain Default_(DefaultCommand), - /// Opens a new bash shell instace with specified toolchain available on `$PATH` - Shell(ShellCommand), /// Manage your fuelup installation. #[clap(name = "self", subcommand)] Fuelup(FuelupCommand), @@ -38,6 +40,10 @@ enum Commands { Show(ShowCommand), /// Updates the distributable toolchains, if already installed Update(UpdateCommand), + /// Use the fuel.nix flake to manage toolchains and components. + /// Refer to the fuel.nix book for more info: https://github.com/FuelLabs/fuel.nix/blob/master/book/src/packages.md#packages + #[clap(subcommand)] + Nix(NixCommand), } pub fn fuelup_cli() -> Result<()> { @@ -45,9 +51,8 @@ pub fn fuelup_cli() -> Result<()> { match cli.command { Commands::Check(command) => check::exec(command), - Commands::Shell(command) => shell::exec(command), - // Commands::Completions(command) => completions::exec(command), - // Commands::Component(command) => component::exec(command), + Commands::Completions(command) => completions::exec(command), + Commands::Component(command) => component::exec(command), Commands::Default_(command) => default::exec(command), Commands::Fuelup(command) => match command { FuelupCommand::Update => fuelup::exec(), @@ -55,5 +60,6 @@ pub fn fuelup_cli() -> Result<()> { Commands::Show(_command) => show::exec(), Commands::Toolchain(command) => toolchain::exec(command), Commands::Update(_command) => update::exec(), + Commands::Nix(command) => nix::exec(command), } } diff --git a/src/ops/fuelup_component/list.rs b/src/ops/fuelup_component/list.rs index 8ec630437..fab8dad26 100644 --- a/src/ops/fuelup_component/list.rs +++ b/src/ops/fuelup_component/list.rs @@ -1,15 +1,13 @@ -use crate::ops::fuelup_toolchain::install::NIX_CMD; use crate::{ commands::component::ListCommand, download::get_latest_version, fmt::bold, toolchain::Toolchain, }; -use anyhow::{bail, Result}; +use anyhow::Result; use component::Components; use semver::Version; use std::io::Write; -use std::process::Command; use tracing::info; -fn _format_installed_component_info( +fn format_installed_component_info( name: &str, version: Option, version_info: &str, @@ -21,11 +19,11 @@ fn _format_installed_component_info( } } -fn _format_installable_component_info(name: &str, latest_version: &str) -> String { +fn format_installable_component_info(name: &str, latest_version: &str) -> String { format!(" {name} (latest: {latest_version})\n") } -fn _format_forc_default_plugins(plugin_executables: Vec) -> String { +fn format_forc_default_plugins(plugin_executables: Vec) -> String { format!( " - {}\n", plugin_executables @@ -36,21 +34,68 @@ fn _format_forc_default_plugins(plugin_executables: Vec) -> String { ) } -const PROFILE_LIST: &[&str; 2] = &["profile", "list"]; pub fn list(_command: ListCommand) -> Result<()> { - match Command::new(NIX_CMD).args(PROFILE_LIST).output() { - Ok(output) => { - let output_strs = std::str::from_utf8(&output.stdout)? - .split(' ') - .filter(|s| s.contains("/nix/store")) - .map(|s| s.trim()) - .collect::>(); - for s in output_strs { - info!("{:#?}", s) + let toolchain = Toolchain::from_settings()?; + + // use write! instead of writeln! here to prevent this from printing first. + bold(|s| write!(s, "{}", toolchain.name)); + + let mut installed_components_summary = String::from("\nInstalled:\n"); + let mut available_components_summary = String::from("Installable:\n"); + + let components = Components::collect_publishables()?; + for component in components { + let latest_version = get_latest_version(&component.name).map_or_else( + |_| String::from("failed to get latest version"), + |v| v.to_string(), + ); + if toolchain.has_component(&component.name) { + let exec_path = toolchain.bin_path.join(&component.name); + + let current_version = if let Ok(o) = std::process::Command::new(exec_path) + .arg("--version") + .output() + { + let output = String::from_utf8_lossy(&o.stdout).into_owned(); + output.split_whitespace().last().map_or_else( + || None, + |v| Version::parse(v).map_or_else(|_| None, |v| Some(v.to_string())), + ) + } else { + None + }; + + let version_info = match Some(&latest_version) == current_version.as_ref() { + true => "up-to-date".to_string(), + false => format!("latest: {}", &latest_version), + }; + + installed_components_summary.push_str(&format_installed_component_info( + &component.name, + current_version, + &version_info, + )); + + if component.name == component::FORC { + installed_components_summary + .push_str(&format_forc_default_plugins(component.executables)) + } + } else { + available_components_summary.push_str(&format_installable_component_info( + &component.name, + &latest_version, + )); + + if component.name == component::FORC { + available_components_summary + .push_str(&format_forc_default_plugins(component.executables)) } } - Err(err) => bail!("failed to show installed binaries for profile: {err}"), } + info!( + "{}\n{}", + installed_components_summary, available_components_summary + ); Ok(()) } diff --git a/src/ops/fuelup_toolchain/install.rs b/src/ops/fuelup_toolchain/install.rs index ff074a438..d86fd7e2d 100644 --- a/src/ops/fuelup_toolchain/install.rs +++ b/src/ops/fuelup_toolchain/install.rs @@ -1,49 +1,63 @@ -use crate::commands::toolchain::{InstallCommand, NixName}; -use anyhow::{anyhow, Result}; -use std::process::Command; -use tracing::info; - -pub(crate) const NIX_CMD: &str = "nix"; -const PROFILE_INSTALL: &[&str; 2] = &["profile", "install"]; -const PRIORITY: &str = "--priority"; -pub(crate) const FUEL_NIX_LINK: &str = "github:fuellabs/fuel.nix"; +use crate::path::{settings_file, warn_existing_fuel_executables}; +use crate::settings::SettingsFile; +use crate::toolchain::{DistToolchainDescription, Toolchain}; +use crate::{channel::Channel, commands::toolchain::InstallCommand}; +use anyhow::{bail, Result}; +use std::fmt::Write; +use std::str::FromStr; +use tracing::{error, info}; pub fn install(command: InstallCommand) -> Result<()> { - info!( - "downloading and installing {} toolchain, if this is the first time it may take a while...", - command.name - ); - let (output, priority) = if let Some(ref priority) = command.priority { - let output = Command::new(NIX_CMD) - .args(PROFILE_INSTALL) - .arg(command.toolchain_link()?) - .arg(PRIORITY) - .arg(priority) - .output() - .map_err(|err| anyhow!("failed to install {} toolchain: {err}", command.name))?; - (output, Some(priority)) + let InstallCommand { name } = command; + + let description = DistToolchainDescription::from_str(&name)?; + + let settings_file = settings_file(); + if !settings_file.exists() { + let settings = SettingsFile::new(settings_file); + settings.with_mut(|s| { + s.default_toolchain = Some(description.to_string()); + Ok(()) + })?; + } + + let mut errored_bins = String::new(); + let mut installed_bins = String::new(); + + warn_existing_fuel_executables()?; + + let toolchain = Toolchain::from_path(&description.to_string()); + let cfgs = if let Ok(channel) = Channel::from_dist_channel(&description) { + channel.build_download_configs() } else { - let output = Command::new(NIX_CMD) - .args(PROFILE_INSTALL) - .arg(command.toolchain_link()?) - .output() - .map_err(|err| anyhow!("failed to install {} toolchain: {err}", command.name))?; - (output, None) + bail!("Could not build download configs from channel") }; - if !output.stdout.is_empty() { - info!("{}", String::from_utf8_lossy(&output.stdout)); - } - if !output.stderr.is_empty() { - info!("{}", String::from_utf8_lossy(&output.stderr)); + + info!( + "Downloading: {}", + cfgs.iter() + .map(|c| c.name.clone() + " ") + .collect::() + ); + + for cfg in cfgs { + match toolchain.add_component(cfg) { + Ok(cfg) => writeln!(installed_bins, "- {} {}", cfg.name, cfg.version)?, + Err(e) => writeln!(errored_bins, "- {e}")?, + }; } - if priority.is_some() { + if errored_bins.is_empty() { + info!("\nInstalled:\n{}", installed_bins); + info!("\nThe Fuel toolchain is installed and up to date"); + } else if installed_bins.is_empty() { + error!("\nfuelup failed to install:\n{}", errored_bins) + } else { info!( - "successfully added {} with priority {}", - command.name, - priority.unwrap() + "\nThe Fuel toolchain is partially installed.\nfuelup failed to install: {}", + errored_bins ); - } + }; Ok(()) } From 47e0ffc821990dccbbdc9cede8a394a42226958e Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 7 Aug 2023 17:12:11 -0500 Subject: [PATCH 36/69] remove shell command --- src/commands/nix/mod.rs | 5 ----- src/commands/nix/shell.rs | 21 --------------------- 2 files changed, 26 deletions(-) delete mode 100644 src/commands/nix/shell.rs diff --git a/src/commands/nix/mod.rs b/src/commands/nix/mod.rs index b5a0fa831..e69f3d83d 100644 --- a/src/commands/nix/mod.rs +++ b/src/commands/nix/mod.rs @@ -11,7 +11,6 @@ mod install; mod link_generator; mod list; mod remove; -mod shell; mod upgrade; macro_rules! nix_info { @@ -27,7 +26,6 @@ macro_rules! nix_info { pub(crate) use nix_info; pub(crate) const NIX_CMD: &str = "nix"; -// pub(crate) const SHELL_ARG: &str = "shell"; pub(crate) const PROFILE_ARG: &str = "profile"; pub(crate) const UNLOCKED_FLAKE_REF: &str = ".*"; pub(crate) const PROFILE_INSTALL_ARGS: &[&str; 2] = &[PROFILE_ARG, "install"]; @@ -48,8 +46,6 @@ pub enum NixCommand { /// with the latest version of the fuel.nix flake. Upgrades all /// installed packages if no index or path is provided. Upgrade(NixUpgradeCommand), - // /// Opens a new bash shell instace with specified toolchain or component available on `$PATH`. - // Shell(NixShellCommand), /// Lists the installed packages by index, unlocked attribute path, /// locked attribute path and nix store path, respectively. List(NixListCommand), @@ -60,7 +56,6 @@ pub fn exec(command: NixCommand) -> Result<()> { NixCommand::Install(command) => nix_install(command), NixCommand::Remove(command) => nix_remove(command), NixCommand::Upgrade(command) => nix_upgrade(command), - // NixCommand::Shell(command) => nix_shell(command), NixCommand::List(_command) => nix_list(_command), } } diff --git a/src/commands/nix/shell.rs b/src/commands/nix/shell.rs deleted file mode 100644 index 228b8be06..000000000 --- a/src/commands/nix/shell.rs +++ /dev/null @@ -1,21 +0,0 @@ -// #[derive(Debug, Parser)] -// pub struct NixShellCommand { -// /// Open a new bash development shell with specified toolchain or component. -// pub name: String, -// } - -// pub fn nix_shell(command: NixShellCommand) -> Result<()> { -// info!( -// "starting new bash shell with fuel {} toolchain available on $PATH...", -// command.name -// ); -// if let Ok(mut child) = Command::new(NIX_CMD) -// .arg(SHELL_ARG) -// .arg(command.toolchain_link()?) -// .spawn() -// { -// child.wait()?; -// } - -// Ok(()) -// } From 73e2f5557de020fe16b5a03a97709475912107c1 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 7 Aug 2023 17:18:56 -0500 Subject: [PATCH 37/69] remove shell impls --- src/commands/nix/link_generator.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/commands/nix/link_generator.rs b/src/commands/nix/link_generator.rs index baf105602..9d4f7ae8b 100644 --- a/src/commands/nix/link_generator.rs +++ b/src/commands/nix/link_generator.rs @@ -72,13 +72,7 @@ impl NeedsNix for NixInstallCommand { self.name.as_str() } } -// impl NeedsNix for NixShellCommand { -// fn name(&self) -> &str { -// self.name.as_str() -// } -// } impl NixName for NixInstallCommand {} -// impl NixName for NixShellCommand {} #[derive(Eq, PartialEq, Debug)] pub(crate) enum FuelToolchain { From f554d33ea9f67c63f090876adbc929023e8062fe Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 9 Aug 2023 21:20:45 -0500 Subject: [PATCH 38/69] wip ci nix check --- .github/workflows/ci.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 17c134d7e..0abde3c5a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,6 +51,36 @@ jobs: - name: Attempt to install fuelup through fuelup-init.sh run: ./fuelup-init.sh + test-fuelup-nix: + needs: cancel-previous-runs + if: github.event_name != 'release' || github.event.action != 'published' + name: Try fuelup installation with fuelup-init + strategy: + matrix: + job: + - os: ubuntu-latest + - os: macos-latest + runs-on: ${{ matrix.job.os }} + steps: + - uses: actions/checkout@v3 + - name: Attempt to install fuelup through fuelup-init.sh + run: ./fuelup-init.sh + - uses: cachix/install-nix-action@v22 + with: + nix_path: nixpkgs=channel:nixos-unstable + - uses: cachix/cachix-action@v12 + with: + name: fuellabs + authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}" + - name: Test fuelup nix install + run: fuelup nix install latest + - name: Test fuelup nix list + run: fuelup nix list + - name: Test fuelup nix upgrade + run: fuelup nix upgrade + - name: Test fuelup nix remove + run: fuelup nix remove 0 + cargo-clippy: needs: cancel-previous-runs runs-on: ubuntu-latest From 69f8b6d8c8f868aeb477051676446024e2652673 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 9 Aug 2023 21:23:39 -0500 Subject: [PATCH 39/69] rm cachix secret --- .github/workflows/ci.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0abde3c5a..2e04ca32e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,10 +68,6 @@ jobs: - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-unstable - - uses: cachix/cachix-action@v12 - with: - name: fuellabs - authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}" - name: Test fuelup nix install run: fuelup nix install latest - name: Test fuelup nix list From db4c1a48d183f6cbddee7068716474ebc4e358ff Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 9 Aug 2023 21:34:00 -0500 Subject: [PATCH 40/69] run fuelup bin via cargo with nix args --- .github/workflows/ci.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2e04ca32e..6bfb7e01f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,19 +63,21 @@ jobs: runs-on: ${{ matrix.job.os }} steps: - uses: actions/checkout@v3 + - name: Install toolchain + uses: dtolnay/rust-toolchain@stable - name: Attempt to install fuelup through fuelup-init.sh run: ./fuelup-init.sh - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-unstable - name: Test fuelup nix install - run: fuelup nix install latest + run: cargo run ~/.cargo/bin/fuelup -- nix install latest - name: Test fuelup nix list - run: fuelup nix list + run: cargo run ~/.cargo/bin/fuelup -- nix list - name: Test fuelup nix upgrade - run: fuelup nix upgrade + run: cargo run ~/.cargo/bin/fuelup -- nix upgrade - name: Test fuelup nix remove - run: fuelup nix remove 0 + run: cargo run ~/.cargo/bin/fuelup -- nix remove 0 cargo-clippy: needs: cancel-previous-runs From d18a233ee6fa35c98630ba2ff10d00bc0462edd2 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 9 Aug 2023 21:43:13 -0500 Subject: [PATCH 41/69] fix usage of fuelup bin --- .github/workflows/ci.yml | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6bfb7e01f..1a8f5045a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,7 +54,7 @@ jobs: test-fuelup-nix: needs: cancel-previous-runs if: github.event_name != 'release' || github.event.action != 'published' - name: Try fuelup installation with fuelup-init + name: Test fuelup nix strategy: matrix: job: @@ -63,21 +63,19 @@ jobs: runs-on: ${{ matrix.job.os }} steps: - uses: actions/checkout@v3 - - name: Install toolchain - uses: dtolnay/rust-toolchain@stable - name: Attempt to install fuelup through fuelup-init.sh run: ./fuelup-init.sh - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-unstable - name: Test fuelup nix install - run: cargo run ~/.cargo/bin/fuelup -- nix install latest + run: ~/.cargo/bin/fuelup nix install latest - name: Test fuelup nix list - run: cargo run ~/.cargo/bin/fuelup -- nix list + run: ~/.cargo/bin/fuelup nix list - name: Test fuelup nix upgrade - run: cargo run ~/.cargo/bin/fuelup -- nix upgrade + run: ~/.cargo/bin/fuelup nix upgrade - name: Test fuelup nix remove - run: cargo run ~/.cargo/bin/fuelup -- nix remove 0 + run: ~/.cargo/bin/fuelup nix remove 0 cargo-clippy: needs: cancel-previous-runs From aa781194727b6f4a96d29220e77f4f95fb7f179f Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 9 Aug 2023 21:46:03 -0500 Subject: [PATCH 42/69] try fix fuelup bin path --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1a8f5045a..014763dfe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,13 +69,13 @@ jobs: with: nix_path: nixpkgs=channel:nixos-unstable - name: Test fuelup nix install - run: ~/.cargo/bin/fuelup nix install latest + run: /home/.cargo/bin/fuelup nix install latest - name: Test fuelup nix list - run: ~/.cargo/bin/fuelup nix list + run: /home/.cargo/bin/fuelup nix list - name: Test fuelup nix upgrade - run: ~/.cargo/bin/fuelup nix upgrade + run: /home/.cargo/bin/fuelup nix upgrade - name: Test fuelup nix remove - run: ~/.cargo/bin/fuelup nix remove 0 + run: /home/.cargo/bin/fuelup nix remove 0 cargo-clippy: needs: cancel-previous-runs From 2586830d6343931c6207648c1ab07b74d0dd4395 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 9 Aug 2023 21:51:59 -0500 Subject: [PATCH 43/69] try adding fuelup to PATH --- .github/workflows/ci.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 014763dfe..db7529c9b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,14 +68,16 @@ jobs: - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-unstable + - name: Add fuelup to PATH + run: export PATH="$HOME/.fuelup/bin:$PATH" - name: Test fuelup nix install - run: /home/.cargo/bin/fuelup nix install latest + run: fuelup nix install latest - name: Test fuelup nix list - run: /home/.cargo/bin/fuelup nix list + run: fuelup nix list - name: Test fuelup nix upgrade - run: /home/.cargo/bin/fuelup nix upgrade + run: fuelup nix upgrade - name: Test fuelup nix remove - run: /home/.cargo/bin/fuelup nix remove 0 + run: fuelup nix remove 0 cargo-clippy: needs: cancel-previous-runs From 7f328f29704c2e3045b93a5c133d9b9ba6a6784f Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 10 Aug 2023 10:37:02 -0500 Subject: [PATCH 44/69] find cargo path --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index db7529c9b..c3bec04e0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,11 +63,15 @@ jobs: runs-on: ${{ matrix.job.os }} steps: - uses: actions/checkout@v3 + - name: Install toolchain + uses: dtolnay/rust-toolchain@stable - name: Attempt to install fuelup through fuelup-init.sh run: ./fuelup-init.sh - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-unstable + - name: Find cargo + run: which cargo - name: Add fuelup to PATH run: export PATH="$HOME/.fuelup/bin:$PATH" - name: Test fuelup nix install From e20600d7a4d3f25949a8a133c5b31420e4e3073e Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 10 Aug 2023 10:52:00 -0500 Subject: [PATCH 45/69] test --- .github/workflows/ci.yml | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c3bec04e0..8766cd901 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,23 +65,21 @@ jobs: - uses: actions/checkout@v3 - name: Install toolchain uses: dtolnay/rust-toolchain@stable - - name: Attempt to install fuelup through fuelup-init.sh - run: ./fuelup-init.sh + - name: Attempt to install fuelup through cargo + run: cargo install --path ./fuelup - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-unstable - - name: Find cargo - run: which cargo - - name: Add fuelup to PATH - run: export PATH="$HOME/.fuelup/bin:$PATH" + - name: Copy nix.conf to /etc/nix/nix.conf + run: sudo cp -f /etc/nix/nix.conf ./nix.conf - name: Test fuelup nix install - run: fuelup nix install latest + run: ~/.cargo/bin/fuelup nix install latest - name: Test fuelup nix list - run: fuelup nix list + run: ~/.cargo/bin/fuelup nix list - name: Test fuelup nix upgrade - run: fuelup nix upgrade + run: ~/.cargo/bin/fuelup nix upgrade - name: Test fuelup nix remove - run: fuelup nix remove 0 + run: ~/.cargo/bin/fuelup nix remove 0 cargo-clippy: needs: cancel-previous-runs From b95bb4e7b92abedbe167b6ed223d04e53db44534 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 10 Aug 2023 11:12:25 -0500 Subject: [PATCH 46/69] trying to get fuelup to install via cargo --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8766cd901..df6d3914c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -66,7 +66,7 @@ jobs: - name: Install toolchain uses: dtolnay/rust-toolchain@stable - name: Attempt to install fuelup through cargo - run: cargo install --path ./fuelup + run: cargo install --path . - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-unstable From 0ee187219e696d92aa76b2753d1b25ab6d81cabb Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 10 Aug 2023 11:36:16 -0500 Subject: [PATCH 47/69] fix preinstall message --- fuelup-init.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 809b22115..15603e78b 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -167,7 +167,9 @@ main() { preinstall_confirmation() { cat 1>&2 < Date: Thu, 10 Aug 2023 11:38:10 -0500 Subject: [PATCH 48/69] fix found nix message --- fuelup-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuelup-init.sh b/fuelup-init.sh index 15603e78b..23b83c73c 100755 --- a/fuelup-init.sh +++ b/fuelup-init.sh @@ -201,7 +201,7 @@ found_nix_message() { Found pre-configured nix installation. -IMPORTANT: Certain nix features must be enabled for fuelup to work. +IMPORTANT: Certain nix features must be enabled for fuelup nix to work. Please follow the instructions below to finish your setup, as fuelup will not amend nix config files for you at this time. From 7ced73951c3cb03abf4e14b0cae79080805c020f Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 10 Aug 2023 15:12:54 -0500 Subject: [PATCH 49/69] add nix.conf file --- nix.conf | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 nix.conf diff --git a/nix.conf b/nix.conf new file mode 100644 index 000000000..6489e288d --- /dev/null +++ b/nix.conf @@ -0,0 +1,9 @@ +# Generated by https://github.com/DeterminateSystems/nix-installer, version 0.9.0. +experimental-features = nix-command flakes auto-allocate-uids +extra-trusted-public-keys = fuellabs.cachix.org-1:3gOmll82VDbT7EggylzOVJ6dr0jgPVU/KMN6+Kf8qx8= +extra-nix-path = nixpkgs=flake:nixpkgs +auto-optimise-store = true +extra-substituters = https://fuellabs.cachix.org +auto-allocate-uids = true +build-users-group = nixbld +bash-prompt-prefix = (nix:$name)\040 \ No newline at end of file From 6244855d4559fc1f1f82578b202f1533a89da032 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Tue, 15 Aug 2023 22:57:16 -0500 Subject: [PATCH 50/69] add cachix auth token --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df6d3914c..a0e3522e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,6 +70,7 @@ jobs: - uses: cachix/install-nix-action@v22 with: nix_path: nixpkgs=channel:nixos-unstable + authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}" - name: Copy nix.conf to /etc/nix/nix.conf run: sudo cp -f /etc/nix/nix.conf ./nix.conf - name: Test fuelup nix install From 2da057ab64daa4d68dbfc6e275ba4e726da030fc Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 16 Aug 2023 01:32:04 -0500 Subject: [PATCH 51/69] auto prioritize packages --- src/commands/nix/install.rs | 144 +++++++++++++++++++++++------------- 1 file changed, 94 insertions(+), 50 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index c86757e03..5512c8152 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -1,70 +1,72 @@ use crate::commands::nix::{ link_generator::{NeedsNix, NixName}, - nix_info, NIX_CMD, PRIORITY_FLAG, PROFILE_INSTALL_ARGS, + NIX_CMD, PRIORITY_FLAG, PROFILE_INSTALL_ARGS, }; use anyhow::{anyhow, bail, Result}; use clap::Parser; -use std::process::Command; +use std::{ + fmt::Debug, + process::{Command, Stdio}, + str::SplitWhitespace, +}; use tracing::info; #[derive(Debug, Parser)] pub struct NixInstallCommand { /// Toolchain or component pub name: String, - pub priority: Option, } pub fn nix_install(command: NixInstallCommand) -> Result<()> { - let (output, priority) = if command.is_toolchain() { + let (output, link) = if command.is_toolchain() { info!( "downloading and installing fuel {} toolchain, this may take a while...", command.name ); - let (output, priority) = if let Some(ref priority) = command.priority { - let output = Command::new(NIX_CMD) - .args(PROFILE_INSTALL_ARGS) - .arg(command.toolchain_link()?) - .arg(PRIORITY_FLAG) - .arg(priority) - .output() - .map_err(|err| { - anyhow!("failed to install fuel {} toolchain: {err}", command.name) - })?; - (output, Some(priority)) - } else { - let output = Command::new(NIX_CMD) + let link = command.toolchain_link()?; + let mut process = Command::new(NIX_CMD); + process + .args(PROFILE_INSTALL_ARGS) + .arg(link.clone()) + .stdout(Stdio::inherit()) + .stderr(Stdio::null()) + .spawn() + .map_err(|err| anyhow!("failed to install fuel {} toolchain: {err}", command.name))? + .wait()?; + // second process is ran to get the output since we silence errors in the first + // this doesn't incur any overhead since nix will look to see if the toolchain is already installed + let mut process = Command::new(NIX_CMD); + ( + process .args(PROFILE_INSTALL_ARGS) - .arg(command.toolchain_link()?) + .arg(link.clone()) .output() - .map_err(|err| { - anyhow!("failed to install fuel {} toolchain: {err}", command.name) - })?; - (output, None) - }; - (output, priority) + .unwrap(), + link, + ) } else if command.is_component() { info!( "downloading and installing {} component, this may take a while...", command.name ); - let (output, priority) = if let Some(ref priority) = command.priority { - let output = Command::new(NIX_CMD) + let link = command.component_link()?; + let mut process = Command::new(NIX_CMD); + process + .args(PROFILE_INSTALL_ARGS) + .arg(link.clone()) + .stdout(Stdio::inherit()) + .stderr(Stdio::null()) + .spawn() + .map_err(|err| anyhow!("failed to install {} component: {err}", command.name))? + .wait()?; + ( + process .args(PROFILE_INSTALL_ARGS) - .arg(command.component_link()?) - .arg(PRIORITY_FLAG) - .arg(priority) + .arg(link.clone()) .output() - .map_err(|err| anyhow!("failed to install {} component: {err}", command.name))?; - (output, Some(priority)) - } else { - let output = Command::new(NIX_CMD) - .args(PROFILE_INSTALL_ARGS) - .arg(command.component_link()?) - .output() - .map_err(|err| anyhow!("failed to install {} component: {err}", command.name))?; - (output, None) - }; - (output, priority) + .unwrap(), + link, + ) } else { bail!( "available distrubuted components:\n -fuel-core\n -fuel-core-client\n -fuel-indexer\n -forc\n -forc-client\n -forc-doc\n -forc-explore\n -forc-fmt\n -forc-index\n -forc-lsp\n -forc-tx\n -forc-wallet\n -sway-vim\n @@ -74,17 +76,59 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" ) }; - nix_info!(output); - - if priority.is_some() && output.stderr.is_empty() { - info!( - "successfully added {} with priority {}", - command.name, - priority.unwrap() - ); - } else { - info!("successfully added {}", command.name,); + // hacky way of getting the priority of the package automatically + if !output.stderr.is_empty() { + const NIX_PKG_MSG: &str = "The conflicting packages have a priority of"; + const NIXOS_MSG: &str = "have the same priority"; + let err_str = String::from_utf8_lossy(&output.stderr); + // nix package manager + if let Some(index) = err_str.find(NIX_PKG_MSG) { + let (_first, last) = err_str.split_at(index); + let iter = last.split_whitespace(); + auto_prioritize_installed_package(iter, 7, link)?; + // nixos + } else if let Some(index) = err_str.find(NIXOS_MSG) { + let (_first, last) = err_str.split_at(index); + let iter = last.split_whitespace(); + auto_prioritize_installed_package(iter, 4, link)?; + } } + // nix_info!(output); + + Ok(()) +} + +/// Given an iterator over a priority error message, get the priority for the installed packages +/// and prioritize the newly installed package. +/// +/// This does not incur an overhead since nix will check if the package is already installed. +fn auto_prioritize_installed_package( + mut iter: SplitWhitespace, + len: usize, + link: String, +) -> Result<()> { + for _ in 0..len { + iter.next(); + } + if let Some(prio) = iter.next() { + let chars = prio.chars(); + if let Ok(num) = chars + .filter(|c| c.is_digit(10)) + .collect::() + .parse::() + { + Command::new(NIX_CMD) + .args(PROFILE_INSTALL_ARGS) + .arg(link) + .arg(PRIORITY_FLAG) + .arg((num - 1).to_string()) + .stdout(Stdio::inherit()) + .stderr(Stdio::null()) + .spawn() + .map_err(|err| anyhow!("failed to auto set package priority: {err}"))? + .wait()?; + } + } Ok(()) } From b3201886bcd4ccaa95ff393d6e66b45574793b44 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 17 Aug 2023 14:01:29 -0500 Subject: [PATCH 52/69] add stdout inherit to upgrade command, update nix_info macro with issue template link --- src/commands/nix/mod.rs | 6 +++++- src/commands/nix/upgrade.rs | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/commands/nix/mod.rs b/src/commands/nix/mod.rs index e69f3d83d..fe7d64bfd 100644 --- a/src/commands/nix/mod.rs +++ b/src/commands/nix/mod.rs @@ -19,7 +19,11 @@ macro_rules! nix_info { info!("{}", String::from_utf8_lossy(&$output.stdout)); } if !$output.stderr.is_empty() { - info!("{}", String::from_utf8_lossy(&$output.stderr)); + info!( +"fuelup nix encountered an problem, please open an issue at https://github.com/FuelLabs/fuelup/issues/new + +{}", String::from_utf8_lossy(&$output.stderr) + ); } }; } diff --git a/src/commands/nix/upgrade.rs b/src/commands/nix/upgrade.rs index f78abff6b..a674edc3c 100644 --- a/src/commands/nix/upgrade.rs +++ b/src/commands/nix/upgrade.rs @@ -1,7 +1,7 @@ use crate::commands::nix::{nix_info, NIX_CMD, PROFILE_UPGRADE_ARGS, UNLOCKED_FLAKE_REF}; use anyhow::Result; use clap::Parser; -use std::process::Command; +use std::process::{Command, Stdio}; use tracing::info; #[derive(Debug, Parser)] @@ -11,13 +11,31 @@ pub struct NixUpgradeCommand { pub fn nix_upgrade(command: NixUpgradeCommand) -> Result<()> { let output = if let Some(pkg) = command.pkg { - info!("upgrading installed fuel.nix package, this may take a while..."); + info!("upgrading {pkg}, this may take a while..."); + Command::new(NIX_CMD) + .args(PROFILE_UPGRADE_ARGS) + .arg(pkg.clone()) + .stdout(Stdio::inherit()) + .stderr(Stdio::null()) + .spawn()? + .wait()?; + // capture output of the command + // because nix checks if the package is already up to date + // this doesn't incur any extra overhead but + // allows us to manage how errors are presented to users Command::new(NIX_CMD) .args(PROFILE_UPGRADE_ARGS) .arg(pkg) .output()? } else { info!("upgrading installed fuel.nix packages, this may take a while..."); + Command::new(NIX_CMD) + .args(PROFILE_UPGRADE_ARGS) + .arg(UNLOCKED_FLAKE_REF) + .stdout(Stdio::inherit()) + .stderr(Stdio::null()) + .spawn()? + .wait()?; Command::new(NIX_CMD) .args(PROFILE_UPGRADE_ARGS) .arg(UNLOCKED_FLAKE_REF) From 20007b12bb5f12043e655e0aff3916096617bf76 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 17 Aug 2023 14:02:35 -0500 Subject: [PATCH 53/69] clippy --- src/commands/nix/install.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 5512c8152..1d43db149 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -114,7 +114,7 @@ fn auto_prioritize_installed_package( if let Some(prio) = iter.next() { let chars = prio.chars(); if let Ok(num) = chars - .filter(|c| c.is_digit(10)) + .filter(|c| c.is_ascii_digit()) .collect::() .parse::() { From 1bf2b041cf8dc58bbe8458f15aa9b182786863cd Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 17 Aug 2023 14:04:22 -0500 Subject: [PATCH 54/69] update info message for upgrade command --- src/commands/nix/upgrade.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/nix/upgrade.rs b/src/commands/nix/upgrade.rs index a674edc3c..78438d8d4 100644 --- a/src/commands/nix/upgrade.rs +++ b/src/commands/nix/upgrade.rs @@ -11,7 +11,7 @@ pub struct NixUpgradeCommand { pub fn nix_upgrade(command: NixUpgradeCommand) -> Result<()> { let output = if let Some(pkg) = command.pkg { - info!("upgrading {pkg}, this may take a while..."); + info!("upgrading package {pkg}, this may take a while..."); Command::new(NIX_CMD) .args(PROFILE_UPGRADE_ARGS) .arg(pkg.clone()) From 8489142245a62a2e2e80402cc192891b5a39b6b1 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 17 Aug 2023 18:30:47 -0500 Subject: [PATCH 55/69] fix stdout and add handling message for auto prioritization --- src/commands/nix/install.rs | 42 ++++++++++++++++++------------------- src/commands/nix/upgrade.rs | 2 -- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 1d43db149..ff9b3250c 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -24,24 +24,20 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { command.name ); let link = command.toolchain_link()?; - let mut process = Command::new(NIX_CMD); - process + Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) .arg(link.clone()) .stdout(Stdio::inherit()) - .stderr(Stdio::null()) .spawn() .map_err(|err| anyhow!("failed to install fuel {} toolchain: {err}", command.name))? .wait()?; // second process is ran to get the output since we silence errors in the first // this doesn't incur any overhead since nix will look to see if the toolchain is already installed - let mut process = Command::new(NIX_CMD); ( - process + Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) .arg(link.clone()) - .output() - .unwrap(), + .output()?, link, ) } else if command.is_component() { @@ -50,21 +46,18 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { command.name ); let link = command.component_link()?; - let mut process = Command::new(NIX_CMD); - process + Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) .arg(link.clone()) .stdout(Stdio::inherit()) - .stderr(Stdio::null()) .spawn() .map_err(|err| anyhow!("failed to install {} component: {err}", command.name))? .wait()?; ( - process + Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) .arg(link.clone()) - .output() - .unwrap(), + .output()?, link, ) } else { @@ -85,17 +78,15 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" if let Some(index) = err_str.find(NIX_PKG_MSG) { let (_first, last) = err_str.split_at(index); let iter = last.split_whitespace(); - auto_prioritize_installed_package(iter, 7, link)?; + auto_prioritize_installed_package(iter, 7, link, command.name)?; // nixos } else if let Some(index) = err_str.find(NIXOS_MSG) { let (_first, last) = err_str.split_at(index); let iter = last.split_whitespace(); - auto_prioritize_installed_package(iter, 4, link)?; + auto_prioritize_installed_package(iter, 4, link, command.name)?; } } - // nix_info!(output); - Ok(()) } @@ -107,6 +98,7 @@ fn auto_prioritize_installed_package( mut iter: SplitWhitespace, len: usize, link: String, + name: String, ) -> Result<()> { for _ in 0..len { iter.next(); @@ -118,16 +110,24 @@ fn auto_prioritize_installed_package( .collect::() .parse::() { - Command::new(NIX_CMD) + let priority = num - 1; + let mut process = Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) .arg(link) .arg(PRIORITY_FLAG) - .arg((num - 1).to_string()) + .arg(priority.to_string()) .stdout(Stdio::inherit()) .stderr(Stdio::null()) .spawn() - .map_err(|err| anyhow!("failed to auto set package priority: {err}"))? - .wait()?; + .map_err(|err| anyhow!("failed to auto set package priority: {err}"))?; + process.wait()?; + info!( + " +Fuelup: newly installed binaries for '{name}' were given priority {}, making it the default. + +No further action is necessary.", + priority + ); } } Ok(()) diff --git a/src/commands/nix/upgrade.rs b/src/commands/nix/upgrade.rs index 78438d8d4..2f46dc6dd 100644 --- a/src/commands/nix/upgrade.rs +++ b/src/commands/nix/upgrade.rs @@ -16,7 +16,6 @@ pub fn nix_upgrade(command: NixUpgradeCommand) -> Result<()> { .args(PROFILE_UPGRADE_ARGS) .arg(pkg.clone()) .stdout(Stdio::inherit()) - .stderr(Stdio::null()) .spawn()? .wait()?; // capture output of the command @@ -33,7 +32,6 @@ pub fn nix_upgrade(command: NixUpgradeCommand) -> Result<()> { .args(PROFILE_UPGRADE_ARGS) .arg(UNLOCKED_FLAKE_REF) .stdout(Stdio::inherit()) - .stderr(Stdio::null()) .spawn()? .wait()?; Command::new(NIX_CMD) From f3ef82a0a0ffd05ba788acee2025edc6c7d6ca6b Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Sat, 19 Aug 2023 02:50:37 -0500 Subject: [PATCH 56/69] wip remove error from log --- src/commands/nix/install.rs | 150 ++++++++++++++++++++++++------------ 1 file changed, 102 insertions(+), 48 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index ff9b3250c..0cf55ce9b 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -6,8 +6,11 @@ use anyhow::{anyhow, bail, Result}; use clap::Parser; use std::{ fmt::Debug, + io::{BufRead, BufReader}, process::{Command, Stdio}, str::SplitWhitespace, + sync::mpsc, + thread, }; use tracing::info; @@ -18,48 +21,106 @@ pub struct NixInstallCommand { } pub fn nix_install(command: NixInstallCommand) -> Result<()> { - let (output, link) = if command.is_toolchain() { + let (tx, rx) = mpsc::channel(); + let (err_str, link) = if command.is_toolchain() { info!( "downloading and installing fuel {} toolchain, this may take a while...", command.name ); let link = command.toolchain_link()?; - Command::new(NIX_CMD) + + let link_clone = link.clone(); + let command_name = command.name.clone(); + let mut err_lines = Vec::new(); + if let Ok(mut child) = Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) - .arg(link.clone()) - .stdout(Stdio::inherit()) + .arg(link_clone) + .arg("--log-format") + .arg("bar-with-logs") + .stderr(Stdio::piped()) .spawn() - .map_err(|err| anyhow!("failed to install fuel {} toolchain: {err}", command.name))? - .wait()?; - // second process is ran to get the output since we silence errors in the first - // this doesn't incur any overhead since nix will look to see if the toolchain is already installed - ( - Command::new(NIX_CMD) - .args(PROFILE_INSTALL_ARGS) - .arg(link.clone()) - .output()?, - link, - ) + .map_err(|err| anyhow!("failed to install fuel {} toolchain: {err}", command_name)) + { + let handle = thread::spawn(move || { + while let Some(stderr) = child.stderr.take() { + let reader = BufReader::new(stderr); + + let mut contains_err = false; + + for line in reader.lines() { + if let Ok(line) = line { + if !line.contains("error") && !contains_err { + tx.send((Some(line), None)).unwrap(); + } else { + contains_err = true; + tx.send((None, Some(line))).unwrap(); + } + } + } + } + }); + + while let Ok((line, err)) = rx.recv() { + if let Some(line) = line { + info!("AYO: {line}"); + } + if let Some(err) = err { + err_lines.push(err); + } + } + handle.join().unwrap(); + } + + (err_lines.concat(), link) } else if command.is_component() { info!( "downloading and installing {} component, this may take a while...", command.name ); let link = command.component_link()?; - Command::new(NIX_CMD) - .args(PROFILE_INSTALL_ARGS) - .arg(link.clone()) - .stdout(Stdio::inherit()) - .spawn() - .map_err(|err| anyhow!("failed to install {} component: {err}", command.name))? - .wait()?; - ( - Command::new(NIX_CMD) + + let link_clone = link.clone(); + let command_name = command.name.clone(); + + let handle = thread::spawn(move || { + if let Ok(mut child) = Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) - .arg(link.clone()) - .output()?, - link, - ) + .arg(link_clone) + .stderr(Stdio::piped()) + .spawn() + .map_err(|err| anyhow!("failed to install {} component: {err}", command_name)) + { + while let Some(stderr) = child.stderr.take() { + let reader = BufReader::new(stderr); + + let mut contains_err = false; + + for line in reader.lines() { + if let Ok(line) = line { + if !line.contains("error") && !contains_err { + tx.send((Some(line), None)).unwrap(); + } else { + contains_err = true; + tx.send((None, Some(line))).unwrap(); + } + } + } + } + } + }); + + let mut err_lines = Vec::new(); + while let Ok((line, err)) = rx.recv() { + if let Some(line) = line { + info!("{line}"); + } + if let Some(err) = err { + err_lines.push(err); + } + } + handle.join().unwrap(); + + (err_lines.concat(), link) } else { bail!( "available distrubuted components:\n -fuel-core\n -fuel-core-client\n -fuel-indexer\n -forc\n -forc-client\n -forc-doc\n -forc-explore\n -forc-fmt\n -forc-index\n -forc-lsp\n -forc-tx\n -forc-wallet\n -sway-vim\n @@ -70,20 +131,19 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" }; // hacky way of getting the priority of the package automatically - if !output.stderr.is_empty() { + if !err_str.is_empty() { const NIX_PKG_MSG: &str = "The conflicting packages have a priority of"; const NIXOS_MSG: &str = "have the same priority"; - let err_str = String::from_utf8_lossy(&output.stderr); // nix package manager if let Some(index) = err_str.find(NIX_PKG_MSG) { let (_first, last) = err_str.split_at(index); let iter = last.split_whitespace(); - auto_prioritize_installed_package(iter, 7, link, command.name)?; + auto_prioritize_installed_package(iter, 7, link)?; // nixos } else if let Some(index) = err_str.find(NIXOS_MSG) { let (_first, last) = err_str.split_at(index); let iter = last.split_whitespace(); - auto_prioritize_installed_package(iter, 4, link, command.name)?; + auto_prioritize_installed_package(iter, 4, link)?; } } @@ -98,7 +158,6 @@ fn auto_prioritize_installed_package( mut iter: SplitWhitespace, len: usize, link: String, - name: String, ) -> Result<()> { for _ in 0..len { iter.next(); @@ -110,24 +169,19 @@ fn auto_prioritize_installed_package( .collect::() .parse::() { - let priority = num - 1; - let mut process = Command::new(NIX_CMD) + // increase the number for the priority of the pkg that is older + // if we decrease the priority for the new package, eventually + // we could end up with collisions were two packages have the highest priority (0) + println!("cmd: nix profile install --priority {} {}", num - 1, link); + Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) - .arg(link) .arg(PRIORITY_FLAG) - .arg(priority.to_string()) - .stdout(Stdio::inherit()) - .stderr(Stdio::null()) + .arg((num - 1).to_string()) + .arg(link) + // .stderr(Stdio::null()) .spawn() - .map_err(|err| anyhow!("failed to auto set package priority: {err}"))?; - process.wait()?; - info!( - " -Fuelup: newly installed binaries for '{name}' were given priority {}, making it the default. - -No further action is necessary.", - priority - ); + .map_err(|err| anyhow!("failed to auto set package priority: {err}"))? + .wait()?; } } Ok(()) From 0156f2c88e1b7d49bb0ea707acfba07e6895260b Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Sat, 19 Aug 2023 22:56:49 -0500 Subject: [PATCH 57/69] finalize auto prioritize pkgs, fix stderr output --- src/commands/nix/install.rs | 145 +++++++++++++++++++----------------- src/commands/nix/mod.rs | 11 ++- 2 files changed, 83 insertions(+), 73 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 0cf55ce9b..66bd1724a 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -7,13 +7,16 @@ use clap::Parser; use std::{ fmt::Debug, io::{BufRead, BufReader}, - process::{Command, Stdio}, + process::{Command, Output, Stdio}, str::SplitWhitespace, sync::mpsc, thread, }; use tracing::info; +const NIX_PKG_MSG: &str = "The conflicting packages have a priority of"; +const NIXOS_MSG: &str = "have the same priority"; + #[derive(Debug, Parser)] pub struct NixInstallCommand { /// Toolchain or component @@ -22,21 +25,20 @@ pub struct NixInstallCommand { pub fn nix_install(command: NixInstallCommand) -> Result<()> { let (tx, rx) = mpsc::channel(); - let (err_str, link) = if command.is_toolchain() { + let (priority_err, link) = if command.is_toolchain() { info!( "downloading and installing fuel {} toolchain, this may take a while...", command.name ); let link = command.toolchain_link()?; - let link_clone = link.clone(); let command_name = command.name.clone(); - let mut err_lines = Vec::new(); + let mut priority_err = Vec::new(); + + // filter the priority errors so we can handle this for the user. if let Ok(mut child) = Command::new(NIX_CMD) .args(PROFILE_INSTALL_ARGS) .arg(link_clone) - .arg("--log-format") - .arg("bar-with-logs") .stderr(Stdio::piped()) .spawn() .map_err(|err| anyhow!("failed to install fuel {} toolchain: {err}", command_name)) @@ -45,15 +47,12 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { while let Some(stderr) = child.stderr.take() { let reader = BufReader::new(stderr); - let mut contains_err = false; - for line in reader.lines() { if let Ok(line) = line { - if !line.contains("error") && !contains_err { - tx.send((Some(line), None)).unwrap(); - } else { - contains_err = true; + if line.contains(NIXOS_MSG) || line.contains(NIX_PKG_MSG) { tx.send((None, Some(line))).unwrap(); + } else { + tx.send((Some(line), None)).unwrap(); } } } @@ -62,65 +61,62 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { while let Ok((line, err)) = rx.recv() { if let Some(line) = line { - info!("AYO: {line}"); + info!("{line}"); } if let Some(err) = err { - err_lines.push(err); + priority_err.push(err); } } handle.join().unwrap(); } - (err_lines.concat(), link) + (priority_err.concat(), link) } else if command.is_component() { info!( - "downloading and installing {} component, this may take a while...", + "downloading and installing component {}, this may take a while...", command.name ); let link = command.component_link()?; - let link_clone = link.clone(); let command_name = command.name.clone(); + let mut priority_err = Vec::new(); - let handle = thread::spawn(move || { - if let Ok(mut child) = Command::new(NIX_CMD) - .args(PROFILE_INSTALL_ARGS) - .arg(link_clone) - .stderr(Stdio::piped()) - .spawn() - .map_err(|err| anyhow!("failed to install {} component: {err}", command_name)) - { + // filter the priority errors so we can handle this for the user. + if let Ok(mut child) = Command::new(NIX_CMD) + .args(PROFILE_INSTALL_ARGS) + .arg(link_clone) + .stderr(Stdio::piped()) + .spawn() + .map_err(|err| anyhow!("failed to install component {}: {err}", command_name)) + { + let handle = thread::spawn(move || { while let Some(stderr) = child.stderr.take() { let reader = BufReader::new(stderr); - let mut contains_err = false; - for line in reader.lines() { if let Ok(line) = line { - if !line.contains("error") && !contains_err { - tx.send((Some(line), None)).unwrap(); - } else { - contains_err = true; + if line.contains(NIXOS_MSG) || line.contains(NIX_PKG_MSG) { tx.send((None, Some(line))).unwrap(); + } else { + tx.send((Some(line), None)).unwrap(); } } } } - } - }); + }); - let mut err_lines = Vec::new(); - while let Ok((line, err)) = rx.recv() { - if let Some(line) = line { - info!("{line}"); - } - if let Some(err) = err { - err_lines.push(err); + while let Ok((line, err)) = rx.recv() { + if let Some(line) = line { + info!("{line}"); + } + if let Some(err) = err { + priority_err.push(err); + } } + handle.join().unwrap(); } - handle.join().unwrap(); - (err_lines.concat(), link) + (priority_err.concat(), link) } else { bail!( "available distrubuted components:\n -fuel-core\n -fuel-core-client\n -fuel-indexer\n -forc\n -forc-client\n -forc-doc\n -forc-explore\n -forc-fmt\n -forc-index\n -forc-lsp\n -forc-tx\n -forc-wallet\n -sway-vim\n @@ -131,18 +127,16 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" }; // hacky way of getting the priority of the package automatically - if !err_str.is_empty() { - const NIX_PKG_MSG: &str = "The conflicting packages have a priority of"; - const NIXOS_MSG: &str = "have the same priority"; + if !priority_err.is_empty() { // nix package manager - if let Some(index) = err_str.find(NIX_PKG_MSG) { - let (_first, last) = err_str.split_at(index); - let iter = last.split_whitespace(); + if let Some(index) = priority_err.find(NIX_PKG_MSG) { + let (_, err) = priority_err.split_at(index); + let iter = err.split_whitespace(); auto_prioritize_installed_package(iter, 7, link)?; // nixos - } else if let Some(index) = err_str.find(NIXOS_MSG) { - let (_first, last) = err_str.split_at(index); - let iter = last.split_whitespace(); + } else if let Some(index) = priority_err.find(NIXOS_MSG) { + let (_, err) = priority_err.split_at(index); + let iter = err.split_whitespace(); auto_prioritize_installed_package(iter, 4, link)?; } } @@ -156,32 +150,43 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" /// This does not incur an overhead since nix will check if the package is already installed. fn auto_prioritize_installed_package( mut iter: SplitWhitespace, - len: usize, + msg_len: usize, link: String, ) -> Result<()> { - for _ in 0..len { + for _ in 0..msg_len { iter.next(); } - if let Some(prio) = iter.next() { - let chars = prio.chars(); - if let Ok(num) = chars + if let Some(given_priority) = iter.next() { + let chars = given_priority.chars(); + if let Ok(current_pkg_priority) = chars .filter(|c| c.is_ascii_digit()) .collect::() - .parse::() + .parse::() { - // increase the number for the priority of the pkg that is older - // if we decrease the priority for the new package, eventually - // we could end up with collisions were two packages have the highest priority (0) - println!("cmd: nix profile install --priority {} {}", num - 1, link); - Command::new(NIX_CMD) - .args(PROFILE_INSTALL_ARGS) - .arg(PRIORITY_FLAG) - .arg((num - 1).to_string()) - .arg(link) - // .stderr(Stdio::null()) - .spawn() - .map_err(|err| anyhow!("failed to auto set package priority: {err}"))? - .wait()?; + try_prioritize(current_pkg_priority, link)? + } + } + Ok(()) +} +/// `nix profile install --priority` can be negative, so here we just continue to try +/// installing the package with decreasing priority number until the error goes away. +/// +/// There currently isn't a way to check the priority of packages other than the error +/// provided by nix when installing a package that it finds a conflict with. +fn try_prioritize(mut pkg_priority: i32, link: String) -> Result<()> { + pkg_priority -= 1; + let output = Command::new(NIX_CMD) + .args(PROFILE_INSTALL_ARGS) + .arg(PRIORITY_FLAG) + .arg(pkg_priority.to_string()) + .arg(link.clone()) + .output()?; + if !output.stderr.is_empty() { + let stderr_str = String::from_utf8_lossy(&output.stderr); + if stderr_str.contains(NIXOS_MSG) || stderr_str.contains(NIX_PKG_MSG) { + // recursively decriment the package priority until the + // newly installed package has the highest priority + try_prioritize(pkg_priority, link)? } } Ok(()) diff --git a/src/commands/nix/mod.rs b/src/commands/nix/mod.rs index fe7d64bfd..867f72675 100644 --- a/src/commands/nix/mod.rs +++ b/src/commands/nix/mod.rs @@ -19,11 +19,16 @@ macro_rules! nix_info { info!("{}", String::from_utf8_lossy(&$output.stdout)); } if !$output.stderr.is_empty() { - info!( + let err_str = String::from_utf8_lossy(&$output.stderr); + if err_str.contains("error") { + info!( "fuelup nix encountered an problem, please open an issue at https://github.com/FuelLabs/fuelup/issues/new -{}", String::from_utf8_lossy(&$output.stderr) - ); +{}", err_str + ); + } else { + info!("{}", err_str); + } } }; } From ea29b8590c320071c625fc6db880b311c09f1aaa Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Sat, 19 Aug 2023 23:09:42 -0500 Subject: [PATCH 58/69] abstract command filter function --- src/commands/nix/install.rs | 129 +++++++++++++++--------------------- 1 file changed, 53 insertions(+), 76 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 66bd1724a..34443f3f0 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -7,7 +7,7 @@ use clap::Parser; use std::{ fmt::Debug, io::{BufRead, BufReader}, - process::{Command, Output, Stdio}, + process::{Command, Stdio}, str::SplitWhitespace, sync::mpsc, thread, @@ -24,51 +24,14 @@ pub struct NixInstallCommand { } pub fn nix_install(command: NixInstallCommand) -> Result<()> { - let (tx, rx) = mpsc::channel(); let (priority_err, link) = if command.is_toolchain() { info!( "downloading and installing fuel {} toolchain, this may take a while...", command.name ); let link = command.toolchain_link()?; - let link_clone = link.clone(); - let command_name = command.name.clone(); let mut priority_err = Vec::new(); - - // filter the priority errors so we can handle this for the user. - if let Ok(mut child) = Command::new(NIX_CMD) - .args(PROFILE_INSTALL_ARGS) - .arg(link_clone) - .stderr(Stdio::piped()) - .spawn() - .map_err(|err| anyhow!("failed to install fuel {} toolchain: {err}", command_name)) - { - let handle = thread::spawn(move || { - while let Some(stderr) = child.stderr.take() { - let reader = BufReader::new(stderr); - - for line in reader.lines() { - if let Ok(line) = line { - if line.contains(NIXOS_MSG) || line.contains(NIX_PKG_MSG) { - tx.send((None, Some(line))).unwrap(); - } else { - tx.send((Some(line), None)).unwrap(); - } - } - } - } - }); - - while let Ok((line, err)) = rx.recv() { - if let Some(line) = line { - info!("{line}"); - } - if let Some(err) = err { - priority_err.push(err); - } - } - handle.join().unwrap(); - } + filter_command(link.clone(), command.name.clone(), &mut priority_err, true); (priority_err.concat(), link) } else if command.is_component() { @@ -77,44 +40,8 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { command.name ); let link = command.component_link()?; - let link_clone = link.clone(); - let command_name = command.name.clone(); let mut priority_err = Vec::new(); - - // filter the priority errors so we can handle this for the user. - if let Ok(mut child) = Command::new(NIX_CMD) - .args(PROFILE_INSTALL_ARGS) - .arg(link_clone) - .stderr(Stdio::piped()) - .spawn() - .map_err(|err| anyhow!("failed to install component {}: {err}", command_name)) - { - let handle = thread::spawn(move || { - while let Some(stderr) = child.stderr.take() { - let reader = BufReader::new(stderr); - - for line in reader.lines() { - if let Ok(line) = line { - if line.contains(NIXOS_MSG) || line.contains(NIX_PKG_MSG) { - tx.send((None, Some(line))).unwrap(); - } else { - tx.send((Some(line), None)).unwrap(); - } - } - } - } - }); - - while let Ok((line, err)) = rx.recv() { - if let Some(line) = line { - info!("{line}"); - } - if let Some(err) = err { - priority_err.push(err); - } - } - handle.join().unwrap(); - } + filter_command(link.clone(), command.name.clone(), &mut priority_err, false); (priority_err.concat(), link) } else { @@ -144,6 +71,55 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" Ok(()) } +/// Filter the priority errors so we can handle it for the user automatically. +fn filter_command( + link_clone: String, + command_name: String, + priority_err: &mut Vec, + is_toolchain_cmd: bool, +) { + let (tx, rx) = mpsc::channel(); + if let Ok(mut child) = Command::new(NIX_CMD) + .args(PROFILE_INSTALL_ARGS) + .arg(link_clone) + .stderr(Stdio::piped()) + .spawn() + .map_err(|err| { + if is_toolchain_cmd { + anyhow!("failed to install fuel {} toolchain: {err}", command_name) + } else { + anyhow!("failed to install component {}: {err}", command_name) + } + }) + { + let handle = thread::spawn(move || { + while let Some(stderr) = child.stderr.take() { + let reader = BufReader::new(stderr); + + for line in reader.lines() { + if let Ok(line) = line { + if line.contains(NIXOS_MSG) || line.contains(NIX_PKG_MSG) { + tx.send((None, Some(line))).unwrap(); + } else { + tx.send((Some(line), None)).unwrap(); + } + } + } + } + }); + + while let Ok((line, err)) = rx.recv() { + if let Some(line) = line { + info!("{line}"); + } + if let Some(err) = err { + priority_err.push(err); + } + } + handle.join().unwrap(); + } +} + /// Given an iterator over a priority error message, get the priority for the installed packages /// and prioritize the newly installed package. /// @@ -168,6 +144,7 @@ fn auto_prioritize_installed_package( } Ok(()) } + /// `nix profile install --priority` can be negative, so here we just continue to try /// installing the package with decreasing priority number until the error goes away. /// From 061391843021e12845cef24dd35e3e2c889542b5 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Sat, 19 Aug 2023 23:10:37 -0500 Subject: [PATCH 59/69] rename prio msg consts --- src/commands/nix/install.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 34443f3f0..6311588bd 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -14,8 +14,8 @@ use std::{ }; use tracing::info; -const NIX_PKG_MSG: &str = "The conflicting packages have a priority of"; -const NIXOS_MSG: &str = "have the same priority"; +const NIX_PKG_PRIORITY_MSG: &str = "The conflicting packages have a priority of"; +const NIXOS_PRIORITY_MSG: &str = "have the same priority"; #[derive(Debug, Parser)] pub struct NixInstallCommand { @@ -56,12 +56,12 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" // hacky way of getting the priority of the package automatically if !priority_err.is_empty() { // nix package manager - if let Some(index) = priority_err.find(NIX_PKG_MSG) { + if let Some(index) = priority_err.find(NIX_PKG_PRIORITY_MSG) { let (_, err) = priority_err.split_at(index); let iter = err.split_whitespace(); auto_prioritize_installed_package(iter, 7, link)?; // nixos - } else if let Some(index) = priority_err.find(NIXOS_MSG) { + } else if let Some(index) = priority_err.find(NIXOS_PRIORITY_MSG) { let (_, err) = priority_err.split_at(index); let iter = err.split_whitespace(); auto_prioritize_installed_package(iter, 4, link)?; @@ -98,7 +98,8 @@ fn filter_command( for line in reader.lines() { if let Ok(line) = line { - if line.contains(NIXOS_MSG) || line.contains(NIX_PKG_MSG) { + if line.contains(NIXOS_PRIORITY_MSG) || line.contains(NIX_PKG_PRIORITY_MSG) + { tx.send((None, Some(line))).unwrap(); } else { tx.send((Some(line), None)).unwrap(); @@ -160,7 +161,7 @@ fn try_prioritize(mut pkg_priority: i32, link: String) -> Result<()> { .output()?; if !output.stderr.is_empty() { let stderr_str = String::from_utf8_lossy(&output.stderr); - if stderr_str.contains(NIXOS_MSG) || stderr_str.contains(NIX_PKG_MSG) { + if stderr_str.contains(NIXOS_PRIORITY_MSG) || stderr_str.contains(NIX_PKG_PRIORITY_MSG) { // recursively decriment the package priority until the // newly installed package has the highest priority try_prioritize(pkg_priority, link)? From c5100dba3375cf9395e43bf2808ae54b8dc63634 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Sat, 19 Aug 2023 23:15:00 -0500 Subject: [PATCH 60/69] update docstring for filter_command --- src/commands/nix/install.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 6311588bd..c5ec1ab8f 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -71,7 +71,7 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" Ok(()) } -/// Filter the priority errors so we can handle it for the user automatically. +/// Execute the `Command` and filter the priority errors so we can handle it for the user automatically. fn filter_command( link_clone: String, command_name: String, From d46db1ee9c2433b384f920173a0ce85fd912769e Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Sat, 19 Aug 2023 23:29:13 -0500 Subject: [PATCH 61/69] clippy --- src/commands/nix/install.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index c5ec1ab8f..020450bc9 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -96,14 +96,11 @@ fn filter_command( while let Some(stderr) = child.stderr.take() { let reader = BufReader::new(stderr); - for line in reader.lines() { - if let Ok(line) = line { - if line.contains(NIXOS_PRIORITY_MSG) || line.contains(NIX_PKG_PRIORITY_MSG) - { - tx.send((None, Some(line))).unwrap(); - } else { - tx.send((Some(line), None)).unwrap(); - } + for line in reader.lines().flatten() { + if line.contains(NIXOS_PRIORITY_MSG) || line.contains(NIX_PKG_PRIORITY_MSG) { + tx.send((None, Some(line))).unwrap(); + } else { + tx.send((Some(line), None)).unwrap(); } } } From 70b84ff4cb78977d0e840978b7b824b624993d43 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 21 Aug 2023 01:31:10 -0500 Subject: [PATCH 62/69] fix error handling of install, wip list --- src/commands/nix/install.rs | 78 +++++++++++++++++++++++------- src/commands/nix/link_generator.rs | 2 + src/commands/nix/list.rs | 78 +++++++++++++++++++++++++++++- src/commands/nix/mod.rs | 5 +- 4 files changed, 143 insertions(+), 20 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 020450bc9..26ef170e1 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -21,19 +21,28 @@ const NIXOS_PRIORITY_MSG: &str = "have the same priority"; pub struct NixInstallCommand { /// Toolchain or component pub name: String, + pub verbose: Option, } pub fn nix_install(command: NixInstallCommand) -> Result<()> { - let (priority_err, link) = if command.is_toolchain() { + let mut success = false; + let (priority_err, all_errs, link) = if command.is_toolchain() { info!( "downloading and installing fuel {} toolchain, this may take a while...", command.name ); let link = command.toolchain_link()?; let mut priority_err = Vec::new(); - filter_command(link.clone(), command.name.clone(), &mut priority_err, true); + let mut all_errs = Vec::new(); + filter_command( + link.clone(), + command.name.clone(), + &mut priority_err, + &mut all_errs, + true, + ); - (priority_err.concat(), link) + (priority_err.concat(), all_errs.concat(), link) } else if command.is_component() { info!( "downloading and installing component {}, this may take a while...", @@ -41,9 +50,16 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { ); let link = command.component_link()?; let mut priority_err = Vec::new(); - filter_command(link.clone(), command.name.clone(), &mut priority_err, false); + let mut all_errs = Vec::new(); + filter_command( + link.clone(), + command.name.clone(), + &mut priority_err, + &mut all_errs, + false, + ); - (priority_err.concat(), link) + (priority_err.concat(), all_errs.concat(), link) } else { bail!( "available distrubuted components:\n -fuel-core\n -fuel-core-client\n -fuel-indexer\n -forc\n -forc-client\n -forc-doc\n -forc-explore\n -forc-fmt\n -forc-index\n -forc-lsp\n -forc-tx\n -forc-wallet\n -sway-vim\n @@ -59,14 +75,27 @@ please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" if let Some(index) = priority_err.find(NIX_PKG_PRIORITY_MSG) { let (_, err) = priority_err.split_at(index); let iter = err.split_whitespace(); - auto_prioritize_installed_package(iter, 7, link)?; + success = auto_prioritize_installed_package(iter, 7, link)?; // nixos } else if let Some(index) = priority_err.find(NIXOS_PRIORITY_MSG) { let (_, err) = priority_err.split_at(index); let iter = err.split_whitespace(); - auto_prioritize_installed_package(iter, 4, link)?; + success = auto_prioritize_installed_package(iter, 4, link)?; } } + if success || all_errs.is_empty() { + if command.is_toolchain() { + info!( + "successfully installed {:?} toolchain", + command.get_toolchain()? + ); + } else { + let (component, toolchain) = command.get_component()?; + info!("successfully installed {toolchain:?} component {component:?}"); + } + } else { + info!("{all_errs:?}"); + } Ok(()) } @@ -76,6 +105,7 @@ fn filter_command( link_clone: String, command_name: String, priority_err: &mut Vec, + all_errs: &mut Vec, is_toolchain_cmd: bool, ) { let (tx, rx) = mpsc::channel(); @@ -95,24 +125,36 @@ fn filter_command( let handle = thread::spawn(move || { while let Some(stderr) = child.stderr.take() { let reader = BufReader::new(stderr); - + let mut is_error = false; for line in reader.lines().flatten() { - if line.contains(NIXOS_PRIORITY_MSG) || line.contains(NIX_PKG_PRIORITY_MSG) { - tx.send((None, Some(line))).unwrap(); + if line.contains("error:") || is_error { + is_error = true; + if line.contains(NIXOS_PRIORITY_MSG) || line.contains(NIX_PKG_PRIORITY_MSG) + { + // send to priority err and all err + tx.send((None, Some(line.clone()), Some(line))).unwrap(); + } else { + // send to all err + // useful if the build fails for reasons other than priority + tx.send((None, None, Some(line))).unwrap(); + } } else { - tx.send((Some(line), None)).unwrap(); + tx.send((Some(line), None, None)).unwrap(); } } } }); - while let Ok((line, err)) = rx.recv() { - if let Some(line) = line { - info!("{line}"); + while let Ok((line_opt, priority_err_opt, all_errs_opt)) = rx.recv() { + if let Some(line) = line_opt { + info!("hi: {line}"); } - if let Some(err) = err { + if let Some(err) = priority_err_opt { priority_err.push(err); } + if let Some(err) = all_errs_opt { + all_errs.push(err); + } } handle.join().unwrap(); } @@ -126,7 +168,7 @@ fn auto_prioritize_installed_package( mut iter: SplitWhitespace, msg_len: usize, link: String, -) -> Result<()> { +) -> Result { for _ in 0..msg_len { iter.next(); } @@ -140,7 +182,8 @@ fn auto_prioritize_installed_package( try_prioritize(current_pkg_priority, link)? } } - Ok(()) + + Ok(true) } /// `nix profile install --priority` can be negative, so here we just continue to try @@ -164,5 +207,6 @@ fn try_prioritize(mut pkg_priority: i32, link: String) -> Result<()> { try_prioritize(pkg_priority, link)? } } + Ok(()) } diff --git a/src/commands/nix/link_generator.rs b/src/commands/nix/link_generator.rs index 9d4f7ae8b..138eb85d7 100644 --- a/src/commands/nix/link_generator.rs +++ b/src/commands/nix/link_generator.rs @@ -100,6 +100,8 @@ impl FuelToolchain { *self == FuelToolchain::Latest } } + +#[derive(Debug)] pub(crate) enum FuelComponent { FuelCore, FuelCoreClient, diff --git a/src/commands/nix/list.rs b/src/commands/nix/list.rs index 7bd2a7977..af07d7fa5 100644 --- a/src/commands/nix/list.rs +++ b/src/commands/nix/list.rs @@ -2,16 +2,90 @@ use super::{NIX_CMD, PROFILE_LIST_ARGS}; use crate::commands::nix::nix_info; use anyhow::{bail, Result}; use clap::Parser; -use std::process::Command; +use std::{collections::HashMap, process::Command}; use tracing::info; #[derive(Debug, Parser)] pub struct NixListCommand; +#[derive(Debug)] +pub(crate) struct NixBinaryInfo { + pub(crate) name: String, + pub(crate) index: u32, + pub(crate) unlocked_attr_path: String, + pub(crate) locked_attr_path: String, + pub(crate) nix_store_path: String, +} +impl NixBinaryInfo { + fn new( + name: &str, + index: &str, + unlocked_attr_path: &str, + locked_attr_path: &str, + nix_store_path: &str, + ) -> Self { + Self { + name: name.into(), + index: index.parse::().unwrap(), + unlocked_attr_path: unlocked_attr_path.into(), + locked_attr_path: unlocked_attr_path.into(), + nix_store_path: nix_store_path.into(), + } + } +} + +#[derive(Debug)] +pub(crate) struct NixBinaryList(pub(crate) HashMap>); + +impl From> for NixBinaryList { + fn from(v: Vec) -> Self { + let mut map: HashMap> = HashMap::new(); + let stdout = String::from_utf8_lossy(&v); + let mut stdout_iter = stdout.split_whitespace(); + let mut count = stdout_iter.clone().count(); + let mut outer_vec = Vec::new(); + while count != 0 { + let mut inner_vec = Vec::new(); + for _ in 0..4 { + if let Some(val) = stdout_iter.next() { + inner_vec.push(val); + } + } + outer_vec.push(inner_vec); + count -= 4; + } + for inner_vec in outer_vec.iter() { + let unlocked_attr_path = inner_vec[1]; + if let Some(index) = unlocked_attr_path.find(".fuel") { + let (_, name) = unlocked_attr_path.split_at(index); + let nix_bin = NixBinaryInfo::new( + name, + inner_vec[0], + unlocked_attr_path, + inner_vec[2], + inner_vec[3], + ); + if let Some(index) = name.find('-') { + let (_, toolchain) = name.clone().split_at(index); + match map.get_mut(toolchain) { + Some(nix_bins) => nix_bins.push(nix_bin), + None => { + map.insert(toolchain.into(), vec![nix_bin]); + } + } + } + } + } + Self(map) + } +} + pub fn nix_list(_command: NixListCommand) -> Result<()> { match Command::new(NIX_CMD).args(PROFILE_LIST_ARGS).output() { Ok(output) => { - nix_info!(output); + let nix_bin_list = NixBinaryList::from(output.stdout); + println!("{nix_bin_list:?}"); + // nix_info!(output); Ok(()) } Err(err) => bail!("failed to show installed binaries for profile: {err}"), diff --git a/src/commands/nix/mod.rs b/src/commands/nix/mod.rs index 867f72675..584cbd9db 100644 --- a/src/commands/nix/mod.rs +++ b/src/commands/nix/mod.rs @@ -65,6 +65,9 @@ pub fn exec(command: NixCommand) -> Result<()> { NixCommand::Install(command) => nix_install(command), NixCommand::Remove(command) => nix_remove(command), NixCommand::Upgrade(command) => nix_upgrade(command), - NixCommand::List(_command) => nix_list(_command), + NixCommand::List(_command) => { + nix_list(_command)?; + Ok(()) + } } } From 854278b17d09747d070d4ce2746947bf38c145b7 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Mon, 21 Aug 2023 16:00:49 -0500 Subject: [PATCH 63/69] more wip list --- src/commands/nix/install.rs | 2 +- src/commands/nix/link_generator.rs | 54 ++++++++++++++++++++++++++---- src/commands/nix/list.rs | 4 ++- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 26ef170e1..ee3c4dacd 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -1,5 +1,5 @@ use crate::commands::nix::{ - link_generator::{NeedsNix, NixName}, + link_generator::{CachixLinkGenerator, FlakeLinkInfo}, NIX_CMD, PRIORITY_FLAG, PROFILE_INSTALL_ARGS, }; use anyhow::{anyhow, bail, Result}; diff --git a/src/commands/nix/link_generator.rs b/src/commands/nix/link_generator.rs index 138eb85d7..0b4c86545 100644 --- a/src/commands/nix/link_generator.rs +++ b/src/commands/nix/link_generator.rs @@ -1,9 +1,11 @@ -use super::{install::NixInstallCommand, FUEL_NIX_LINK}; +use crate::commands::fuelup::FuelupCommand; + +use super::{install::NixInstallCommand, list::UnlockedAttributePath, FUEL_NIX_LINK}; use anyhow::{bail, Result}; /// Handles getting toolchain or component information to translate /// into fuel.nix flake links and info presented to the user. -pub(crate) trait NeedsNix { +pub(crate) trait FlakeLinkInfo { /// the name of a toolchain or component fn name(&self) -> &str; fn get_toolchain(&self) -> Result { @@ -20,7 +22,7 @@ pub(crate) trait NeedsNix { } } /// Create toolchain and component links for the fuel.nix flake. -pub(crate) trait NixName: NeedsNix { +pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { fn nix_toolchain_suffix(&self) -> Result<&str> { Ok(match self.get_toolchain()? { FuelToolchain::Latest => "fuel", @@ -55,6 +57,7 @@ pub(crate) trait NixName: NeedsNix { FuelToolchain::Beta2 => "-beta-2", FuelToolchain::Beta3 => "-beta-3", FuelToolchain::Beta4rc => "-beta-4-rc", + FuelToolchain::Unknown => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") }; Ok((comp, tool)) } @@ -67,12 +70,24 @@ pub(crate) trait NixName: NeedsNix { } } -impl NeedsNix for NixInstallCommand { +impl FlakeLinkInfo for NixInstallCommand { fn name(&self) -> &str { self.name.as_str() } } -impl NixName for NixInstallCommand {} +impl FlakeLinkInfo for UnlockedAttributePath { + fn name(&self) -> &str { + let Ok((comp, toolchain)) = split_at_toolchain(self.0.clone()) else { + + }; + if comp == ".fuel" { + toolchain.into() + } else { + &comp + } + } +} +impl CachixLinkGenerator for NixInstallCommand {} #[derive(Eq, PartialEq, Debug)] pub(crate) enum FuelToolchain { @@ -82,6 +97,7 @@ pub(crate) enum FuelToolchain { Beta2, Beta3, Beta4rc, + Unknown, } impl FuelToolchain { @@ -100,6 +116,32 @@ impl FuelToolchain { *self == FuelToolchain::Latest } } +impl<'a> From<&'a str> for FuelToolchain { + fn from(s: &'a str) -> Self { + match s.to_lowercase().as_str() { + "latest" => Self::Latest, + "nightly" => Self::Nightly, + "beta-1" | "beta1" => Self::Beta1, + "beta-2" | "beta2" => Self::Beta2, + "beta-3" | "beta3" => Self::Beta3, + "beta-4-rc" | "beta-4rc" | "beta4rc" => Self::Beta4rc, + _ => Self::Unknown, + } + } +} +impl From for &str { + fn from(ft: FuelToolchain) -> &'static str { + match ft { + FuelToolchain::Latest => "latest", + FuelToolchain::Nightly => "nightly", + FuelToolchain::Beta1 => "beta-1", + FuelToolchain::Beta2 => "beta-2", + FuelToolchain::Beta3 => "beta-3", + FuelToolchain::Beta4rc => "beta-4-rc", + FuelToolchain::Unknown => "unknown", + } + } +} #[derive(Debug)] pub(crate) enum FuelComponent { @@ -151,7 +193,7 @@ please form a valid component, like so: fuel-core-beta-3" }, tool)) } } -fn split_at_toolchain(s: String) -> Result<(String, FuelToolchain)> { +pub(crate) fn split_at_toolchain(s: String) -> Result<(String, FuelToolchain)> { let (comp, tool) = if let Some(index) = s.find("beta") { let (comp, tool) = s.split_at(index); (comp.into(), FuelToolchain::from_str(tool)?) diff --git a/src/commands/nix/list.rs b/src/commands/nix/list.rs index af07d7fa5..e0e614ef6 100644 --- a/src/commands/nix/list.rs +++ b/src/commands/nix/list.rs @@ -1,4 +1,4 @@ -use super::{NIX_CMD, PROFILE_LIST_ARGS}; +use super::{link_generator::FlakeLinkInfo, NIX_CMD, PROFILE_LIST_ARGS}; use crate::commands::nix::nix_info; use anyhow::{bail, Result}; use clap::Parser; @@ -34,6 +34,8 @@ impl NixBinaryInfo { } } +pub(crate) struct UnlockedAttributePath(pub(crate) String); + #[derive(Debug)] pub(crate) struct NixBinaryList(pub(crate) HashMap>); From abdfa24f49c4457fde5c81c3679f2fc9afc371cb Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Tue, 22 Aug 2023 23:09:39 -0500 Subject: [PATCH 64/69] list command debug internals are more concise --- src/commands/nix/link_generator.rs | 64 +++++++++------- src/commands/nix/list.rs | 113 +++++++++++++++++++++++------ 2 files changed, 127 insertions(+), 50 deletions(-) diff --git a/src/commands/nix/link_generator.rs b/src/commands/nix/link_generator.rs index 0b4c86545..616823bab 100644 --- a/src/commands/nix/link_generator.rs +++ b/src/commands/nix/link_generator.rs @@ -1,13 +1,13 @@ use crate::commands::fuelup::FuelupCommand; -use super::{install::NixInstallCommand, list::UnlockedAttributePath, FUEL_NIX_LINK}; +use super::{install::NixInstallCommand, list::UnlockedFlakeURL, FUEL_NIX_LINK}; use anyhow::{bail, Result}; /// Handles getting toolchain or component information to translate /// into fuel.nix flake links and info presented to the user. pub(crate) trait FlakeLinkInfo { /// the name of a toolchain or component - fn name(&self) -> &str; + fn name(&self) -> String; fn get_toolchain(&self) -> Result { FuelToolchain::from_str(self.name()) } @@ -31,6 +31,7 @@ pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { FuelToolchain::Beta2 => "fuel-beta-2", FuelToolchain::Beta3 => "fuel-beta-3", FuelToolchain::Beta4rc => "fuel-beta-4-rc", + FuelToolchain::Unknown => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") }) } fn nix_component_suffix(&self) -> Result<(&str, &str)> { @@ -57,7 +58,7 @@ pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { FuelToolchain::Beta2 => "-beta-2", FuelToolchain::Beta3 => "-beta-3", FuelToolchain::Beta4rc => "-beta-4-rc", - FuelToolchain::Unknown => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") + FuelToolchain::Unknown => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") }; Ok((comp, tool)) } @@ -71,25 +72,36 @@ pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { } impl FlakeLinkInfo for NixInstallCommand { - fn name(&self) -> &str { - self.name.as_str() + fn name(&self) -> String { + self.name.clone() } } -impl FlakeLinkInfo for UnlockedAttributePath { - fn name(&self) -> &str { - let Ok((comp, toolchain)) = split_at_toolchain(self.0.clone()) else { - - }; - if comp == ".fuel" { - toolchain.into() +impl FlakeLinkInfo for UnlockedFlakeURL { + fn name(&self) -> String { + let (comp, _) = split_at_toolchain(self.0.clone()) + .expect("failed to split whitespace of unlocked attribute path"); + if let Some(index) = comp.find(".fuel") { + let (_, comp) = comp.split_at(index); + if comp == ".fuel-" { + // return the full toolchain name + let comp = comp.replace('.', ""); + let comp = comp.replace('-', ""); + comp.to_string() + } else if comp == ".fuel" { + let comp = comp.replace('.', ""); + comp.to_string() + } else { + self.0.clone() + } } else { - &comp + self.0.clone() } } } impl CachixLinkGenerator for NixInstallCommand {} +impl CachixLinkGenerator for UnlockedFlakeURL {} -#[derive(Eq, PartialEq, Debug)] +#[derive(Eq, PartialEq, Debug, Hash)] pub(crate) enum FuelToolchain { Latest, Nightly, @@ -101,14 +113,14 @@ pub(crate) enum FuelToolchain { } impl FuelToolchain { - fn from_str(s: &str) -> Result { + fn from_str(s: String) -> Result { Ok(match s.to_lowercase().as_str() { - "latest" => Self::Latest, - "nightly" => Self::Nightly, - "beta-1" | "beta1" => Self::Beta1, - "beta-2" | "beta2" => Self::Beta2, - "beta-3" | "beta3" => Self::Beta3, - "beta-4-rc" | "beta-4rc" | "beta4rc" => Self::Beta4rc, + "latest" | "fuel" => Self::Latest, + "nightly" | "fuel-nightly" => Self::Nightly, + "beta-1" | "beta1" | "fuel-beta-1" => Self::Beta1, + "beta-2" | "beta2" | "fuel-beta-2" => Self::Beta2, + "beta-3" | "beta3" | "fuel-beta-3" => Self::Beta3, + "beta-4-rc" | "beta-4rc" | "beta4rc" | "fuel-beta-4-rc" => Self::Beta4rc, _ => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") }) } @@ -116,8 +128,8 @@ impl FuelToolchain { *self == FuelToolchain::Latest } } -impl<'a> From<&'a str> for FuelToolchain { - fn from(s: &'a str) -> Self { +impl From for FuelToolchain { + fn from(s: String) -> Self { match s.to_lowercase().as_str() { "latest" => Self::Latest, "nightly" => Self::Nightly, @@ -160,7 +172,7 @@ pub(crate) enum FuelComponent { SwayVim, } impl FuelComponent { - fn from_str(s: &str) -> Result<(Self, FuelToolchain)> { + fn from_str(s: String) -> Result<(Self, FuelToolchain)> { let (comp, tool) = split_at_toolchain(s.to_lowercase())?; // remove the excess '-' between the comp and toolchain vers let comp = if !tool.is_latest() { @@ -196,10 +208,10 @@ please form a valid component, like so: fuel-core-beta-3" pub(crate) fn split_at_toolchain(s: String) -> Result<(String, FuelToolchain)> { let (comp, tool) = if let Some(index) = s.find("beta") { let (comp, tool) = s.split_at(index); - (comp.into(), FuelToolchain::from_str(tool)?) + (comp.into(), FuelToolchain::from_str(tool.to_string())?) } else if let Some(index) = s.find("nightly") { let (comp, tool) = s.split_at(index); - (comp.into(), FuelToolchain::from_str(tool)?) + (comp.into(), FuelToolchain::from_str(tool.to_string())?) } else { (s, FuelToolchain::Latest) }; diff --git a/src/commands/nix/list.rs b/src/commands/nix/list.rs index e0e614ef6..b76003697 100644 --- a/src/commands/nix/list.rs +++ b/src/commands/nix/list.rs @@ -1,4 +1,7 @@ -use super::{link_generator::FlakeLinkInfo, NIX_CMD, PROFILE_LIST_ARGS}; +use super::{ + link_generator::{split_at_toolchain, FlakeLinkInfo, FuelToolchain}, + NIX_CMD, PROFILE_LIST_ARGS, +}; use crate::commands::nix::nix_info; use anyhow::{bail, Result}; use clap::Parser; @@ -8,40 +11,82 @@ use tracing::info; #[derive(Debug, Parser)] pub struct NixListCommand; +/// A binary package installed by the fuel.nix flake. #[derive(Debug)] pub(crate) struct NixBinaryInfo { pub(crate) name: String, pub(crate) index: u32, - pub(crate) unlocked_attr_path: String, - pub(crate) locked_attr_path: String, + pub(crate) flake_attribute: Option, + pub(crate) unlocked_flake_url: String, + pub(crate) locked_flake_url: String, pub(crate) nix_store_path: String, } impl NixBinaryInfo { fn new( - name: &str, + name: String, index: &str, - unlocked_attr_path: &str, - locked_attr_path: &str, + flake_attribute: Option, + unlocked_flake_url: &str, + locked_flake_url: &str, nix_store_path: &str, ) -> Self { Self { - name: name.into(), + name, index: index.parse::().unwrap(), - unlocked_attr_path: unlocked_attr_path.into(), - locked_attr_path: unlocked_attr_path.into(), + flake_attribute, + unlocked_flake_url: unlocked_flake_url.into(), + locked_flake_url: unlocked_flake_url.into(), nix_store_path: nix_store_path.into(), } } } -pub(crate) struct UnlockedAttributePath(pub(crate) String); +/// Used to get the information about a package since it has a +/// more reliable structure than either the flake attribute, which +/// isn't present on my machine (M1 macbook pro, nix (Nix) 2.15.0), +/// or the nix store path which contains a randomly produced hash. +pub(crate) struct UnlockedFlakeURL(pub(crate) String); +impl<'a> From<&'a str> for UnlockedFlakeURL { + fn from(s: &'a str) -> Self { + Self(s.to_string()) + } +} +impl UnlockedFlakeURL { + fn split_at_toolchain(&self) -> Result<(String, FuelToolchain)> { + if let Some(index) = self.0.find(".fuel") { + let (_, tool) = split_at_toolchain(self.0.split_at(index).1.split_at(1).1.to_string()) + .expect("failed to get toolchain from unlocked attribute path"); + Ok((self.name(), tool)) + } else { + bail!("could not get toolchain info from attribute path") + } + } + fn split_at_component(&self) -> Result<(String, FuelToolchain)> { + if let Some(index) = self.0.find(".forc") { + split_at_toolchain(self.0.split_at(index).1.split_at(1).1.to_string()) + } else { + bail!("could not get toolchain info from attribute path") + } + } +} #[derive(Debug)] -pub(crate) struct NixBinaryList(pub(crate) HashMap>); +pub(crate) struct NixBinaryList(pub(crate) HashMap>); +/// Currently this collects a static 4 values from the stdout string +/// produced by `nix profile list`, however, in some cases we may actually +/// get 5. The four present are the index, unlocked flake link, locked flake link +/// and nix store path. The fifth would come after the index and is the flake +/// attribute which for some reason doesn't show up presently but _could_ in the +/// future. +/// +/// To avoid breakage we could look to collect at every index, then to be sure +/// of each value we can perform checks to see what that data holds. eg, an index +/// can be parsed as an integer, the flake links will start with "github:fuellabs" and +/// the nix store path will start with "nix/store/". impl From> for NixBinaryList { fn from(v: Vec) -> Self { - let mut map: HashMap> = HashMap::new(); + let mut map: HashMap> = HashMap::new(); let stdout = String::from_utf8_lossy(&v); let mut stdout_iter = stdout.split_whitespace(); let mut count = stdout_iter.clone().count(); @@ -57,23 +102,43 @@ impl From> for NixBinaryList { count -= 4; } for inner_vec in outer_vec.iter() { - let unlocked_attr_path = inner_vec[1]; - if let Some(index) = unlocked_attr_path.find(".fuel") { - let (_, name) = unlocked_attr_path.split_at(index); + let unlocked_attr_path = UnlockedFlakeURL::from(inner_vec[1]); + if unlocked_attr_path.is_toolchain() { + let (name, toolchain) = unlocked_attr_path + .split_at_toolchain() + .expect("failed to get pkg info from unlocked attribute path"); let nix_bin = NixBinaryInfo::new( name, inner_vec[0], - unlocked_attr_path, + None, /* see comment on this impl */ + inner_vec[1], inner_vec[2], inner_vec[3], ); - if let Some(index) = name.find('-') { - let (_, toolchain) = name.clone().split_at(index); - match map.get_mut(toolchain) { - Some(nix_bins) => nix_bins.push(nix_bin), - None => { - map.insert(toolchain.into(), vec![nix_bin]); - } + + match map.get_mut(&toolchain) { + Some(nix_bins) => nix_bins.push(nix_bin), + None => { + map.insert(toolchain, vec![nix_bin]); + } + } + } else if unlocked_attr_path.is_component() { + let (name, toolchain) = unlocked_attr_path + .split_at_component() + .expect("failed to get pkg info from unlocked attribute path"); + let nix_bin = NixBinaryInfo::new( + name, + inner_vec[0], + None, /* see comment on this impl */ + inner_vec[1], + inner_vec[2], + inner_vec[3], + ); + + match map.get_mut(&toolchain) { + Some(nix_bins) => nix_bins.push(nix_bin), + None => { + map.insert(toolchain, vec![nix_bin]); } } } @@ -86,7 +151,7 @@ pub fn nix_list(_command: NixListCommand) -> Result<()> { match Command::new(NIX_CMD).args(PROFILE_LIST_ARGS).output() { Ok(output) => { let nix_bin_list = NixBinaryList::from(output.stdout); - println!("{nix_bin_list:?}"); + dbg!(nix_bin_list); // nix_info!(output); Ok(()) } From 7cdd51ed4dde9348231cab78100680a1b5f7e0c5 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Wed, 23 Aug 2023 23:54:46 -0500 Subject: [PATCH 65/69] update docs, adjust naming conventions, refactor comp err msg --- .vscode/settings.json | 3 + .../nix/{link_generator.rs => flake_utils.rs} | 124 +++++++++++++----- src/commands/nix/install.rs | 6 +- src/commands/nix/list.rs | 2 +- src/commands/nix/mod.rs | 2 +- 5 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 .vscode/settings.json rename src/commands/nix/{link_generator.rs => flake_utils.rs} (64%) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..b48948abd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.showUnlinkedFileNotification": false +} diff --git a/src/commands/nix/link_generator.rs b/src/commands/nix/flake_utils.rs similarity index 64% rename from src/commands/nix/link_generator.rs rename to src/commands/nix/flake_utils.rs index 616823bab..4d13ec976 100644 --- a/src/commands/nix/link_generator.rs +++ b/src/commands/nix/flake_utils.rs @@ -1,6 +1,9 @@ -use crate::commands::fuelup::FuelupCommand; +//! Utility for translating user commands into flake links, +//! and flake links into display messages or internal debug info +//! for handling toolchain or component management for the user automatically. use super::{install::NixInstallCommand, list::UnlockedFlakeURL, FUEL_NIX_LINK}; +use crate::commands::fuelup::FuelupCommand; use anyhow::{bail, Result}; /// Handles getting toolchain or component information to translate @@ -12,18 +15,18 @@ pub(crate) trait FlakeLinkInfo { FuelToolchain::from_str(self.name()) } fn get_component(&self) -> Result<(FuelComponent, FuelToolchain)> { - FuelComponent::from_str(self.name()) + FuelComponent::from_str_with_toolchain(self.name()) } fn is_toolchain(&self) -> bool { FuelToolchain::from_str(self.name()).is_ok() } fn is_component(&self) -> bool { - FuelComponent::from_str(self.name()).is_ok() + FuelComponent::from_str_with_toolchain(self.name()).is_ok() } } /// Create toolchain and component links for the fuel.nix flake. pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { - fn nix_toolchain_suffix(&self) -> Result<&str> { + fn flake_link_toolchain_suffix(&self) -> Result<&str> { Ok(match self.get_toolchain()? { FuelToolchain::Latest => "fuel", FuelToolchain::Nightly => "fuel-nightly", @@ -34,7 +37,7 @@ pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { FuelToolchain::Unknown => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") }) } - fn nix_component_suffix(&self) -> Result<(&str, &str)> { + fn flake_link_component_suffix(&self) -> Result<(&str, &str)> { let (comp, tool) = self.get_component()?; let comp = match comp { FuelComponent::FuelCore => "fuel-core", @@ -62,11 +65,14 @@ pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { }; Ok((comp, tool)) } - fn toolchain_link(&self) -> Result { - Ok(format!("{FUEL_NIX_LINK}#{}", self.nix_toolchain_suffix()?)) + fn flake_toolchain_link(&self) -> Result { + Ok(format!( + "{FUEL_NIX_LINK}#{}", + self.flake_link_toolchain_suffix()? + )) } - fn component_link(&self) -> Result { - let (comp, tool) = self.nix_component_suffix()?; + fn flake_component_link(&self) -> Result { + let (comp, tool) = self.flake_link_component_suffix()?; Ok(format!("{FUEL_NIX_LINK}#{}{}", comp, tool)) } } @@ -155,6 +161,26 @@ impl From for &str { } } +const DIST_COMPONENTS: &[FuelComponent; 13] = &[ + FuelComponent::FuelCore, + FuelComponent::FuelCoreClient, + FuelComponent::FuelIndexer, + FuelComponent::Forc, + FuelComponent::ForcClient, + FuelComponent::ForcDoc, + FuelComponent::ForcExplore, + FuelComponent::ForcFmt, + FuelComponent::ForcIndex, + FuelComponent::ForcLsp, + FuelComponent::ForcTx, + FuelComponent::ForcWallet, + FuelComponent::SwayVim, +]; + +// ... +// +// bail!("available distrubuted components: {err_str}\n") + #[derive(Debug)] pub(crate) enum FuelComponent { FuelCore, @@ -172,37 +198,67 @@ pub(crate) enum FuelComponent { SwayVim, } impl FuelComponent { - fn from_str(s: String) -> Result<(Self, FuelToolchain)> { - let (comp, tool) = split_at_toolchain(s.to_lowercase())?; + fn from_str_with_toolchain(s: String) -> Result<(Self, FuelToolchain)> { + let (raw_comp_str, tool) = split_at_toolchain(s.to_lowercase())?; // remove the excess '-' between the comp and toolchain vers - let comp = if !tool.is_latest() { - let mut comp = comp.chars(); - comp.next_back(); - comp.collect::() + let comp_str = if !tool.is_latest() { + let mut comp_str = raw_comp_str.chars(); + comp_str.next_back(); + comp_str.collect::() } else { - comp + raw_comp_str }; - Ok((match comp.as_str() { - "fuel-core" => Self::FuelCore, - "fuel-core-client" => Self::FuelCoreClient, - "fuel-indexer" => Self::FuelIndexer, - "forc" => Self::Forc, - "forc-client" => Self::ForcClient, - "forc-doc" => Self::ForcDoc, - "forc-explore" => Self::ForcExplore, - "forc-fmt" => Self::ForcFmt, - "forc-index" => Self::ForcIndex, - "forc-lsp" => Self::ForcLsp, - "forc-tx" => Self::ForcTx, - "forc-wallet" => Self::ForcWallet, - "sway-vim" => Self::SwayVim, - _ => bail!( - "available distrubuted components:\n -fuel-core\n -fuel-core-client\n -fuel-indexer\n -forc\n -forc-client\n -forc-doc\n -forc-explore\n -forc-fmt\n -forc-index\n -forc-lsp\n -forc-tx\n -forc-wallet\n -sway-vim\n + let comp = Self::from_str(comp_str)?; + Ok((comp, tool)) + } + + fn from_str(comp_str: String) -> Result { + match comp_str.as_str() { + "fuel-core" => Ok(Self::FuelCore), + "fuel-core-client" => Ok(Self::FuelCoreClient), + "fuel-indexer" => Ok(Self::FuelIndexer), + "forc" => Ok(Self::Forc), + "forc-client" => Ok(Self::ForcClient), + "forc-doc" => Ok(Self::ForcDoc), + "forc-explore" => Ok(Self::ForcExplore), + "forc-fmt" => Ok(Self::ForcFmt), + "forc-index" => Ok(Self::ForcIndex), + "forc-lsp" => Ok(Self::ForcLsp), + "forc-tx" => Ok(Self::ForcTx), + "forc-wallet" => Ok(Self::ForcWallet), + "sway-vim" => Ok(Self::SwayVim), + _ => { + let available_components = DIST_COMPONENTS + .iter() + .map(|comp| comp.as_display_str()) + .collect::>() + .join("\n"); + bail!("available distrubuted components:\n {available_components}\n + available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc please form a valid component, like so: fuel-core-beta-3" - ) - }, tool)) + ) + } + } + } + + fn as_display_str(&self) -> &'static str { + match self { + FuelComponent::FuelCore => "- fuel-core", + FuelComponent::FuelCoreClient => "- fuel-core-client", + FuelComponent::FuelIndexer => "- fuel-indexer", + FuelComponent::Forc => "- forc", + FuelComponent::ForcClient => "- forc-client", + FuelComponent::ForcDoc => "- forc-doc", + FuelComponent::ForcExplore => "- forc-explore", + FuelComponent::ForcFmt => "- forc-fmt", + FuelComponent::ForcIndex => "- forc-index", + FuelComponent::ForcLsp => "- forc-lsp", + FuelComponent::ForcTx => "- forc-tx", + FuelComponent::ForcWallet => "- forc-wallet", + FuelComponent::SwayVim => "- sway-vim", + } } } pub(crate) fn split_at_toolchain(s: String) -> Result<(String, FuelToolchain)> { diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index ee3c4dacd..63bf53896 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -1,5 +1,5 @@ use crate::commands::nix::{ - link_generator::{CachixLinkGenerator, FlakeLinkInfo}, + flake_utils::{CachixLinkGenerator, FlakeLinkInfo}, NIX_CMD, PRIORITY_FLAG, PROFILE_INSTALL_ARGS, }; use anyhow::{anyhow, bail, Result}; @@ -31,7 +31,7 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { "downloading and installing fuel {} toolchain, this may take a while...", command.name ); - let link = command.toolchain_link()?; + let link = command.flake_toolchain_link()?; let mut priority_err = Vec::new(); let mut all_errs = Vec::new(); filter_command( @@ -48,7 +48,7 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { "downloading and installing component {}, this may take a while...", command.name ); - let link = command.component_link()?; + let link = command.flake_component_link()?; let mut priority_err = Vec::new(); let mut all_errs = Vec::new(); filter_command( diff --git a/src/commands/nix/list.rs b/src/commands/nix/list.rs index b76003697..572bfadd3 100644 --- a/src/commands/nix/list.rs +++ b/src/commands/nix/list.rs @@ -1,5 +1,5 @@ use super::{ - link_generator::{split_at_toolchain, FlakeLinkInfo, FuelToolchain}, + flake_utils::{split_at_toolchain, FlakeLinkInfo, FuelToolchain}, NIX_CMD, PROFILE_LIST_ARGS, }; use crate::commands::nix::nix_info; diff --git a/src/commands/nix/mod.rs b/src/commands/nix/mod.rs index 584cbd9db..ef046a6c4 100644 --- a/src/commands/nix/mod.rs +++ b/src/commands/nix/mod.rs @@ -7,8 +7,8 @@ use self::{ use anyhow::Result; use clap::Parser; +mod flake_utils; mod install; -mod link_generator; mod list; mod remove; mod upgrade; From 7c520b49b2fa263ea2990b814b8a5c758d921dcc Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 24 Aug 2023 00:01:51 -0500 Subject: [PATCH 66/69] remove unused impl --- src/commands/nix/flake_utils.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/commands/nix/flake_utils.rs b/src/commands/nix/flake_utils.rs index 4d13ec976..511567385 100644 --- a/src/commands/nix/flake_utils.rs +++ b/src/commands/nix/flake_utils.rs @@ -105,7 +105,6 @@ impl FlakeLinkInfo for UnlockedFlakeURL { } } impl CachixLinkGenerator for NixInstallCommand {} -impl CachixLinkGenerator for UnlockedFlakeURL {} #[derive(Eq, PartialEq, Debug, Hash)] pub(crate) enum FuelToolchain { From 90f4966a0dc95f1bc9c6c460a7c3ab59b80ecfe4 Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 24 Aug 2023 00:02:33 -0500 Subject: [PATCH 67/69] clean up stray comments --- src/commands/nix/flake_utils.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/commands/nix/flake_utils.rs b/src/commands/nix/flake_utils.rs index 511567385..282263c16 100644 --- a/src/commands/nix/flake_utils.rs +++ b/src/commands/nix/flake_utils.rs @@ -176,10 +176,6 @@ const DIST_COMPONENTS: &[FuelComponent; 13] = &[ FuelComponent::SwayVim, ]; -// ... -// -// bail!("available distrubuted components: {err_str}\n") - #[derive(Debug)] pub(crate) enum FuelComponent { FuelCore, From b24e74e5d43d7866760ab29b2a6fef6d0ea33a6a Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 24 Aug 2023 00:16:33 -0500 Subject: [PATCH 68/69] update all err msgs --- src/commands/nix/flake_utils.rs | 57 ++++++++++++++++++++++++++++++--- src/commands/nix/install.rs | 15 +++++++-- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/commands/nix/flake_utils.rs b/src/commands/nix/flake_utils.rs index 282263c16..ac1119fb1 100644 --- a/src/commands/nix/flake_utils.rs +++ b/src/commands/nix/flake_utils.rs @@ -34,7 +34,14 @@ pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { FuelToolchain::Beta2 => "fuel-beta-2", FuelToolchain::Beta3 => "fuel-beta-3", FuelToolchain::Beta4rc => "fuel-beta-4-rc", - FuelToolchain::Unknown => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") + FuelToolchain::Unknown => { + let available_toolchains = DIST_TOOLCHAINS + .iter() + .map(|tc| tc.as_display_str()) + .collect::>() + .join("\n"); + bail!("available distributed toolchains:\n {available_toolchains}\n") + } }) } fn flake_link_component_suffix(&self) -> Result<(&str, &str)> { @@ -61,7 +68,14 @@ pub(crate) trait CachixLinkGenerator: FlakeLinkInfo { FuelToolchain::Beta2 => "-beta-2", FuelToolchain::Beta3 => "-beta-3", FuelToolchain::Beta4rc => "-beta-4-rc", - FuelToolchain::Unknown => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") + FuelToolchain::Unknown => { + let available_toolchains = DIST_TOOLCHAINS + .iter() + .map(|tc| tc.as_display_str()) + .collect::>() + .join("\n"); + bail!("available distributed toolchains:\n {available_toolchains}\n") + } }; Ok((comp, tool)) } @@ -106,6 +120,15 @@ impl FlakeLinkInfo for UnlockedFlakeURL { } impl CachixLinkGenerator for NixInstallCommand {} +const DIST_TOOLCHAINS: &[FuelToolchain; 6] = &[ + FuelToolchain::Latest, + FuelToolchain::Nightly, + FuelToolchain::Beta1, + FuelToolchain::Beta2, + FuelToolchain::Beta3, + FuelToolchain::Beta4rc, +]; + #[derive(Eq, PartialEq, Debug, Hash)] pub(crate) enum FuelToolchain { Latest, @@ -126,9 +149,27 @@ impl FuelToolchain { "beta-2" | "beta2" | "fuel-beta-2" => Self::Beta2, "beta-3" | "beta3" | "fuel-beta-3" => Self::Beta3, "beta-4-rc" | "beta-4rc" | "beta4rc" | "fuel-beta-4-rc" => Self::Beta4rc, - _ => bail!("available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc") + _ => { + let available_toolchains = DIST_TOOLCHAINS + .iter() + .map(|tc| tc.as_display_str()) + .collect::>() + .join("\n"); + bail!("available distributed toolchains:\n {available_toolchains}\n") + } }) } + fn as_display_str(&self) -> &'static str { + match self { + FuelToolchain::Latest => "- latest", + FuelToolchain::Nightly => "- nightly", + FuelToolchain::Beta1 => "- beta-1", + FuelToolchain::Beta2 => "- beta-2", + FuelToolchain::Beta3 => "- beta-3", + FuelToolchain::Beta4rc => "- beta-4-rc", + FuelToolchain::Unknown => "unknown", + } + } fn is_latest(&self) -> bool { *self == FuelToolchain::Latest } @@ -228,9 +269,15 @@ impl FuelComponent { .map(|comp| comp.as_display_str()) .collect::>() .join("\n"); - bail!("available distrubuted components:\n {available_components}\n + let available_toolchains = DIST_TOOLCHAINS + .iter() + .map(|tc| tc.as_display_str()) + .collect::>() + .join("\n"); + bail!( + "available distrubuted components:\n {available_components} -available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc +available distributed toolchains:\n {available_toolchains} please form a valid component, like so: fuel-core-beta-3" ) diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 63bf53896..550ae9901 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -61,9 +61,20 @@ pub fn nix_install(command: NixInstallCommand) -> Result<()> { (priority_err.concat(), all_errs.concat(), link) } else { + let available_components = DIST_COMPONENTS + .iter() + .map(|comp| comp.as_display_str()) + .collect::>() + .join("\n"); + let available_toolchains = DIST_TOOLCHAINS + .iter() + .map(|tc| tc.as_display_str()) + .collect::>() + .join("\n"); bail!( - "available distrubuted components:\n -fuel-core\n -fuel-core-client\n -fuel-indexer\n -forc\n -forc-client\n -forc-doc\n -forc-explore\n -forc-fmt\n -forc-index\n -forc-lsp\n -forc-tx\n -forc-wallet\n -sway-vim\n -available distributed toolchains:\n -latest\n -nightly\n -beta-1\n -beta-2\n -beta-3\n -beta-4-rc + "available distrubuted components:\n {available_components} + +available distributed toolchains:\n {available_toolchains} please form a valid component or toolchain, like so: fuel-core-beta-3 or beta-3" ) From 9a92823d49cf9a28d4e50bdf0df54ec44347b19e Mon Sep 17 00:00:00 2001 From: Chris O'Brien Date: Thu, 24 Aug 2023 00:18:31 -0500 Subject: [PATCH 69/69] make utils available to crate --- src/commands/nix/flake_utils.rs | 8 ++++---- src/commands/nix/install.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/commands/nix/flake_utils.rs b/src/commands/nix/flake_utils.rs index ac1119fb1..d02efef78 100644 --- a/src/commands/nix/flake_utils.rs +++ b/src/commands/nix/flake_utils.rs @@ -120,7 +120,7 @@ impl FlakeLinkInfo for UnlockedFlakeURL { } impl CachixLinkGenerator for NixInstallCommand {} -const DIST_TOOLCHAINS: &[FuelToolchain; 6] = &[ +pub(crate) const DIST_TOOLCHAINS: &[FuelToolchain; 6] = &[ FuelToolchain::Latest, FuelToolchain::Nightly, FuelToolchain::Beta1, @@ -159,7 +159,7 @@ impl FuelToolchain { } }) } - fn as_display_str(&self) -> &'static str { + pub(crate) fn as_display_str(&self) -> &'static str { match self { FuelToolchain::Latest => "- latest", FuelToolchain::Nightly => "- nightly", @@ -201,7 +201,7 @@ impl From for &str { } } -const DIST_COMPONENTS: &[FuelComponent; 13] = &[ +pub(crate) const DIST_COMPONENTS: &[FuelComponent; 13] = &[ FuelComponent::FuelCore, FuelComponent::FuelCoreClient, FuelComponent::FuelIndexer, @@ -285,7 +285,7 @@ please form a valid component, like so: fuel-core-beta-3" } } - fn as_display_str(&self) -> &'static str { + pub(crate) fn as_display_str(&self) -> &'static str { match self { FuelComponent::FuelCore => "- fuel-core", FuelComponent::FuelCoreClient => "- fuel-core-client", diff --git a/src/commands/nix/install.rs b/src/commands/nix/install.rs index 550ae9901..ceef2ba22 100644 --- a/src/commands/nix/install.rs +++ b/src/commands/nix/install.rs @@ -1,5 +1,5 @@ use crate::commands::nix::{ - flake_utils::{CachixLinkGenerator, FlakeLinkInfo}, + flake_utils::{CachixLinkGenerator, FlakeLinkInfo, DIST_COMPONENTS, DIST_TOOLCHAINS}, NIX_CMD, PRIORITY_FLAG, PROFILE_INSTALL_ARGS, }; use anyhow::{anyhow, bail, Result};