-2

I have a command where $string_or_null_value is either a string or null. If null, the user is prompted an input box to enter its own value due to ${input:store_id}. The issue is that the input box is always prompted even when it's not required. I've been through the Input Variables doc and tasks code but I have a hard time figuring this out. What's the best approach?

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "my-task",
      "type": "shell",
      "command": "id=$($string_or_null_value || echo \"${input:store_id}\"); my_command=$id"
    }
  ],
  "inputs": [
    {
      "id": "store_id",
      "description": "Store id",
      "type": "promptString"
    }     
  ]
}
oguz ismail
  • 1
  • 16
  • 47
  • 69
Max
  • 169
  • 2
  • 11
  • Just out of curiosity, what is `$string_or_null_value`? Is it a [VS Code variable](https://code.visualstudio.com/docs/editor/variables-reference)? Is it an environment variable? Is it an environment variable that already exists in the environment of the VS Code process? Or only in your shell's environment? – starball Apr 02 '23 at 23:12
  • And why not just use your shell's input command? (Ex. `read` for Bash). And what is this task supposed to do? Are you trying to set an environment variable in an existing integrated terminal session? And what's the point of that- given that a task will always be given its own shell session? – starball Apr 02 '23 at 23:17
  • your link in "tasks code" is not linking to source code- it's linking to user docs. Did you mean that you read the source code? (if so, which files), or did you mean that you read user docs and its _example_ code? – starball Apr 02 '23 at 23:19
  • @user it's not a variable, it returns either a string or it fails to return anything. It fetches a value in my settings.json file `$(jq -r '.store' .vscode/settings.json)`. The task run a command, taking as an argument `$id`. Is this helping to answer the question and is the question clear? – Max Apr 02 '23 at 23:42
  • if you're going to use shell-specific syntax, again, why not go with a shell script that uses the shell's input prompting command and conditional logic syntax? – starball Apr 03 '23 at 00:26
  • @user I'm a little new to this so I don't get all these questions. I just need a simple `promptString` input box when the tasks runs, unless the first condition of the command is true, nothing more really. Can this be done? – Max Apr 03 '23 at 00:53
  • I think not. I don't think VS Code would bother to understand a specific shell's syntax unless a lot of people cared for it to. Again, what's your end goal here? I'm still confused what this task of yours is supposed to do. – starball Apr 03 '23 at 01:27
  • I simplified the command in the example. In reality this is the command I want to use `"command": "store_id=$(jq -r '.store' .vscode/settings.json || echo \"${input:store_id}\"); shopify theme dev --store=$store_id"` where `shopify` is a node module doing its own thing. I need the variable `$stored_id` to be either used from my `settings.json` file, or if non-existant from user input with `promptString`. The input box should not show if the file exist. Is this a bad question to ask? Maybe simple but I spent hours on this being new to bash. – Max Apr 03 '23 at 09:31
  • I'm pretty sure you cannot create variables in tasks.json to be used in settings.json. – starball Apr 03 '23 at 16:43

1 Answers1

1

${input:store_id} will be evaluated before the bash script, so you will always be prompted for an input by VsCode. You can use read instead. Try this

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "my-task",
          "type": "shell",
          // Prompt for user input
          "command": "id=$(echo null); [ \"$id\" = \"null\" ] && read -p 'Enter id: ' id; my_command=$id"
          // Your command returns a value
          "command": "id=$(echo somevalue); [ \"$id\" = \"null\" ] && read -p 'Enter id: ' id; my_command=$id" 
        }
      ]
    }
Yoann
  • 112
  • 6