Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(build): #1002 args manage ports fix #1120

Merged
merged 1 commit into from
Jul 13, 2023
Merged

Conversation

este6an13
Copy link
Contributor

  • default import of shellCommands builtin in makeTemplate

@dsalaza4
Copy link
Contributor

dsalaza4 commented Jul 12, 2023

m . /lintGitCommitMsg failing. Please read https://makes.fluidattacks.com/contributing/

@dsalaza4
Copy link
Contributor

dsalaza4 commented Jul 12, 2023

@este6an13,

I just realized this is not a valid change 😢, here is why:

Both makeDerivation and makeEntrypoint receive a sh script and execute it, this is why sourcing __shellCommands__ by default makes sense for them.

makeTemplate is different. It receives any text file and simply replaces placeholders declared within it with __argName__ provided values. Here is an example of makeTemplate using a simple txt file:

makeTemplate {
replace = {
__argFirst__ = "aaaaaaaaa";
__argSecond__ = "bbbb";
__argThird__ = testString;
};
name = "test-make-template";
template = ./template.txt;
}

This basically means that we cannot source __shellCommands__ by default for makeTemplate.

After running some tests on this change I realized that the resulting templated script still cannot find the Makes' info, warn and error functions. Here's what I did:

{makeTemplate, ...}:
makeTemplate {
  replace = {
    __argTest__ = "This is a test";
  };
  name = "test-make-template-2";
  template = "error __argTest__";
}

The resulting template file was simply

error This is a test

When executing this with a shell interpreter like bash, It could not find error as it did not source it in the first place.

$ bash /nix/store/b2dk4p5gyarrgwd5gqd91vygi1dbnbxg-make-template-for-test-make-template-2/template
/nix/store/b2dk4p5gyarrgwd5gqd91vygi1dbnbxg-make-template-for-test-make-template-2/template: line 1: error: command not found

The only feasible solution I see here if we want to keep the info, warn and error functions is directly sourcing them in managePorts by:

adding __shellCommands__ as a replace argument here:

makeTemplate {
name = "manage-ports";
searchPaths = {
bin = [
__nixpkgs__.lsof
__nixpkgs__.netcat
];
};
template = ./template.sh;
}

and sourcing it here, right before declaring the functions:

# shellcheck shell=bash
function _kill_port {

@dsalaza4
Copy link
Contributor

BTW, let's keep this PR open for easier traceability, you can simply amend your commit and push again, that will update the PR.

@este6an13
Copy link
Contributor Author

Hello @dsalaza4, I made a few changes following your recommendations.

I'm passing __shellCommands__ as a replace argument in managePorts. Then I'm sourcing it in manage-ports/template.sh.

I tried to test this by modifying the files in makes/makes/tests/makeTemplate folder, and it worked for the default template.txt file just by using this new __argShellCommands__ argument there. However when I tried to do this for a new file, I got the following error:

image

I'm just showing the end of the backtrace. It says that it cannot find my my-template.sh file. I executed m . /tests/makeTemplate from the root makes folder.

Here's how I modified the main.nix file:

{
  fromYaml,
  makeTemplate,
  __shellCommands__,
  ...
}: let
  testFile = fromYaml (
    builtins.readFile ./test.yaml
  );
  testString = testFile.testTitle;
in
  makeTemplate {
    replace = {
      __argShellCommands__ = __shellCommands__;
    };
    name = "my-template";
    template = ./my-template.sh;
  }

And this is the new my-template.sh file I created:

source __argShellCommands__

error "Hello"

Could you help me understanding why this happens?

I'm looking forward to your comments. Thank you!

@dsalaza4
Copy link
Contributor

Hello @dsalaza4, I made a few changes following your recommendations.

I'm passing __shellCommands__ as a replace argument in managePorts. Then I'm sourcing it in manage-ports/template.sh.

I tried to test this by modifying the files in makes/makes/tests/makeTemplate folder, and it worked for the default template.txt file just by using this new __argShellCommands__ argument there. However when I tried to do this for a new file, I got the following error:

image I'm just showing the end of the backtrace. It says that it cannot find my `my-template.sh` file. I executed `m . /tests/makeTemplate` from the root `makes` folder.

Here's how I modified the main.nix file:

{
  fromYaml,
  makeTemplate,
  __shellCommands__,
  ...
}: let
  testFile = fromYaml (
    builtins.readFile ./test.yaml
  );
  testString = testFile.testTitle;
in
  makeTemplate {
    replace = {
      __argShellCommands__ = __shellCommands__;
    };
    name = "my-template";
    template = ./my-template.sh;
  }

And this is the new my-template.sh file I created:

source __argShellCommands__

error "Hello"

Could you help me understanding why this happens?

I'm looking forward to your comments. Thank you!

I think this is because you did not git add the new files? Read this https://makes.fluidattacks.com/contributing/#testing-your-local-changes

@dsalaza4
Copy link
Contributor

Hello @dsalaza4, I made a few changes following your recommendations.
I'm passing __shellCommands__ as a replace argument in managePorts. Then I'm sourcing it in manage-ports/template.sh.
I tried to test this by modifying the files in makes/makes/tests/makeTemplate folder, and it worked for the default template.txt file just by using this new __argShellCommands__ argument there. However when I tried to do this for a new file, I got the following error:
image
I'm just showing the end of the backtrace. It says that it cannot find my my-template.sh file. I executed m . /tests/makeTemplate from the root makes folder.
Here's how I modified the main.nix file:

{
  fromYaml,
  makeTemplate,
  __shellCommands__,
  ...
}: let
  testFile = fromYaml (
    builtins.readFile ./test.yaml
  );
  testString = testFile.testTitle;
in
  makeTemplate {
    replace = {
      __argShellCommands__ = __shellCommands__;
    };
    name = "my-template";
    template = ./my-template.sh;
  }

And this is the new my-template.sh file I created:

source __argShellCommands__

error "Hello"

Could you help me understanding why this happens?
I'm looking forward to your comments. Thank you!

I think this is because you did not git add the new files? Read this https://makes.fluidattacks.com/contributing/#testing-your-local-changes

I just ran the same test and it worked well for me!

@dsalaza4
Copy link
Contributor

dsalaza4 commented Jul 12, 2023

Please add missing mailmap entry for noreply https://github.com/fluidattacks/makes/actions/runs/5537342039/jobs/10106243030?pr=1120

- sourcing `shellCommands` into `managePorts` builtin

Signed-off-by: Diego Quintero <diquintero@unal.edu.co>
@sonarcloud
Copy link

sonarcloud bot commented Jul 13, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Contributor

@dsalaza4 dsalaza4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dsalaza4 dsalaza4 merged commit c8411a4 into fluidattacks:main Jul 13, 2023
38 of 76 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants