Ejemplo n.º 1
0
Archivo: rm.go Proyecto: dropbox/dbxcli
func rm(cmd *cobra.Command, args []string) error {
	if len(args) < 1 {
		return errors.New("rm: missing operand")
	}

	force, err := cmd.Flags().GetBool("force")
	if err != nil {
		return err
	}

	deletePaths := []string{}
	dbx := files.New(config)

	// Validate remove paths before executing removal
	for i := range args {
		path, err := validatePath(args[i])
		if err != nil {
			return err
		}

		pathMetaData, err := getFileMetadata(dbx, path)
		if err != nil {
			return err
		}

		if _, ok := pathMetaData.(*files.FileMetadata); !ok {
			folderArg := files.NewListFolderArg(path)
			res, err := dbx.ListFolder(folderArg)
			if err != nil {
				return err
			}
			if len(res.Entries) != 0 && !force {
				return fmt.Errorf("rm: cannot remove ‘%s’: Directory not empty, use `--force` or `-f` to proceed", path)
			}
		}
		deletePaths = append(deletePaths, path)
	}

	// Execute removals
	for _, path := range deletePaths {
		arg := files.NewDeleteArg(path)

		if _, err = dbx.Delete(arg); err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 2
0
Archivo: ls.go Proyecto: dropbox/dbxcli
func ls(cmd *cobra.Command, args []string) (err error) {
	path := ""
	if len(args) > 0 {
		if path, err = validatePath(args[0]); err != nil {
			return err
		}
	}
	dbx := files.New(config)

	arg := files.NewListFolderArg(path)

	res, err := dbx.ListFolder(arg)
	var entries []files.IsMetadata
	if err != nil {
		switch e := err.(type) {
		case files.ListFolderAPIError:
			// Don't treat a "not_folder" error as fatal; recover by sending a
			// get_metadata request for the same path and using that response instead.
			if e.EndpointError.Path.Tag == files.LookupErrorNotFolder {
				var metaRes files.IsMetadata
				metaRes, err = getFileMetadata(dbx, path)
				entries = []files.IsMetadata{metaRes}
			} else {
				return err
			}
		default:
			return err
		}

		// Return if there's an error other than "not_folder" or if the follow-up
		// metadata request fails.
		if err != nil {
			return err
		}
	} else {
		entries = res.Entries

		for res.HasMore {
			arg := files.NewListFolderContinueArg(res.Cursor)

			res, err = dbx.ListFolderContinue(arg)
			if err != nil {
				return err
			}

			entries = append(entries, res.Entries...)
		}
	}

	long, _ := cmd.Flags().GetBool("long")
	if long {
		w := new(tabwriter.Writer)
		w.Init(os.Stdout, 4, 8, 1, ' ', 0)
		fmt.Fprintf(w, "Revision\tSize\tLast modified\tPath\n")
		for _, entry := range entries {
			switch f := entry.(type) {
			case *files.FileMetadata:
				printFileMetadata(w, f, long)
			case *files.FolderMetadata:
				printFolderMetadata(w, f, long)
			}
		}
		w.Flush()
	} else {
		entryNames := listOfEntryNames(entries)
		golumns.Display(entryNames)
	}

	return err
}