|
30 | 30 | import hashlib
|
31 | 31 | import json
|
32 | 32 | import tempfile
|
| 33 | +import contextlib |
33 | 34 |
|
34 | 35 | VERSION = "0.0.4"
|
35 | 36 |
|
@@ -761,6 +762,25 @@ def config_get(args):
|
761 | 762 | print(val)
|
762 | 763 | return 0
|
763 | 764 |
|
| 765 | + def mkconfig(args): |
| 766 | + @contextlib.contextmanager |
| 767 | + def get_output_file(): |
| 768 | + if args.output == "-": |
| 769 | + yield sys.stdout |
| 770 | + else: |
| 771 | + with open(args.output, "w" if args.force else "x") as f: |
| 772 | + yield f |
| 773 | + print(os.path.abspath(args.output)) |
| 774 | + |
| 775 | + try: |
| 776 | + with get_output_file() as f: |
| 777 | + f.write(read_default_config(False)) |
| 778 | + except FileExistsError: |
| 779 | + sys.stderr.write("Refusing to overwrite existing file '%s'\n" % args.output) |
| 780 | + return 1 |
| 781 | + |
| 782 | + return 0 |
| 783 | + |
764 | 784 | subparser_args = {}
|
765 | 785 | if sys.version_info >= (3, 7, 0):
|
766 | 786 | subparser_args["required"] = True
|
@@ -827,6 +847,23 @@ def config_get(args):
|
827 | 847 | )
|
828 | 848 | config_get_parser.set_defaults(func=config_get)
|
829 | 849 |
|
| 850 | + mkconfig_parser = subparsers.add_parser( |
| 851 | + "mkconfig", help="Create a default Pyrex configuration" |
| 852 | + ) |
| 853 | + mkconfig_parser.add_argument( |
| 854 | + "-f", |
| 855 | + "--force", |
| 856 | + action="store_true", |
| 857 | + help="Overwrite destination file if it already exists", |
| 858 | + ) |
| 859 | + mkconfig_parser.add_argument( |
| 860 | + "output", |
| 861 | + default="-", |
| 862 | + nargs="?", |
| 863 | + help="Output file. Use '-' for standard out. Default is %(default)s", |
| 864 | + ) |
| 865 | + mkconfig_parser.set_defaults(func=mkconfig) |
| 866 | + |
830 | 867 | args = parser.parse_args()
|
831 | 868 |
|
832 | 869 | func = getattr(args, "func", None)
|
|
0 commit comments