Skip to content

Commit

Permalink
Fix empty client output path (#1159)
Browse files Browse the repository at this point in the history
* If the client output parameter is not specified, the client cannot automatically obtain the target file path

Signed-off-by: sunwp <[email protected]>
  • Loading branch information
244372610 authored Mar 16, 2022
1 parent 85d31a5 commit b53cbda
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions client/config/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,22 @@ func (cfg *ClientOption) Validate() error {
}

func (cfg *ClientOption) Convert(args []string) error {
var err error

if cfg.Output, err = filepath.Abs(cfg.Output); err != nil {
return err
if stringutils.IsBlank(cfg.Output) {
url := strings.TrimRight(cfg.URL, "/")
idx := strings.LastIndexByte(url, '/')
if idx < 0 {
return fmt.Errorf("get output from url[%s] error", cfg.URL)
}
cfg.Output = url[idx+1:]
}

if !filepath.IsAbs(cfg.Output) {
absPath, err := filepath.Abs(cfg.Output)
if err != nil {
return fmt.Errorf("get absolute path[%s] error: %v", cfg.Output, err)
}
cfg.Output = absPath
}
if cfg.URL == "" && len(args) > 0 {
cfg.URL = args[0]
}
Expand All @@ -180,7 +190,6 @@ func (cfg *ClientOption) Convert(args []string) error {
if cfg.Console {
cfg.ShowProgress = false
}

return nil
}

Expand All @@ -191,23 +200,9 @@ func (cfg *ClientOption) String() string {

// This function must be called after checkURL
func (cfg *ClientOption) checkOutput() error {
if stringutils.IsBlank(cfg.Output) {
url := strings.TrimRight(cfg.URL, "/")
idx := strings.LastIndexByte(url, '/')
if idx < 0 {
return fmt.Errorf("get output from url[%s] error", cfg.URL)
}
cfg.Output = url[idx+1:]
}

if !filepath.IsAbs(cfg.Output) {
absPath, err := filepath.Abs(cfg.Output)
if err != nil {
return fmt.Errorf("get absolute path[%s] error: %v", cfg.Output, err)
}
cfg.Output = absPath
return fmt.Errorf("path[%s] is not absolute path", cfg.Output)
}

outputDir, _ := path.Split(cfg.Output)
if err := MkdirAll(outputDir, 0777, basic.UserID, basic.UserGroup); err != nil {
return err
Expand Down

0 comments on commit b53cbda

Please sign in to comment.