Skip to content

Commit 5576f2a

Browse files
banoolMartinDelille
authored andcommitted
Polish, commentating, renaming
1 parent aee39f1 commit 5576f2a

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

cmd/share-createlink.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Copyright © 2016 Dropbox, Inc.
1+
// Copyright © 2017 Dropbox, Inc.
2+
// Author: Daniel Porteous
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
45
// you may not use this file except in compliance with the License.
@@ -28,10 +29,10 @@ import (
2829
)
2930

3031
/**
31-
Try to get the share link for a file if it already exists.
32-
If it doesn't make a new share link for it.
33-
*/
34-
func shareLink(cmd *cobra.Command, args []string) (err error) {
32+
** Try to get the share link for a file if it already exists.
33+
** If it doesn't make a new share link for it.
34+
*/
35+
func getShareLink(cmd *cobra.Command, args []string) (err error) {
3536
if len(args) != 1 {
3637
printShareLinkUsage()
3738
return
@@ -55,7 +56,7 @@ func shareLink(cmd *cobra.Command, args []string) (err error) {
5556
return
5657
}
5758

58-
print("File / folder does not yet have a sharelink, creating one...\n")
59+
// print("File / folder does not yet have a sharelink, creating one...\n")
5960

6061
// The file had no share link, let's get it.
6162
getNewLink(dbx, path)
@@ -64,9 +65,13 @@ func shareLink(cmd *cobra.Command, args []string) (err error) {
6465
}
6566

6667
func printShareLinkUsage() {
67-
fmt.Printf("Usage: %s share createlink [file / folder path]\n", os.Args[0])
68+
fmt.Printf("Usage: %s share getlink [file / folder path]\n", os.Args[0])
6869
}
6970

71+
/*
72+
** Try to get an existing share link for a file / folder.
73+
** It returns true if the file / folder had a link. Otherwise it returns false.
74+
*/
7075
func getExistingLink(dbx sharing.Client, path string) bool {
7176
// Remove the Dropbox folder from the start.
7277
path = strings.Replace(path, getDropboxFolder(), "", 1)
@@ -83,8 +88,13 @@ func getExistingLink(dbx sharing.Client, path string) bool {
8388
return false
8489
}
8590

91+
/*
92+
** Create and print a link for file / folder that doesn't yet have one.
93+
**
94+
** CreateSharedLinkWithSettings doesn't allow pending uploads,
95+
** so we use the partially deprecated CreateSharedLink.
96+
*/
8697
func getNewLink(dbx sharing.Client, path string) bool {
87-
// CreateSharedLinkWithSettings is cooked, I won't use it.
8898
arg := sharing.NewCreateSharedLinkArg(strings.Replace(path, getDropboxFolder(), "", 1))
8999
// Get the sharelink even if the file isn't fully uploaded yet.
90100
arg.PendingUpload = new(sharing.PendingUploadMode)
@@ -105,10 +115,11 @@ func getNewLink(dbx sharing.Client, path string) bool {
105115
fmt.Printf("%+v\n", err)
106116
return false
107117
}
108-
fmt.Printf("%s %s\n", res.Path[1:], res.Url)
118+
fmt.Printf("%s\t%s\n", res.Path[1:], res.Url)
109119
return true
110120
}
111121

122+
/* Return the path of the Dropbox folder. */
112123
func getDropboxFolder() string {
113124
// I should be using a JSON parser here but it's a pain in Go.
114125
usr, _ := user.Current()
@@ -123,6 +134,7 @@ func getDropboxFolder() string {
123134
return strings.Split(strings.Split(string(raw), "\"path\": \"")[1], "\"")[0]
124135
}
125136

137+
/* Check whether a file / folder exists. */
126138
func exists(path string) (bool, error) {
127139
_, err := os.Stat(path)
128140
if err == nil {

cmd/share.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ var shareListCmd = &cobra.Command{
2828
Short: "List shared things",
2929
}
3030

31-
var shareLinkCmd = &cobra.Command{
32-
Use: "createlink",
33-
Short: "Get share link for file / folder",
34-
RunE: shareLink,
31+
var shareGetLinkCmd = &cobra.Command{
32+
Use: "getlink",
33+
Short: "Get share link for file / folder (create if it doesn't exist)",
34+
RunE: getShareLink,
3535
}
3636

3737
func init() {
3838
RootCmd.AddCommand(shareCmd)
3939
shareCmd.AddCommand(shareListCmd)
40-
shareCmd.AddCommand(shareLinkCmd)
40+
shareCmd.AddCommand(shareGetLinkCmd)
4141
}

0 commit comments

Comments
 (0)