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

I cannot beautify my code #11

Open
kamontat opened this issue Sep 9, 2017 · 21 comments
Open

I cannot beautify my code #11

kamontat opened this issue Sep 9, 2017 · 21 comments

Comments

@kamontat
Copy link

kamontat commented Sep 9, 2017

It throw this error out, but i'm not sure what it mean??

File src/core/download_resource_management: error: indent/outdent mismatch: 2.
@lovesegfault
Copy link
Owner

@kamontat Can you provide me the sample file?

@kamontat
Copy link
Author

here link

@jesselang
Copy link
Contributor

The code is no longer available. @kamontat can you post it here please? Otherwise there's nothing to do here.

@kamontat
Copy link
Author

So you mean, you also don't know what this error cause?

@kamontat
Copy link
Author

here the example code

#!/bin/bash

# -------------------------------------------------
# Description:  resource management of the download command
# List Command: check_is_resource_exist - check resource file exist
#               get_resource            - get the value in constant key
#               update_resource         - update value of resource constants in resource file
#               save_resource           - create new resource file or update if exist
# Usage:        run file in `source` command only
# Create by:    Kamontat Chantrachirathumrong
# Since:        09/09/2560 (DD/MM/YYYY)
# -------------------------------------------------
# Version:      1.0
# -------------------------------------------------
# Bug:
# -------------------------------------------------

[ -d "$NOVEL_LOCATION" ] || return 8
[ -n "$NOVEL_RESOURCE_NAME" ] || return 8

# -------------------------------------------------
# Constants
# -------------------------------------------------

# @require    - $NOVEL_LOCATION variable
#             - $NOVEL_RESOURCE_NAME variable
function get_resource_file {
    echo "$NOVEL_LOCATION"/$NOVEL_RESOURCE_NAME
}
DELIMITER="" # alt+t

# RESOURCE_CONSTANTS
LINK_CONSTANT="Link"                   # save as string
DATE_CONSTANT="Date"                   # save as string and can convert to date
VERSION_CONSTANT="Version"             # save as string
STORY_NAME_CONSTANT="StoryName"        # save as string
FIRST_CHAPTER_CONSTANT="FirstChapter"  # save as int
LAST_CHAPTER_CONSTANT="LastChapter"    # save as int
IGNORE_CONSTANT="Ignore"               # save as boolean

DATE_FORMAT="%d%m%y%H%M%S"

# -------------------------------------------------
# Export constants
# -------------------------------------------------

declare -x DATE="$(date +$DATE_FORMAT)"

# -------------------------------------------------
# Functions
# -------------------------------------------------

# @return     - true if resource file exist, otherwise return false
function is_resource_exist {
    is_file_exist "$(get_resource_file)"
}

# @params - 1 - RESOURCE_CONSTANTS only
# @return     - value of key in resource file
function get_resource {
    local temp=$(grep -w "^$1" "$(get_resource_file)")
    IFS="$DELIMITER" read -r -a ARR <<< "$temp"
    local result=${ARR[1]}
    if [[ $1 = "$DATE_CONSTANT" ]]; then
        echo "$(date -jf "$DATE_FORMAT" "$result")"
    else
        echo "$result"
    fi
}

# @require    - $DELIMITER variable
# @params - 1 - RESOURCE_CONSTANTS only
#           2 - new value of input constant
function update_resource {
    local temp=$(grep -nw "^$1" "$(get_resource_file)")
    local line_number=${temp%%:*}
    if [[ $line_number != "" ]]; then
        sed -i -e "${line_number}s/$1.*/$1$DELIMITER$2/" "$(get_resource_file)"
        rm -f "$(get_resource_file)-e"
    else
        echo "$1$DELIMITER$2" >> "$(get_resource_file)"
    fi
}

# @require    - check_is_resource_exist function
#             - get_resource function
#             - update_resource function
#             - $DELIMITER variable
#             - $COMMAND_VERSION variable
#             - $NOVEL_LINK variable
#             - $NOVEL_NAME variable
#             - $NOVEL_START variable
#             - $NOVEL_END variable
#             - $NOVEL_IS_IGNORE variable
#             - $DATE variable
function save_resource {
    if $(is_resource_exist); then
        [[ "$(get_resource $STORY_NAME_CONSTANT)" = "" ]] && update_resource $STORY_NAME_CONSTANT $STORY_NAME                    # in case version is too old
        [[ "$(get_resource $VERSION_CONSTANT)" != "$COMMAND_VERSION" ]] && update_resource $VERSION_CONSTANT $COMMAND_VERSION    # update version (only if not equal)
        [[ $NOVEL_START -lt $(get_resource $FIRST_CHAPTER_CONSTANT) ]] && update_resource $FIRST_CHAPTER_CONSTANT $NOVEL_START   # update first chapter (only if current less than previous)
        [[ $NOVEL_END -gt $(get_resource $LAST_CHAPTER_CONSTANT) ]] && update_resource $LAST_CHAPTER_CONSTANT $NOVEL_END         # update last chapter (only if current more than previous)
        update_resource $IGNORE_CONSTANT $NOVEL_IS_IGNORE                                                                        # update to ignore this (only if ignore is 'true')
        update_resource $DATE_CONSTANT $DATE                                                                                     # update date to current
    else
        echo "$VERSION_CONSTANT$DELIMITER$COMMAND_VERSION" >> "$(get_resource_file)"
        echo "$LINK_CONSTANT$DELIMITER$NOVEL_LINK" >> "$(get_resource_file)"
        echo "$STORY_NAME_CONSTANT$DELIMITER$NOVEL_NAME" >> "$(get_resource_file)"
        echo "$FIRST_CHAPTER_CONSTANT$DELIMITER$NOVEL_START" >> "$(get_resource_file)"
        echo "$LAST_CHAPTER_CONSTANT$DELIMITER$NOVEL_END" >> "$(get_resource_file)"
        echo "$DATE_CONSTANT$DELIMITER$DATE" >> "$(get_resource_file)"
        echo "$IGNORE_CONSTANT$DELIMITER$NOVEL_IS_IGNORE" >> "$(get_resource_file)"
    fi
}

@kamontat
Copy link
Author

example error
screen shot 2560-10-17 at 18 52 43

@lovesegfault
Copy link
Owner

@kamontat I have merged @jesselang 's PR which should have a fix for this. I already have the new beautysh version on PyPI, so try updating the package and testing it again.

@kamontat
Copy link
Author

screen shot 2560-10-18 at 00 17 58
Oh error still there...

@jesselang
Copy link
Contributor

Appears to be choking here:

function get_resource {
    local temp=$(grep -w "^$1" "$(get_resource_file)")

Unclear to me what's causing the error.

@kamontat
Copy link
Author

@jesselang oh but all syntax is correct right? 🤔

@lovesegfault
Copy link
Owner

@jesselang Yeah, I've been poking at his code now for a bit and I cannot understand what's causing the error. @kamontat Check your script with shellcheck.

@kamontat
Copy link
Author

kamontat commented Oct 18, 2017

First it cause this error to me so I fixes it, but the problem still going on
screen shot 2560-10-18 at 20 14 48

@kamontat
Copy link
Author

Here is final code that I fixes all code error warning and suggestion.

#!/bin/bash

# -------------------------------------------------
# Description:  resource management of the download command
# List Command: check_is_resource_exist - check resource file exist
#               get_resource            - get the value in constant key
#               update_resource         - update value of resource constants in resource file
#               save_resource           - create new resource file or update if exist
# Usage:        run file in `source` command only
# Create by:    Kamontat Chantrachirathumrong
# Since:        09/09/2560 (DD/MM/YYYY)
# -------------------------------------------------
# Version:      1.0
# -------------------------------------------------
# Bug:
# -------------------------------------------------

[ -d "$NOVEL_LOCATION" ] || return 8
[ -n "$NOVEL_RESOURCE_NAME" ] || return 8

# -------------------------------------------------
# Constants
# -------------------------------------------------

# @require    - $NOVEL_LOCATION variable
#             - $NOVEL_RESOURCE_NAME variable
function get_resource_file {
    echo "$NOVEL_LOCATION"/"$NOVEL_RESOURCE_NAME"
}
DELIMITER="" # alt+t

# RESOURCE_CONSTANTS
LINK_CONSTANT="Link"                   # save as string
DATE_CONSTANT="Date"                   # save as string and can convert to date
VERSION_CONSTANT="Version"             # save as string
STORY_NAME_CONSTANT="StoryName"        # save as string
FIRST_CHAPTER_CONSTANT="FirstChapter"  # save as int
LAST_CHAPTER_CONSTANT="LastChapter"    # save as int
IGNORE_CONSTANT="Ignore"               # save as boolean

DATE_FORMAT="%d%m%y%H%M%S"

# -------------------------------------------------
# Export constants
# -------------------------------------------------

export DATE
DATE="$(date +$DATE_FORMAT)"

# -------------------------------------------------
# Functions
# -------------------------------------------------

# @return     - true if resource file exist, otherwise return false
function is_resource_exist {
    is_file_exist "$(get_resource_file)"
}

# @params - 1 - RESOURCE_CONSTANTS only
# @return     - value of key in resource file
function get_resource {
    local temp, result
    temp=$(grep -w "^$1" "$(get_resource_file)")
    IFS="$DELIMITER" read -r -a ARR <<< "$temp"
    result=${ARR[1]}
    if [[ $1 = "$DATE_CONSTANT" ]]; then
        date -jf "$DATE_FORMAT" "$result"
    else
        echo "$result"
    fi
}

# @require    - $DELIMITER variable
# @params - 1 - RESOURCE_CONSTANTS only
#           2 - new value of input constant
function update_resource {
    local temp, line_number

    temp=$(grep -nw "^$1" "$(get_resource_file)")
    line_number=${temp%%:*}
    if [[ $line_number != "" ]]; then
        sed -i -e "${line_number}s/$1.*/$1$DELIMITER$2/" "$(get_resource_file)"
        rm -f "$(get_resource_file)-e"
    else
        echo "$1$DELIMITER$2" >> "$(get_resource_file)"
    fi
}

# @require    - check_is_resource_exist function
#             - get_resource function
#             - update_resource function
#             - $DELIMITER variable
#             - $COMMAND_VERSION variable
#             - $NOVEL_LINK variable
#             - $NOVEL_NAME variable
#             - $NOVEL_START variable
#             - $NOVEL_END variable
#             - $NOVEL_IS_IGNORE variable
#             - $DATE variable
function save_resource {
    if is_resource_exist; then
        [[ "$(get_resource $STORY_NAME_CONSTANT)" = "" ]] && update_resource $STORY_NAME_CONSTANT "$STORY_NAME"                    # in case version is too old
        [[ "$(get_resource $VERSION_CONSTANT)" != "$COMMAND_VERSION" ]] && update_resource $VERSION_CONSTANT "$COMMAND_VERSION"    # update version (only if not equal)
        [[ $NOVEL_START -lt $(get_resource $FIRST_CHAPTER_CONSTANT) ]] && update_resource $FIRST_CHAPTER_CONSTANT "$NOVEL_START"   # update first chapter (only if current less than previous)
        [[ $NOVEL_END -gt $(get_resource $LAST_CHAPTER_CONSTANT) ]] && update_resource $LAST_CHAPTER_CONSTANT "$NOVEL_END"         # update last chapter (only if current more than previous)
        update_resource $IGNORE_CONSTANT "$NOVEL_IS_IGNORE"                                                                        # update to ignore this (only if ignore is 'true')
        update_resource $DATE_CONSTANT "$DATE"                                                                                     # update date to current
    else
      {
        echo "$VERSION_CONSTANT$DELIMITER$COMMAND_VERSION"
        echo "$LINK_CONSTANT$DELIMITER$NOVEL_LINK"
        echo "$STORY_NAME_CONSTANT$DELIMITER$NOVEL_NAME"
        echo "$FIRST_CHAPTER_CONSTANT$DELIMITER$NOVEL_START"
        echo "$LAST_CHAPTER_CONSTANT$DELIMITER$NOVEL_END"
        echo "$DATE_CONSTANT$DELIMITER$DATE"
        echo "$IGNORE_CONSTANT$DELIMITER$NOVEL_IS_IGNORE"
      } >> "$(get_resource_file)"
    fi
}

@zone01-org
Copy link

in my code it fails to process lines with <<<

@lovesegfault
Copy link
Owner

This should now be fixed. @zone01-org Can you confirm the latest version solves this?

@rkennedy
Copy link

The current master branch exhibits the reported behavior on the code submitted by kamontat on October 18. Commit 9e98979 doesn't fix it. Here's a smaller example that exhibits the same behavior:

#!/bin/bash

function get_resource {
    IFS="$DELIMITER" read -r -a ARR <<< "$temp"
}

The problem is indeed the <<< here-string syntax. Beautysh mostly assumes that any appearance of << indicates a here document, which is false. The above commit changed beautysh to exclude a very specific use of here strings (i.e., the ones at the ends of while loops), but doesn't address broader use of here strings. (Several examples available in the Advanced Bash-Scripting Guide on the topic.)

@brlin-tw
Copy link

brlin-tw commented May 2, 2018

I encounter this issue as well, which is also reproduced with bashbeautify, which seems to have similar codebase: bergwerf/bashbeautify#12

Here's the stacktrace:

Traceback (most recent call last):
  File "/usr/local/bin/beautysh", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/dist-packages/beautysh/beautysh.py", line 11, in main
    Beautify().main()
  File "/usr/local/lib/python3.6/dist-packages/beautysh/beautysh.py", line 215, in main
    error |= self.beautify_file(path)
  File "/usr/local/lib/python3.6/dist-packages/beautysh/beautysh.py", line 177, in beautify_file
    result, error = self.beautify_string(data, '(stdin)')
  File "/usr/local/lib/python3.6/dist-packages/beautysh/beautysh.py", line 76, in beautify_string
    if(re.search(here_string, test_record) and not
  File "/usr/lib/python3.6/re.py", line 182, in search
    return _compile(pattern, flags).search(string)
  File "/usr/lib/python3.6/re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/usr/lib/python3.6/sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/lib/python3.6/sre_parse.py", line 855, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/lib/python3.6/sre_parse.py", line 416, in _parse_sub
    not nested and not items))
  File "/usr/lib/python3.6/sre_parse.py", line 488, in _parse
    sourceget()
  File "/usr/lib/python3.6/sre_parse.py", line 255, in get
    self.__next()
  File "/usr/lib/python3.6/sre_parse.py", line 245, in __next
    self.string, len(self.string) - 1) from None
sre_constants.error: bad escape (end of pattern) at position 49

@ApoXalvation
Copy link

ApoXalvation commented May 24, 2018

Same here:

#!/bin/bash
if true; then
  OUTPUT=$OUTPUT"\033[0;33mINTERFACES\033[0m\n"
fi

@Abasz
Copy link

Abasz commented Nov 13, 2020

Hi, I still have some problems with here string formatting in beautybash:

function install() {
    echo "Certificate is ready, downloading..."
    CERTIFICATES=$(curl api.zerossl.com/certificates/"$ID"/download/return?access_key="$ACCESS_KEY")

    echo "Installing new certificates"
    jq -r '.["certificate.crt"]' <<< "$CERTIFICATES" > "$APACHE_CONF_DIR/ssl/test.cert"
       jq -r '.["ca_bundle.crt"]' <<< "$CERTIFICATES" > "$APACHE_CONF_DIR/ssl/test_boundle.cert"
}

this would give an error: Couldn't format the document:File (stdin): error: indent/outdent mismatch: 1.

if I change the lines with the here string to be in double quotes "jq -r '.[\"certificate.crt\"]' <<< $CERTIFICATES)" formatting works fine.

@abrahammurciano
Copy link

abrahammurciano commented Apr 11, 2021

I am still getting this error. Here is my code, which runs fine

#!/usr/bin/env bash

# Program which will capitalize the first letter of all sentences in all files in the directory txt.

for file in txt/*; do
    replaced=$(< "$file" tr '\n' '\r' | sed -Ee "s/(^|[.?!]['’\"”)]?\s+)(['‘\"“(]?)([a-z])/\1\2\U\3/g" | tr '\r' '\n')
    echo "$replaced" > "$file"
done

@bartekpacia
Copy link

I get the same error, and my script works just fine.

@lovesegfault lovesegfault reopened this Sep 29, 2021
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

10 participants