Skip to content

Password as CLI argument #34

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

Closed
wants to merge 6 commits into from
Closed

Password as CLI argument #34

wants to merge 6 commits into from

Conversation

Himura2la
Copy link
Contributor

I don't want to enter password every time. I created run-scripts with the host and file and I need it to run quick.

@dpgeorge
Copy link
Member

It would be preferable to specify the password using a switch, like -p mypass (and it's not hard to do this without argparse). That's what users would expect. Additional positional arguments would tend to indicate that you are copying multiple files.

Also, please don't use f-strings, they will only work with very recent versions of CPython.

@Himura2la
Copy link
Contributor Author

Himura2la commented Sep 22, 2017

Multiple files are not supported so it's OK, one need a single glance on format to realize it. However, if you insist on -p, I can implement it.
I'll get rid of the interpolated string, OK.

@Himura2la
Copy link
Contributor Author

I hope it's OK if -p argument must be the last one, trying to write another argparse will be rather complex task

@pfalcon
Copy link
Contributor

pfalcon commented Sep 22, 2017

I hope it's OK if -p argument must be the last one, trying to write another argparse will be rather complex task

Better to let it appear anywhere. And no, you don't need to write another argparse for this (e.g. it's ok to assume -p and the actual password will be separated by a space).

@Himura2la
Copy link
Contributor Author

Himura2la commented Sep 22, 2017

IMHO adding so much abstract arguments syntax is not a MicroPython way either, but OK, I'll try to do it with not very many lines and changes...

@pfalcon
Copy link
Contributor

pfalcon commented Sep 22, 2017

There's definitely another alternative, which we followed so far: to treat a webrepl client implementation here as a simple, reference one and let community develop "value added" tools using it as an example.

But if there's desire to add a password arg to the reference client, it apparently should be done to not potentially confuse users (not in an adhoc way).

@Himura2la
Copy link
Contributor Author

Himura2la commented Sep 22, 2017

"value added" tools are not good in this case, because passwords should never be hardcoded inside tools. Password should be near host address or nowhere.
Please check my last commit.

Following the no ad-hoc code way I also got rid of this e9ff506#diff-275e901131ecb58a8c9fee40909d64c0L224

There are still two unconditional print() calls whose aim I don't understand, maybe they are redundant on OUTPUT_LEVEL = 0

@Himura2la
Copy link
Contributor Author

Himura2la commented Sep 25, 2017

how about merge? *_*

Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

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

Thanks @Himura2la for updating the patch. But please note that the MicroPython philosophy of minimal and clean code applies not only to the core VM/runtime but also to the tools like this one. To that end there's a much simpler way to support optional args which allows to more easily add other options than the way you did it. Eg something like (the following is untested code):

passwd = None
i = 1
while i < len(sys.argv):
    if sys.argv[i] == '--':
        sys.argv.pop(i)
        break
    elif sys.argv[i] == '-p':
        sys.argv.pop(i)
        if i >= len(sys.argv):
            help(1)
        passwd = sys.argv.pop(i)
    else:
        i += 1
if passwd is None:
    import getpass
    passwd = getpass.getpass()

webrepl_cli.py Outdated
@@ -14,7 +14,7 @@
# Treat this remote directory as a root for file transfers
SANDBOX = ""
#SANDBOX = "/tmp/webrepl/"
DEBUG = 0
OUTPUT_LEVEL = 1
Copy link
Member

Choose a reason for hiding this comment

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

No need to change this.

webrepl_cli.py Outdated
print(msg)

def infomsg(msg):
Copy link
Member

Choose a reason for hiding this comment

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

No need to add this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm afraid

if 1:
    print("op:%s, host:%s, port:%d, passwd:%s." % (op, host, port, passwd))
    print(src_file, "->", dst_file)

is a kind of bad code. But if you insist to leave it, OK then. Or maybe we could add a parameter to debugmsg() ?

webrepl_cli.py Outdated
print(" <host>:<remote_file> <local_file> - Copy remote file to local file")
print(" <local_file> <host>:<remote_file> - Copy local file to remote file")
print(" <host>:<remote_file> <local_file> [-p password] - Copy remote file to local file")
print(" <local_file> <host>:<remote_file> [-p password] - Copy local file to remote file")
Copy link
Member

Choose a reason for hiding this comment

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

It's customary to put options before the mandatory positional args.

webrepl_cli.py Outdated
if os.path.isdir(dst_file):
basename = src_file.rsplit("/", 1)[-1]
dst_file += "/" + basename
infomsg("%s %s from %s:%d (passwd:%s) and save as %s" %
(op, src_file, host, port, passwd, os.path.abspath(dst_file)))
Copy link
Member

Choose a reason for hiding this comment

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

Please leave the original messages as they were. It particular you don't want to print out the password!

@Himura2la
Copy link
Contributor Author

Himura2la commented Sep 29, 2017

OK, what do you think?
I was not sure it's safe to modify sys.argv, but this really is much clearer code

@Himura2la
Copy link
Contributor Author

@dpgeorge ping

@bhollosi
Copy link

patch working as intended, this is a huge improvement to use webrepl in a shell script.
merge pls!

@Himura2la
Copy link
Contributor Author

@dpgeorge @pfalcon ping

@pfalcon
Copy link
Contributor

pfalcon commented Nov 11, 2017

@Himura2la : Instead of pop'ing values out of sys.args, it would be better to just parse it once and assign values to local vars. Because your approach doesn't scale if more options are added.

But what really precluded your patch to be merged is that it contained unrelated changes. If you want to add a password option, the patch should contain the minimal changes to achieve it, and not a character more.

Anyway, thanks for persistence, I cleaned up the patch and merged it.

@pfalcon pfalcon closed this Nov 11, 2017
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.

4 participants