Skip to content

Commit 67a85b8

Browse files
committed
update: make some flags as persitant flags
Signed-off-by: LinPr <[email protected]>
1 parent e9cd6c3 commit 67a85b8

File tree

6 files changed

+131
-24
lines changed

6 files changed

+131
-24
lines changed

cmd/ls/ls.go

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16+
// NOTE:refer to https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html#description
1617
func NewLsCmd() *cobra.Command {
1718
o := newOptions()
1819
cmd := cobra.Command{
@@ -21,10 +22,7 @@ func NewLsCmd() *cobra.Command {
2122
// Args: cobra.ExactArgs(1),
2223
Example: ls_examples,
2324
Run: func(cmd *cobra.Command, args []string) {
24-
if len(args) > 0 {
25-
o.S3Uri = args[0]
26-
}
27-
if err := o.complete(); err != nil {
25+
if err := o.complete(cmd, args); err != nil {
2826
fmt.Fprintf(os.Stderr, "err: %v\n", err)
2927
return
3028
}
@@ -39,7 +37,7 @@ func NewLsCmd() *cobra.Command {
3937
},
4038
}
4139

42-
cmd.Flags().BoolVarP(&o.DryRun, "dryRun", "n", false, "show what would be transferred")
40+
// cmd.Flags().BoolVarP(&o.DryRun, "dryRun", "n", false, "show what would be transferred")
4341

4442
return &cmd
4543
}
@@ -48,9 +46,19 @@ type Args struct {
4846
S3Uri string `validate:"omitempty"`
4947
}
5048
type Flags struct {
51-
DryRun bool `json:"DryRun" yaml:"DryRun"`
52-
Region string `json:"Region" yaml:"Region"`
53-
Summarize string `json:"Summarize" yaml:"Summarize"`
49+
//TODO: 全局flag 需要移动到 root 中
50+
Debug bool
51+
EndpointUrl string
52+
NoVerifySSL bool
53+
NoPaginate bool
54+
Output string
55+
Profile string
56+
Region string
57+
Recursive bool
58+
Summarize string
59+
HumanReadable bool
60+
PageSize int32
61+
PathStyle bool
5462
}
5563

5664
type Options struct {
@@ -62,8 +70,40 @@ func newOptions() *Options {
6270
return &Options{}
6371
}
6472

65-
func (o *Options) complete() error {
66-
// 使用 viper 获取到最终生效的配置 flag > env > config > default
73+
func (o *Options) complete(cmd *cobra.Command, args []string) error {
74+
if len(args) > 0 {
75+
o.S3Uri = args[0]
76+
}
77+
// Get persistent flags from parent command
78+
if cmd.Parent() != nil {
79+
parentFlags := cmd.Parent().PersistentFlags()
80+
81+
if parentFlags.Lookup("debug") != nil {
82+
o.Debug, _ = parentFlags.GetBool("debug")
83+
}
84+
if parentFlags.Lookup("endpoint-url") != nil {
85+
o.EndpointUrl, _ = parentFlags.GetString("endpoint-url")
86+
}
87+
if parentFlags.Lookup("no-verify-ssl") != nil {
88+
o.NoVerifySSL, _ = parentFlags.GetBool("no-verify-ssl")
89+
}
90+
if parentFlags.Lookup("no-paginate") != nil {
91+
o.NoPaginate, _ = parentFlags.GetBool("no-paginate")
92+
}
93+
if parentFlags.Lookup("output") != nil {
94+
o.Output, _ = parentFlags.GetString("output")
95+
}
96+
if parentFlags.Lookup("profile") != nil {
97+
o.Profile, _ = parentFlags.GetString("profile")
98+
}
99+
if parentFlags.Lookup("region") != nil {
100+
o.Region, _ = parentFlags.GetString("region")
101+
}
102+
if parentFlags.Lookup("path-style") != nil {
103+
o.PathStyle, _ = parentFlags.GetBool("path-style")
104+
}
105+
106+
}
67107
return nil
68108
}
69109

@@ -79,7 +119,9 @@ func (o *Options) run() error {
79119
j, _ := json.Marshal(o)
80120
fmt.Fprintf(os.Stdout, "options: %s\n", string(j))
81121
// return nil
82-
opt := s3store.S3Option{}
122+
opt := s3store.S3Option{
123+
UsePathStyle: o.PathStyle,
124+
}
83125

84126
cli, err := s3store.NewS3Client(context.TODO(), opt)
85127
if err != nil {

cmd/mb/mb.go

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func NewMbCmd() *cobra.Command {
2121
Args: cobra.ExactArgs(1),
2222
Example: mb_examples,
2323
Run: func(cmd *cobra.Command, args []string) {
24-
o.S3Uri = args[0]
25-
if err := o.complete(); err != nil {
24+
25+
if err := o.complete(cmd, args); err != nil {
2626
fmt.Fprintf(os.Stderr, "err: %v\n", err)
2727
return
2828
}
@@ -44,11 +44,21 @@ func NewMbCmd() *cobra.Command {
4444
}
4545

4646
type Args struct {
47-
S3Uri string `validate:"required"`
47+
S3Uri string
4848
}
4949
type Flags struct {
50-
DryRun bool `json:"DryRun" yaml:"DryRun"`
51-
Region string `json:"Region" yaml:"Region"`
50+
DryRun bool
51+
EndpointUrl string
52+
NoVerifySSL bool
53+
NoPaginate bool
54+
Output string
55+
Profile string
56+
Region string
57+
Recursive bool
58+
Summarize string
59+
HumanReadable bool
60+
PageSize int32
61+
PathStyle bool
5262
}
5363

5464
type Options struct {
@@ -60,8 +70,33 @@ func newOptions() *Options {
6070
return &Options{}
6171
}
6272

63-
func (o *Options) complete() error {
64-
// 使用 viper 获取到最终生效的配置 flag > env > config > default
73+
func (o *Options) complete(cmd *cobra.Command, args []string) error {
74+
o.S3Uri = args[0]
75+
76+
parentFlags := cmd.Parent().PersistentFlags()
77+
if parentFlags != nil {
78+
if parentFlags.Lookup("endpoint-url") != nil {
79+
o.EndpointUrl, _ = parentFlags.GetString("endpoint-url")
80+
}
81+
if parentFlags.Lookup("no-verify-ssl") != nil {
82+
o.NoVerifySSL, _ = parentFlags.GetBool("no-verify-ssl")
83+
}
84+
if parentFlags.Lookup("no-paginate") != nil {
85+
o.NoPaginate, _ = parentFlags.GetBool("no-paginate")
86+
}
87+
if parentFlags.Lookup("output") != nil {
88+
o.Output, _ = parentFlags.GetString("output")
89+
}
90+
if parentFlags.Lookup("profile") != nil {
91+
o.Profile, _ = parentFlags.GetString("profile")
92+
}
93+
if parentFlags.Lookup("region") != nil {
94+
o.Region, _ = parentFlags.GetString("region")
95+
}
96+
if parentFlags.Lookup("path-style") != nil {
97+
o.PathStyle, _ = parentFlags.GetBool("path-style")
98+
}
99+
}
65100
return nil
66101
}
67102

@@ -86,7 +121,11 @@ func (o *Options) run() error {
86121
fmt.Fprintf(os.Stdout, "options: %s\n", string(j))
87122
// return nil
88123

89-
cli, err := s3store.NewS3Client(context.TODO(), s3store.S3Option{})
124+
opt := s3store.S3Option{
125+
UsePathStyle: o.PathStyle,
126+
Region: o.Region,
127+
}
128+
cli, err := s3store.NewS3Client(context.TODO(), opt)
90129
if err != nil {
91130
return err
92131
}

cmd/root.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ import (
2323
var ConfigFile string
2424

2525
type Options struct {
26+
EndpointUrl string
27+
NoVerifySSL bool
28+
NoPaginate bool
29+
Output string
30+
Profile string
31+
Region string
32+
PathStyle bool
2633
}
2734

2835
func NewOptions() *Options {
@@ -77,6 +84,14 @@ func NewRootCmd() *cobra.Command {
7784
defaultConfigPath := fmt.Sprintf("%s/.s6cmd.yaml", homeDir)
7885
cmd.PersistentFlags().StringVar(&ConfigFile, "config", "", "default to "+defaultConfigPath)
7986

87+
cmd.PersistentFlags().StringVarP(&o.EndpointUrl, "endpoint-url", "e", "", "Override the default endpoint URL (or use AWS_ENDPOINT_URL_S3 environment variable)")
88+
cmd.PersistentFlags().BoolVarP(&o.NoVerifySSL, "no-verify-ssl", "", false, "Disable SSL certificate verification (or use AWS_NO_VERIFY_SSL environment variable)")
89+
cmd.PersistentFlags().BoolVarP(&o.NoPaginate, "no-paginate", "", false, "Disable automatic pagination of responses (or use AWS_NO_PAGINATE environment variable)")
90+
cmd.PersistentFlags().StringVarP(&o.Output, "output", "o", "text", "Set output format. One of: json, text, table (or use AWS_OUTPUT environment variable)")
91+
cmd.PersistentFlags().StringVarP(&o.Profile, "profile", "p", "", "Use a specific profile from your credential file (or use AWS_PROFILE environment variable)")
92+
cmd.PersistentFlags().StringVarP(&o.Region, "region", "r", "", "The region to use. Overrides config/env settings (or use AWS_REGION environment variable)")
93+
cmd.PersistentFlags().BoolVarP(&o.PathStyle, "path-style", "", false, "Force to use path style addressing (or use S6CMD_USE_PATH_STYLE environment variable)")
94+
8095
if err := viper.BindEnv("config", "S6CMD_CONFIG"); err != nil {
8196
panic(err)
8297
}

cmd/stat/stat.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func NewStatCmd() *cobra.Command {
3737
}
3838

3939
cmd.Flags().BoolVarP(&o.DryRun, "dryRun", "n", false, "show what would be transferred")
40+
cmd.Flags().BoolVarP(&o.UsePathStyle, "usePathStyle", "", false, "force to use path style addressing")
4041

4142
return &cmd
4243
}
@@ -45,7 +46,8 @@ type Args struct {
4546
S3Uri string `validate:"omitempty"`
4647
}
4748
type Flags struct {
48-
DryRun bool `json:"DryRun" yaml:"DryRun"`
49+
UsePathStyle bool `json:"UsePathStyle" yaml:"UsePathStyle"`
50+
DryRun bool `json:"DryRun" yaml:"DryRun"`
4951
}
5052

5153
type Options struct {
@@ -75,7 +77,10 @@ func (o *Options) run() error {
7577
fmt.Fprintf(os.Stdout, "options: %s\n", string(j))
7678
// return nil
7779

78-
cli, err := s3store.NewS3Client(context.TODO(), s3store.S3Option{})
80+
cli, err := s3store.NewS3Client(context.TODO(), s3store.S3Option{
81+
UsePathStyle: o.UsePathStyle,
82+
})
83+
7984
if err != nil {
8085
return err
8186
}

storage/s3/option.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package s3store
33
// Options stores configuration for storage.
44
type S3Option struct {
55
// 已实现
6-
Region string
6+
Region string
7+
UsePathStyle bool
78

89
// 待实现
910
MaxRetries int

storage/s3/s3store.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ func NewS3Client(ctx context.Context, option S3Option) (*S3Store, error) {
5757
if err != nil {
5858
return nil, err
5959
}
60-
61-
client := s3.NewFromConfig(conf)
60+
fmt.Printf("option.UsePathStyle: %v\n", option.UsePathStyle)
61+
client := s3.NewFromConfig(conf,
62+
func(o *s3.Options) {
63+
o.UsePathStyle = option.UsePathStyle
64+
o.Region = option.Region
65+
},
66+
)
6267
uploader := manager.NewUploader(client)
6368
downloader := manager.NewDownloader(client)
6469

0 commit comments

Comments
 (0)