Skip to content

Commit e80d029

Browse files
banoolMartinDelille
authored andcommitted
Address issues raised regarding commenting and code structure
1 parent 5576f2a commit e80d029

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

cmd/share-createlink.go

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Copyright © 2017 Dropbox, Inc.
2-
// Author: Daniel Porteous
32
//
43
// Licensed under the Apache License, Version 2.0 (the "License");
54
// you may not use this file except in compliance with the License.
@@ -28,10 +27,8 @@ import (
2827
"github.com/spf13/cobra"
2928
)
3029

31-
/**
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-
*/
30+
// Try to get the share link for a file if it already exists.
31+
// If it doesn't make a new share link for it.
3532
func getShareLink(cmd *cobra.Command, args []string) (err error) {
3633
if len(args) != 1 {
3734
printShareLinkUsage()
@@ -47,17 +44,15 @@ func getShareLink(cmd *cobra.Command, args []string) (err error) {
4744
// Confirm that the file exists.
4845
exists, err := exists(path)
4946
if !exists || err != nil {
50-
print("The file / folder specified does not exist.\n")
47+
fmt.Printf("The file / folder specified (\"%s\") does not exist.\n", path)
5148
return
5249
}
5350

5451
// Try to get a link if it already exists.
55-
if getExistingLink(dbx, path) {
52+
if getExistingLink(dbx, path) != nil {
5653
return
5754
}
5855

59-
// print("File / folder does not yet have a sharelink, creating one...\n")
60-
6156
// The file had no share link, let's get it.
6257
getNewLink(dbx, path)
6358

@@ -68,42 +63,35 @@ func printShareLinkUsage() {
6863
fmt.Printf("Usage: %s share getlink [file / folder path]\n", os.Args[0])
6964
}
7065

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-
*/
75-
func getExistingLink(dbx sharing.Client, path string) bool {
66+
// Try to get an existing share link for a file / folder.
67+
// It returns true if the file / folder had a link. Otherwise it returns false.
68+
func getExistingLink(dbx sharing.Client, path string) (err error) {
7669
// Remove the Dropbox folder from the start.
7770
path = strings.Replace(path, getDropboxFolder(), "", 1)
7871

7972
arg := sharing.ListSharedLinksArg{Path: path}
8073
// This method can be called with a path and just get that share link.
8174
res, err := dbx.ListSharedLinks(&arg)
8275
if err != nil || len(res.Links) == 0 {
83-
84-
} else {
85-
printLinks(res.Links)
86-
return true
76+
return err
8777
}
88-
return false
78+
printLinks(res.Links)
79+
return nil
8980
}
9081

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-
*/
97-
func getNewLink(dbx sharing.Client, path string) bool {
98-
arg := sharing.NewCreateSharedLinkArg(strings.Replace(path, getDropboxFolder(), "", 1))
99-
// Get the sharelink even if the file isn't fully uploaded yet.
100-
arg.PendingUpload = new(sharing.PendingUploadMode)
82+
// Create and print a link for file / folder that doesn't yet have one.
83+
// CreateSharedLinkWithSettings doesn't allow pending uploads,
84+
// so we use the partially deprecated CreateSharedLink.
85+
func getNewLink(dbx sharing.Client, path string) (err error) {
10186
// Determine whether the target is a file or folder.
10287
fi, err := os.Stat(path)
10388
if err != nil {
10489
fmt.Println(err)
105-
return false
90+
return err
10691
}
92+
arg := sharing.NewCreateSharedLinkArg(strings.Replace(path, getDropboxFolder(), "", 1))
93+
// Get the sharelink even if the file isn't fully uploaded yet.
94+
arg.PendingUpload = new(sharing.PendingUploadMode)
10795
switch mode := fi.Mode(); {
10896
case mode.IsDir():
10997
arg.PendingUpload.Tag = sharing.PendingUploadModeFolder
@@ -113,13 +101,13 @@ func getNewLink(dbx sharing.Client, path string) bool {
113101
res, err := dbx.CreateSharedLink(arg)
114102
if err != nil {
115103
fmt.Printf("%+v\n", err)
116-
return false
104+
return err
117105
}
118106
fmt.Printf("%s\t%s\n", res.Path[1:], res.Url)
119-
return true
107+
return nil
120108
}
121109

122-
/* Return the path of the Dropbox folder. */
110+
// Return the path of the Dropbox folder.
123111
func getDropboxFolder() string {
124112
// I should be using a JSON parser here but it's a pain in Go.
125113
usr, _ := user.Current()
@@ -134,7 +122,7 @@ func getDropboxFolder() string {
134122
return strings.Split(strings.Split(string(raw), "\"path\": \"")[1], "\"")[0]
135123
}
136124

137-
/* Check whether a file / folder exists. */
125+
// Check whether a file / folder exists.
138126
func exists(path string) (bool, error) {
139127
_, err := os.Stat(path)
140128
if err == nil {

0 commit comments

Comments
 (0)