@@ -570,7 +570,12 @@ def config():
570
570
pass
571
571
572
572
573
- @config .command (short_help = "Return the config file for s3" )
573
+ def _safe_makedirs (path : str ):
574
+ if path not in ("" , "." , "/" ):
575
+ os .makedirs (path , exist_ok = True )
576
+
577
+
578
+ @config .command (short_help = "Update the config file for s3" )
574
579
@click .option (
575
580
"-p" ,
576
581
"--path" ,
@@ -584,7 +589,8 @@ def config():
584
589
@click .argument ("aws_access_key_id" )
585
590
@click .argument ("aws_secret_access_key" )
586
591
@click .option ("-e" , "--endpoint-url" , help = "endpoint-url" )
587
- @click .option ("-s" , "--addressing-style" , help = "addressing-style" )
592
+ @click .option ("-as" , "--addressing-style" , help = "addressing-style" )
593
+ @click .option ("-sv" , "--signature-version" , help = "signature-version" )
588
594
@click .option ("--no-cover" , is_flag = True , help = "Not cover the same-name config" )
589
595
def s3 (
590
596
path ,
@@ -593,6 +599,7 @@ def s3(
593
599
aws_secret_access_key ,
594
600
endpoint_url ,
595
601
addressing_style ,
602
+ signature_version ,
596
603
no_cover ,
597
604
):
598
605
path = os .path .expanduser (path )
@@ -602,30 +609,27 @@ def s3(
602
609
"aws_access_key_id" : aws_access_key_id ,
603
610
"aws_secret_access_key" : aws_secret_access_key ,
604
611
}
605
- s3 = {}
606
- if endpoint_url :
607
- s3 .update ({"endpoint_url" : endpoint_url })
608
- if addressing_style :
609
- s3 .update ({"addressing_style" : addressing_style })
610
- if s3 :
611
- config_dict .update ({"s3" : s3 })
612
+ s3_config_dict = {
613
+ "endpoint_url" : endpoint_url ,
614
+ "addressing_style" : addressing_style ,
615
+ "signature_version" : signature_version ,
616
+ }
617
+
618
+ s3_config_dict = {k : v for k , v in s3_config_dict .items () if v }
619
+ if s3_config_dict :
620
+ config_dict ["s3" ] = s3_config_dict
612
621
613
622
def dumps (config_dict : dict ) -> str :
614
623
content = "[{}]\n " .format (config_dict ["name" ])
615
- content += "aws_access_key_id = {}\n " .format (config_dict ["aws_access_key_id" ])
616
- content += "aws_secret_access_key = {}\n " .format (
617
- config_dict ["aws_secret_access_key" ]
618
- )
624
+ for key in ("aws_access_key_id" , "aws_secret_access_key" ):
625
+ content += "{} = {}\n " .format (key , config_dict [key ])
619
626
if "s3" in config_dict .keys ():
620
627
content += "\n s3 = \n "
621
- s3 : dict = config_dict ["s3" ]
622
- if "endpoint_url" in s3 .keys ():
623
- content += " endpoint_url = {}\n " .format (s3 ["endpoint_url" ])
624
- if "addressing_style" in s3 .keys ():
625
- content += " addressing_style = {}\n " .format (s3 ["addressing_style" ])
628
+ for key , value in config_dict ["s3" ].items ():
629
+ content += " {} = {}\n " .format (key , value )
626
630
return content
627
631
628
- os . makedirs (os .path .dirname (path ), exist_ok = True ) # make sure dirpath exist
632
+ _safe_makedirs (os .path .dirname (path )) # make sure dirpath exist
629
633
if not os .path .exists (path ): # If this file doesn't exist.
630
634
content_str = dumps (config_dict )
631
635
with open (path , "w" ) as fp :
@@ -663,15 +667,15 @@ def dumps(config_dict: dict) -> str:
663
667
click .echo (f"Your oss config has been saved into { path } " )
664
668
665
669
666
- @config .command (short_help = "Return the config file for s3" )
667
- @click .argument ("url" )
670
+ @config .command (short_help = "Update the config file for hdfs" )
668
671
@click .option (
669
672
"-p" ,
670
673
"--path" ,
671
674
default = "~/.hdfscli.cfg" ,
672
- help = "s3 config file, default is $HOME/.hdfscli.cfg" ,
675
+ help = "hdfs config file, default is $HOME/.hdfscli.cfg" ,
673
676
)
674
- @click .option ("-n" , "--profile-name" , default = "default" , help = "s3 config file" )
677
+ @click .argument ("url" )
678
+ @click .option ("-n" , "--profile-name" , default = "default" , help = "hdfs config file" )
675
679
@click .option ("-u" , "--user" , help = "user name" )
676
680
@click .option ("-r" , "--root" , help = "hdfs path's root dir" )
677
681
@click .option ("-t" , "--token" , help = "token for requesting hdfs server" )
@@ -681,7 +685,7 @@ def dumps(config_dict: dict) -> str:
681
685
help = f"request hdfs server timeout, default { DEFAULT_HDFS_TIMEOUT } " ,
682
686
)
683
687
@click .option ("--no-cover" , is_flag = True , help = "Not cover the same-name config" )
684
- def hdfs (url , path , profile_name , user , root , token , timeout , no_cover ):
688
+ def hdfs (path , url , profile_name , user , root , token , timeout , no_cover ):
685
689
path = os .path .expanduser (path )
686
690
current_config = {
687
691
"url" : url ,
@@ -704,11 +708,40 @@ def hdfs(url, path, profile_name, user, root, token, timeout, no_cover):
704
708
for key , value in current_config .items ():
705
709
if value :
706
710
config [profile_name ][key ] = value
711
+
712
+ _safe_makedirs (os .path .dirname (path )) # make sure dirpath exist
707
713
with open (path , "w" ) as fp :
708
714
config .write (fp )
709
715
click .echo (f"Your hdfs config has been saved into { path } " )
710
716
711
717
718
+ @config .command (short_help = "Update the config file for aliases" )
719
+ @click .option (
720
+ "-p" ,
721
+ "--path" ,
722
+ default = "~/.config/megfile/aliases.conf" ,
723
+ help = "alias config file, default is $HOME/.config/megfile/aliases.conf" ,
724
+ )
725
+ @click .argument ("name" )
726
+ @click .argument ("protocol" )
727
+ @click .option ("--no-cover" , is_flag = True , help = "Not cover the same-name config" )
728
+ def alias (path , name , protocol , no_cover ):
729
+ path = os .path .expanduser (path )
730
+ config = configparser .ConfigParser ()
731
+ if os .path .exists (path ):
732
+ config .read (path )
733
+ if name in config .sections () and no_cover :
734
+ raise NameError (f"alias-name has been used: { name } " )
735
+ config [name ] = {
736
+ "protocol" : protocol ,
737
+ }
738
+
739
+ _safe_makedirs (os .path .dirname (path )) # make sure dirpath exist
740
+ with open (path , "w" ) as fp :
741
+ config .write (fp )
742
+ click .echo (f"Your alias config has been saved into { path } " )
743
+
744
+
712
745
if __name__ == "__main__" :
713
746
# Usage: python -m megfile.cli
714
747
safe_cli () # pragma: no cover
0 commit comments