import re, click
from shub.utils import get_key_netrc, NETRC_FILE

@click.command(help='remove Scrapinghug API key from the netrc file')
@click.pass_context
def cli(context):
    if not get_key_netrc():
        context.fail('Key not found in netrc file')
    with open(NETRC_FILE, 'r+') as out:
        key_re = r'machine\s+scrapinghub\.com\s+login\s+\w+\s+password\s+""\s*'
        content = out.read()
        content_new = re.sub(key_re, '', content)
        if content_new != content:
            out.seek(0)
            out.truncate()
            out.write(content_new)
    if get_key_netrc():
        context.fail(
            'Error removing key from the netrc file.'
            'Please, open the file ~/.netrc and remove it manually')

import re, click
from shub.utils import get_key_netrc, NETRC_FILE

@click.command(help='remove Scrapinghug API key from the netrc file')
@click.pass_context
def cli(context):
    if not get_key_netrc():
        context.fail('Key not found in netrc file')
    error, msg = remove_sh_key()
    if error:
        context.fail(msg)
    if get_key_netrc():
        context.fail(
            'Error removing key from the netrc file.'
            'Please, open the file ~/.netrc and remove it manually')

def remove_sh_key():
    key_re = r'machine\s+scrapinghub\.com\s+login\s+\w+\s+password\s+""\s*'
    error_msg = ''
    try:
        with open(NETRC_FILE, 'r+') as out:
            content = out.read()
            content_new = re.sub(key_re, '', content)
            if content_new == content:
                error_msg = 'Regex didn\'t match. \
                             Key wasn\'t removed from netrc file'
            else:
                out.seek(0)
                out.truncate()
                out.write(content_new)
    except Exception as err:
        error_msg = str(err)
    if error_msg:
        return True, error_msg
    else:
        return False, ''

Add verification for removing key from netrc file
Add verification for removing key from netrc file

  import re, click
  from shub.utils import get_key_netrc, NETRC_FILE
  
  @click.command(help='remove Scrapinghug API key from the netrc file')
  @click.pass_context
  def cli(context):
      if not get_key_netrc():
          context.fail('Key not found in netrc file')
+     error, msg = remove_sh_key()
+     if error:
+         context.fail(msg)
-     with open(NETRC_FILE, 'r+') as out:
-         key_re = r'machine\s+scrapinghub\.com\s+login\s+\w+\s+password\s+""\s*'
-         content = out.read()
-         content_new = re.sub(key_re, '', content)
-         if content_new != content:
-             out.seek(0)
-             out.truncate()
-             out.write(content_new)
      if get_key_netrc():
          context.fail(
              'Error removing key from the netrc file.'
              'Please, open the file ~/.netrc and remove it manually')
  
+ def remove_sh_key():
+     key_re = r'machine\s+scrapinghub\.com\s+login\s+\w+\s+password\s+""\s*'
+     error_msg = ''
+     try:
+         with open(NETRC_FILE, 'r+') as out:
+             content = out.read()
+             content_new = re.sub(key_re, '', content)
+             if content_new == content:
+                 error_msg = 'Regex didn\'t match. \
+                              Key wasn\'t removed from netrc file'
+             else:
+                 out.seek(0)
+                 out.truncate()
+                 out.write(content_new)
+     except Exception as err:
+         error_msg = str(err)
+     if error_msg:
+         return True, error_msg
+     else:
+         return False, ''
+ 
Add verification for removing key from netrc file
## Code Before:
import re, click
from shub.utils import get_key_netrc, NETRC_FILE

@click.command(help='remove Scrapinghug API key from the netrc file')
@click.pass_context
def cli(context):
    if not get_key_netrc():
        context.fail('Key not found in netrc file')
    with open(NETRC_FILE, 'r+') as out:
        key_re = r'machine\s+scrapinghub\.com\s+login\s+\w+\s+password\s+""\s*'
        content = out.read()
        content_new = re.sub(key_re, '', content)
        if content_new != content:
            out.seek(0)
            out.truncate()
            out.write(content_new)
    if get_key_netrc():
        context.fail(
            'Error removing key from the netrc file.'
            'Please, open the file ~/.netrc and remove it manually')

## Instruction:
Add verification for removing key from netrc file
## Code After:
import re, click
from shub.utils import get_key_netrc, NETRC_FILE

@click.command(help='remove Scrapinghug API key from the netrc file')
@click.pass_context
def cli(context):
    if not get_key_netrc():
        context.fail('Key not found in netrc file')
    error, msg = remove_sh_key()
    if error:
        context.fail(msg)
    if get_key_netrc():
        context.fail(
            'Error removing key from the netrc file.'
            'Please, open the file ~/.netrc and remove it manually')

def remove_sh_key():
    key_re = r'machine\s+scrapinghub\.com\s+login\s+\w+\s+password\s+""\s*'
    error_msg = ''
    try:
        with open(NETRC_FILE, 'r+') as out:
            content = out.read()
            content_new = re.sub(key_re, '', content)
            if content_new == content:
                error_msg = 'Regex didn\'t match. \
                             Key wasn\'t removed from netrc file'
            else:
                out.seek(0)
                out.truncate()
                out.write(content_new)
    except Exception as err:
        error_msg = str(err)
    if error_msg:
        return True, error_msg
    else:
        return False, ''
