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

ShellCheck does not recommend quote variables #2995

Closed
4 tasks done
dalisoft opened this issue Jun 5, 2024 · 5 comments
Closed
4 tasks done

ShellCheck does not recommend quote variables #2995

dalisoft opened this issue Jun 5, 2024 · 5 comments

Comments

@dalisoft
Copy link

dalisoft commented Jun 5, 2024

For bugs

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/bin/sh
set -eu

# Docker buildx script was copied from
# https://docs.docker.com/build/cloud/ci
# and modified by @dalisoft for AMD64/ARM64 platforms
ARCH=$(uname -m | sed 's/aarch64/arm64/' | sed 's/x86_64/amd64/')
OS=$(uname -s | tr '[:upper:]' '[:lower:]')

prepare() {
  BUILDX_URL=$(curl -s https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-lab-releases.json | grep "${OS}-${ARCH}\",$" | head -1 | xargs | tr -d ',' | xargs)
  # Download docker buildx with Hyrdobuild support
  mkdir -vp ~/.docker/cli-plugins/
  curl --silent -L --output ~/.docker/cli-plugins/docker-buildx "${BUILDX_URL}"
  chmod a+x ~/.docker/cli-plugins/docker-buildx

  echo "${DOCKER_HUB_PAT-}" | docker login --username "${DOCKER_HUB_USERNAME-}" --password-stdin
}

cleanup() {
  rm -rf ~/.docker/cli-plugins/

  docker logout
}

release() {
  # Build and publish a `Docker` tag
  if [ -n "${DOCKER_HUB_USERNAME-}" ] && [ -n "${DOCKER_HUB_PAT-}" ]; then

    log "Building and publishing Docker image..."
    log_verbose "Docker tag: $NEXT_RELEASE_TAG and version: $NEXT_RELEASE_VERSION!"

    # Don't load this plugin if
    # - `--dry-run` used
    # - `Dockerfile` is missing
    if ! $IS_DRY_RUN; then
      if [ ! -f Dockerfile ]; then
        log "Project does not have Dockerfile"
        return 1
      fi

      prepare

      docker build -t "$GIT_REPO_NAME:$NEXT_BUILD_VERSION" . --push
      docker tag "$GIT_REPO_NAME:$NEXT_BUILD_VERSION" "$GIT_REPO_NAME:latest"
      docker push "$GIT_REPO_NAME:latest"

      log "Docker image published [$NEXT_RELEASE_TAG]!"

      cleanup
    else
      log "Skipped Docker image [$NEXT_RELEASE_TAG] in DRY-RUN mode."
    fi

  else
    echo "
Docker Personal Access Token is not found
Please export Docker Personal Access Token so this plugin can be used
"
    exit 1
  fi
}

Here's what shellcheck currently says:

No reports

Here's what I wanted or expected to see:

Should be quoted variables

#!/bin/sh
set -eu

# Docker buildx script was copied from
# https://docs.docker.com/build/cloud/ci
# and modified by @dalisoft for AMD64/ARM64 platforms
+ARCH="$(uname -m | sed 's/aarch64/arm64/' | sed 's/x86_64/amd64/')"
+OS="$(uname -s | tr '[:upper:]' '[:lower:]')"

prepare() {
  BUILDX_URL=$(curl -s https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-lab-releases.json | grep "${OS}-${ARCH}\",$" | head -1 | xargs | tr -d ',' | xargs)
  # Download docker buildx with Hyrdobuild support
  mkdir -vp ~/.docker/cli-plugins/
  curl --silent -L --output ~/.docker/cli-plugins/docker-buildx "${BUILDX_URL}"
  chmod a+x ~/.docker/cli-plugins/docker-buildx

  echo "${DOCKER_HUB_PAT-}" | docker login --username "${DOCKER_HUB_USERNAME-}" --password-stdin
}

cleanup() {
  rm -rf ~/.docker/cli-plugins/

  docker logout
}

release() {
  # Build and publish a `Docker` tag
  if [ -n "${DOCKER_HUB_USERNAME-}" ] && [ -n "${DOCKER_HUB_PAT-}" ]; then

    log "Building and publishing Docker image..."
    log_verbose "Docker tag: $NEXT_RELEASE_TAG and version: $NEXT_RELEASE_VERSION!"

    # Don't load this plugin if
    # - `--dry-run` used
    # - `Dockerfile` is missing
    if ! $IS_DRY_RUN; then
      if [ ! -f Dockerfile ]; then
        log "Project does not have Dockerfile"
        return 1
      fi

      prepare

      docker build -t "$GIT_REPO_NAME:$NEXT_BUILD_VERSION" . --push
      docker tag "$GIT_REPO_NAME:$NEXT_BUILD_VERSION" "$GIT_REPO_NAME:latest"
      docker push "$GIT_REPO_NAME:latest"

      log "Docker image published [$NEXT_RELEASE_TAG]!"

      cleanup
    else
      log "Skipped Docker image [$NEXT_RELEASE_TAG] in DRY-RUN mode."
    fi

  else
    echo "
Docker Personal Access Token is not found
Please export Docker Personal Access Token so this plugin can be used
"
    exit 1
  fi
}
@dalisoft
Copy link
Author

dalisoft commented Jun 5, 2024

This case will may fix case of infertux/bashcov#86 partially

@brother
Copy link
Collaborator

brother commented Jun 5, 2024

Uppercase variables are treated as global and always skipped.
If you enable check-unassigned-uppercase you'll notice some extra warnings around that, not those lines though as they are used later on inside quoted things and most probably are safe any ways..

@ale5000-git
Copy link

You can use this directive to enable all warnings/info/style/etc. messages: # shellcheck enable=all

@aeiplatform
Copy link

This is correct behaviour

a=$(printf %s "a b c d")
echo "$a"

@dalisoft
Copy link
Author

dalisoft commented Jun 8, 2024

@ale5000-git Thank you, enabe=all solved my case

@dalisoft dalisoft closed this as completed Jun 8, 2024
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

No branches or pull requests

4 participants