Skip to content

Commit

Permalink
feat: add download cmd (#176)
Browse files Browse the repository at this point in the history
* feat: add download cmd
  • Loading branch information
hunjixin authored Sep 1, 2024
1 parent 402cc50 commit 6f2a1a3
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Upload
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
token: ${{secrets.CODECOV_TOKEN}}
files: ./coverage.out
name: jzfs
fail_ci_if_error: true
Expand Down
4 changes: 2 additions & 2 deletions cmd/aksk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ var createAkskCmd = &cobra.Command{
Description: utils.String(desc),
})
if err != nil {
return err
return fmt.Errorf("request aksk %w", err)
}

result, err := api.ParseCreateAkskResponse(resp)
if err != nil {
return err
return fmt.Errorf("parser aksk response %w", err)
}

fmt.Printf("ak %s sk %s \n", result.JSON201.AccessKey, result.JSON201.SecretKey)
Expand Down
100 changes: 94 additions & 6 deletions cmd/uploadfiles.go → cmd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ var uploadCmd = &cobra.Command{
return errors.New("owner and repo must be set")
}

refName, err := cmd.Flags().GetString("refName")
refName, err := cmd.Flags().GetString("ref-name")
if err != nil {
return err
}
if len(refName) == 0 {
return errors.New("refName must be set")
}

uploadPath, err := cmd.Flags().GetString("uploadPath")
uploadPath, err := cmd.Flags().GetString("upload-path")
if err != nil {
return err
}
Expand All @@ -69,7 +69,7 @@ var uploadCmd = &cobra.Command{
uploadPath = "/"
}

ignoreRootName, err := cmd.Flags().GetBool("ignoreRootName")
ignoreRootName, err := cmd.Flags().GetBool("ignore-root-name")
if err != nil {
return err
}
Expand Down Expand Up @@ -143,14 +143,102 @@ var uploadCmd = &cobra.Command{
},
}

// versionCmd represents the version command
var downloadCmd = &cobra.Command{
Use: "download",
Short: "download files from server",
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
client, err := GetClient(cmd)
if err != nil {
return err
}

path, err := cmd.Flags().GetString("path")
if err != nil {
return err
}
if len(path) == 0 {
return errors.New("path must be set")
}

owner, err := cmd.Flags().GetString("owner")
if err != nil {
return err
}
if len(owner) == 0 {
return errors.New("owner must be set")
}

repo, err := cmd.Flags().GetString("repo")
if err != nil {
return err
}
if len(owner) == 0 || len(repo) == 0 {
return errors.New("owner and repo must be set")
}

refName, err := cmd.Flags().GetString("ref-name")
if err != nil {
return err
}
if len(refName) == 0 {
return errors.New("ref-name must be set")
}

refType, err := cmd.Flags().GetString("ref-type")
if err != nil {
return err
}
if len(refType) == 0 {
return errors.New("ref-type must be set")
}

fileName := filepath.Base(path)
output, err := cmd.Flags().GetString("output")
if err != nil {
return err
}

if len(output) == 0 {
fileName = output
}

opjResp, err := client.GetObject(ctx, owner, repo, &api.GetObjectParams{
// Type type indicate to retrieve from wip/branch/tag, default branch
Type: api.RefType(refType),
RefName: refName,
Path: path,
})
if err != nil {
return err
}

headObj, err := api.ParseGetObjectResponse(opjResp)
if err != nil {
return err
}

return os.WriteFile(fileName, headObj.Body, 0666)
},
}

func init() {
rootCmd.AddCommand(uploadCmd)

uploadCmd.Flags().String("path", "", "path of files to upload")
uploadCmd.Flags().String("owner", "", "owner")
uploadCmd.Flags().String("repo", "", "repo")
uploadCmd.Flags().String("refName", "main", "branch name")
uploadCmd.Flags().String("uploadPath", "", "path to save in server")
uploadCmd.Flags().String("ref-name", "main", "branch name")
uploadCmd.Flags().String("upload-path", "", "path to save in server")
uploadCmd.Flags().Bool("replace", true, "path to save in server")
uploadCmd.Flags().Bool("ignoreRootName", false, "ignore root name")
uploadCmd.Flags().Bool("ignore-root-name", false, "ignore root name")

rootCmd.AddCommand(downloadCmd)
downloadCmd.Flags().String("path", "", "path of files to upload")
downloadCmd.Flags().String("owner", "", "owner")
downloadCmd.Flags().String("repo", "", "repo")
downloadCmd.Flags().String("ref-name", "main", "branch name")
downloadCmd.Flags().String("ref-type", "branch", "reference type")
downloadCmd.Flags().String("output", "branch", "reference type")
}
2 changes: 1 addition & 1 deletion cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func GetClient(cmd *cobra.Command) (*api.Client, error) {
url := cmd.Flags().Lookup("url").Value.String()
url := cmd.Flags().Lookup("url").Value.String() + "/api/v1"
ak := cmd.Flags().Lookup("ak").Value.String()
sk := cmd.Flags().Lookup("sk").Value.String()

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ func init() {
rootCmd.PersistentFlags().String("user", "", "user name")
rootCmd.PersistentFlags().String("password", "", "password")

rootCmd.PersistentFlags().String("url", "https://127.0.0.1:34913", "url")
rootCmd.PersistentFlags().String("url", "http://127.0.0.1:34913", "url")

}
2 changes: 1 addition & 1 deletion controller/object_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes

err = validator.ValidateObjectPath(params.Path)
if err != nil {
w.BadRequest(err.Error())
w.BadRequest("%s %s", params.Path, err.Error())
return
}

Expand Down
2 changes: 1 addition & 1 deletion controller/validator/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestValidateUsername(t *testing.T) {

func TestValidateObjectPath(t *testing.T) {
//Validate Obj Path
validObjectPaths := []string{"path/to/object", "file.txt", "folder/file.txt", "我的图片.png", "我的文件/我的应用.exe", "私のビデオ.mp3", "/video.mp3", "/path/pic.png"}
validObjectPaths := []string{"path/to/object", "/data/housing-simplified-1.csv", "file.txt", "folder/file.txt", "我的图片.png", "我的文件/我的应用.exe", "私のビデオ.mp3", "/video.mp3", "/path/pic.png"}
for _, path := range validObjectPaths {
err := ValidateObjectPath(path)
if err != nil {
Expand Down

0 comments on commit 6f2a1a3

Please sign in to comment.