如何在命令行应用程序中持久化数据?

huangapple go评论86阅读模式
英文:

How to persist data in a command-line application?

问题

我想使用Go或Node.js构建一个命令行应用程序。我希望它是多用户的,所以需要一个登录功能。我可以实现登录功能,但我不明白应该在哪里存储用户数据,以便在运行时的任何时刻都可以获取。

需要帮助找到一种存储用户数据以检查登录状态的方法。

编辑:
我曾考虑过这种方法-如果配置文件存在,则用户已登录,否则未登录。然后我意识到,如果有人篡改了文件,登录功能就失去了意义。我猜应该有更好的方法来实现,我想在这里询问一下。

英文:

I want to build a command-line application using Go or Node.js. I want to make it multiuser, so a login feature is needed. I can implement the login feature but I don't understand where should I store the user data which can be fetched any moment at runtime.
The user data is needed to check if a user is logged in or not if he is logged in which user it is.

Need help with a method to store user data to check logged in status

Edit:
I had thought of this- If the config file is present that the user is logged in else not. Then I realized that if one tampers the file, the whole point of login feature will get invalid. I am guessing there must be a better way to do it, which I am trying to know by asking here

答案1

得分: 3

许多命令行实用程序选择将其配置存储为用户主目录下的纯文本文件。
您可以使用任何可用的配置格式,如JSON、TOML等。

尽管值得一提的是,这种方式并不安全,如果用户不能以任何方式查看其他用户的信息,这不是首选方式。

英文:

Many cli utilities opt to store their configuration as a plain text file on the user's home directory.
You can use any of the available configuration formats such as JSON, TOML, etc.

Although it's good to mention that this is not a secure way of storing data and if users should not be able to see others information in any way this is not the preferred way.

huangapple
  • 本文由 发表于 2021年7月19日 23:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/68442813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定