From 3e1a8fe8b3270f04ed47631eb1818cfacec6c017 Mon Sep 17 00:00:00 2001 From: Daniel Porteous Date: Tue, 2 Jan 2018 21:50:31 +1100 Subject: [PATCH 1/7] Early partially working functionality for createlink --- cmd/root.go | 4 +-- cmd/share-link.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/share.go | 7 ++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 cmd/share-link.go diff --git a/cmd/root.go b/cmd/root.go index 46c07e2..b03e956 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -40,8 +40,8 @@ const ( ) var ( - personalAppKey = "mvhz183vwqibe7q" - personalAppSecret = "q0kquhzgetjwcz1" + personalAppKey = "wwdxi7cr0cdd11m" + personalAppSecret = "xi9zp6t27kyy78v" teamAccessAppKey = "zud1va492pnehkc" teamAccessAppSecret = "p3ginm1gy0kmj54" teamManageAppKey = "xxe04eai4wmlitv" diff --git a/cmd/share-link.go b/cmd/share-link.go new file mode 100644 index 0000000..cfcc914 --- /dev/null +++ b/cmd/share-link.go @@ -0,0 +1,86 @@ +// Copyright © 2016 Dropbox, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "fmt" + "os" + "path/filepath" + "reflect" + + "github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/sharing" + "github.com/spf13/cobra" +) + +/** +Try to get the share link for a file if it already exists. +If it doesn't make a new share link for it. +*/ +func shareLink(cmd *cobra.Command, args []string) (err error) { + if len(args) != 1 { + printShareLinkUsage() + return + } + dbx := sharing.New(config) + + // TODO: Remove the /Users/Dropbox part from the path + path, err := filepath.Abs(args[0]) + if err != nil { + return + } + + arg := sharing.ListSharedLinksArg{Path: path} + // This method can be called with a path and just get that share link. + res, err := dbx.ListSharedLinks(&arg) + if err != nil || len(res.Links) == 0 { + print("File / folder does not yet have a sharelink, creating one...\n") + } else { + fmt.Printf("%+v\n", res) + printLinks(res.Links) + return + } + + // The file had no share link, let's get it. + arg2 := sharing.NewCreateSharedLinkWithSettingsArg(path) + res2, err2 := dbx.CreateSharedLinkWithSettings(arg2) + if err2 != nil { + return + } + + //fmt.Printf("%+v\n", res2) + print(reflect.TypeOf(&res2).String()) + + /* + printLinks(res.Links) + + for res.HasMore { + arg = sharing.NewListSharedLinksArg() + arg.Cursor = res.Cursor + + res, err = dbx.ListSharedLinks(arg) + if err != nil { + return + } + + printLinks(res.Links) + } + */ + + return +} + +func printShareLinkUsage() { + fmt.Printf("Usage: %s share createlink [file / folder path]\n", os.Args[0]) +} diff --git a/cmd/share.go b/cmd/share.go index 3fb32fe..af5d0ea 100644 --- a/cmd/share.go +++ b/cmd/share.go @@ -28,7 +28,14 @@ var shareListCmd = &cobra.Command{ Short: "List shared things", } +var shareLinkCmd = &cobra.Command{ + Use: "createlink", + Short: "Get share link for file / folder", + RunE: shareLink, +} + func init() { RootCmd.AddCommand(shareCmd) shareCmd.AddCommand(shareListCmd) + shareCmd.AddCommand(shareLinkCmd) } From 6750176dfbb34a8830845d4498294b91719bc694 Mon Sep 17 00:00:00 2001 From: Daniel Porteous Date: Tue, 2 Jan 2018 22:21:04 +1100 Subject: [PATCH 2/7] Accept a file path as a createlink argument --- cmd/share-link.go | 71 ++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/cmd/share-link.go b/cmd/share-link.go index cfcc914..a369d2e 100644 --- a/cmd/share-link.go +++ b/cmd/share-link.go @@ -16,14 +16,19 @@ package cmd import ( "fmt" + "io/ioutil" "os" + "os/user" + "path" "path/filepath" - "reflect" + "strings" "github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/sharing" "github.com/spf13/cobra" ) +const DefaultDropboxName = "/Users/daniel/Dropbox" + /** Try to get the share link for a file if it already exists. If it doesn't make a new share link for it. @@ -34,53 +39,57 @@ func shareLink(cmd *cobra.Command, args []string) (err error) { return } dbx := sharing.New(config) - - // TODO: Remove the /Users/Dropbox part from the path path, err := filepath.Abs(args[0]) if err != nil { return } + // Remove the Dropbox folder from the start. + path = strings.Replace(path, getDropboxFolder(), "", 1) + + // Try to get a link if it already exists. + getExistingLink(dbx, path) + + // The file had no share link, let's get it. + getNewLink(dbx, path) + + return +} +func printShareLinkUsage() { + fmt.Printf("Usage: %s share createlink [file / folder path]\n", os.Args[0]) +} + +func getExistingLink(dbx sharing.Client, path string) { arg := sharing.ListSharedLinksArg{Path: path} // This method can be called with a path and just get that share link. res, err := dbx.ListSharedLinks(&arg) if err != nil || len(res.Links) == 0 { print("File / folder does not yet have a sharelink, creating one...\n") } else { - fmt.Printf("%+v\n", res) printLinks(res.Links) return } +} - // The file had no share link, let's get it. - arg2 := sharing.NewCreateSharedLinkWithSettingsArg(path) - res2, err2 := dbx.CreateSharedLinkWithSettings(arg2) - if err2 != nil { +func getNewLink(dbx sharing.Client, path string) { + arg := sharing.NewCreateSharedLinkWithSettingsArg(path) + res, err := dbx.CreateSharedLinkWithSettings(arg) + if err != nil { return } - - //fmt.Printf("%+v\n", res2) - print(reflect.TypeOf(&res2).String()) - - /* - printLinks(res.Links) - - for res.HasMore { - arg = sharing.NewListSharedLinksArg() - arg.Cursor = res.Cursor - - res, err = dbx.ListSharedLinks(arg) - if err != nil { - return - } - - printLinks(res.Links) - } - */ - - return + print(res) } -func printShareLinkUsage() { - fmt.Printf("Usage: %s share createlink [file / folder path]\n", os.Args[0]) +func getDropboxFolder() string { + // I should be using a JSON parser here but it's a pain in Go. + usr, _ := user.Current() + homedir := usr.HomeDir + infoFilePath := path.Join(homedir, ".dropbox/info.json") + raw, err := ioutil.ReadFile(infoFilePath) + if err != nil { + print("Couldn't find Dropbox folder") + return DefaultDropboxName + } + // This is obviously dirty. + return strings.Split(strings.Split(string(raw), "\"path\": \"")[1], "\"")[0] } From 909915b9baf8c419484a444fd962fa40c8c04060 Mon Sep 17 00:00:00 2001 From: Daniel Porteous Date: Tue, 2 Jan 2018 22:48:30 +1100 Subject: [PATCH 3/7] Get a link when it didn't yet exist --- cmd/share-link.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/share-link.go b/cmd/share-link.go index a369d2e..4c6ca66 100644 --- a/cmd/share-link.go +++ b/cmd/share-link.go @@ -47,7 +47,9 @@ func shareLink(cmd *cobra.Command, args []string) (err error) { path = strings.Replace(path, getDropboxFolder(), "", 1) // Try to get a link if it already exists. - getExistingLink(dbx, path) + if getExistingLink(dbx, path) { + return + } // The file had no share link, let's get it. getNewLink(dbx, path) @@ -59,7 +61,7 @@ func printShareLinkUsage() { fmt.Printf("Usage: %s share createlink [file / folder path]\n", os.Args[0]) } -func getExistingLink(dbx sharing.Client, path string) { +func getExistingLink(dbx sharing.Client, path string) bool { arg := sharing.ListSharedLinksArg{Path: path} // This method can be called with a path and just get that share link. res, err := dbx.ListSharedLinks(&arg) @@ -67,17 +69,20 @@ func getExistingLink(dbx sharing.Client, path string) { print("File / folder does not yet have a sharelink, creating one...\n") } else { printLinks(res.Links) - return + return true } + return false } -func getNewLink(dbx sharing.Client, path string) { - arg := sharing.NewCreateSharedLinkWithSettingsArg(path) - res, err := dbx.CreateSharedLinkWithSettings(arg) +func getNewLink(dbx sharing.Client, path string) bool { + // CreateSharedLinkWithSettings is cooked, I won't use it. + arg := sharing.NewCreateSharedLinkArg(path) + res, err := dbx.CreateSharedLink(arg) if err != nil { - return + return false } - print(res) + fmt.Printf("%s %s\n", res.Path[1:], res.Url) + return true } func getDropboxFolder() string { From 952dece8634725b94f1439137c36d4e9ace71879 Mon Sep 17 00:00:00 2001 From: Daniel Porteous Date: Wed, 3 Jan 2018 10:52:42 +1100 Subject: [PATCH 4/7] Allow files / folders pending upload --- cmd/{share-link.go => share-createlink.go} | 49 ++++++++++++++++++---- 1 file changed, 42 insertions(+), 7 deletions(-) rename cmd/{share-link.go => share-createlink.go} (71%) diff --git a/cmd/share-link.go b/cmd/share-createlink.go similarity index 71% rename from cmd/share-link.go rename to cmd/share-createlink.go index 4c6ca66..4ebae7f 100644 --- a/cmd/share-link.go +++ b/cmd/share-createlink.go @@ -27,8 +27,6 @@ import ( "github.com/spf13/cobra" ) -const DefaultDropboxName = "/Users/daniel/Dropbox" - /** Try to get the share link for a file if it already exists. If it doesn't make a new share link for it. @@ -38,19 +36,27 @@ func shareLink(cmd *cobra.Command, args []string) (err error) { printShareLinkUsage() return } + dbx := sharing.New(config) path, err := filepath.Abs(args[0]) if err != nil { return } - // Remove the Dropbox folder from the start. - path = strings.Replace(path, getDropboxFolder(), "", 1) + + // Confirm that the file exists. + exists, err := exists(path) + if !exists || err != nil { + print("The file / folder specified does not exist.\n") + return + } // Try to get a link if it already exists. if getExistingLink(dbx, path) { return } + print("File / folder does not yet have a sharelink, creating one...\n") + // The file had no share link, let's get it. getNewLink(dbx, path) @@ -62,11 +68,14 @@ func printShareLinkUsage() { } func getExistingLink(dbx sharing.Client, path string) bool { + // Remove the Dropbox folder from the start. + path = strings.Replace(path, getDropboxFolder(), "", 1) + arg := sharing.ListSharedLinksArg{Path: path} // This method can be called with a path and just get that share link. res, err := dbx.ListSharedLinks(&arg) if err != nil || len(res.Links) == 0 { - print("File / folder does not yet have a sharelink, creating one...\n") + } else { printLinks(res.Links) return true @@ -76,9 +85,24 @@ func getExistingLink(dbx sharing.Client, path string) bool { func getNewLink(dbx sharing.Client, path string) bool { // CreateSharedLinkWithSettings is cooked, I won't use it. - arg := sharing.NewCreateSharedLinkArg(path) + arg := sharing.NewCreateSharedLinkArg(strings.Replace(path, getDropboxFolder(), "", 1)) + // Get the sharelink even if the file isn't fully uploaded yet. + arg.PendingUpload = new(sharing.PendingUploadMode) + // Determine whether the target is a file or folder. + fi, err := os.Stat(path) + if err != nil { + fmt.Println(err) + return false + } + switch mode := fi.Mode(); { + case mode.IsDir(): + arg.PendingUpload.Tag = sharing.PendingUploadModeFolder + case mode.IsRegular(): + arg.PendingUpload.Tag = sharing.PendingUploadModeFile + } res, err := dbx.CreateSharedLink(arg) if err != nil { + fmt.Printf("%+v\n", err) return false } fmt.Printf("%s %s\n", res.Path[1:], res.Url) @@ -93,8 +117,19 @@ func getDropboxFolder() string { raw, err := ioutil.ReadFile(infoFilePath) if err != nil { print("Couldn't find Dropbox folder") - return DefaultDropboxName + return "" } // This is obviously dirty. return strings.Split(strings.Split(string(raw), "\"path\": \"")[1], "\"")[0] } + +func exists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return true, err +} From 7ced6fb8d48e97bb110f92c10b98a7c2cbeca917 Mon Sep 17 00:00:00 2001 From: Daniel Porteous Date: Wed, 3 Jan 2018 12:38:04 +1100 Subject: [PATCH 5/7] Revert api key inclusion --- cmd/root.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b03e956..46c07e2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -40,8 +40,8 @@ const ( ) var ( - personalAppKey = "wwdxi7cr0cdd11m" - personalAppSecret = "xi9zp6t27kyy78v" + personalAppKey = "mvhz183vwqibe7q" + personalAppSecret = "q0kquhzgetjwcz1" teamAccessAppKey = "zud1va492pnehkc" teamAccessAppSecret = "p3ginm1gy0kmj54" teamManageAppKey = "xxe04eai4wmlitv" From 2900a25da91215f1e006408c7d27f01a8ca276d5 Mon Sep 17 00:00:00 2001 From: Daniel Porteous Date: Wed, 3 Jan 2018 12:49:06 +1100 Subject: [PATCH 6/7] Polish, commentating, renaming --- cmd/share-createlink.go | 30 +++++++++++++++++++++--------- cmd/share.go | 10 +++++----- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/cmd/share-createlink.go b/cmd/share-createlink.go index 4ebae7f..fdc502d 100644 --- a/cmd/share-createlink.go +++ b/cmd/share-createlink.go @@ -1,4 +1,5 @@ -// Copyright © 2016 Dropbox, Inc. +// Copyright © 2017 Dropbox, Inc. +// Author: Daniel Porteous // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -28,10 +29,10 @@ import ( ) /** -Try to get the share link for a file if it already exists. -If it doesn't make a new share link for it. -*/ -func shareLink(cmd *cobra.Command, args []string) (err error) { +** Try to get the share link for a file if it already exists. +** If it doesn't make a new share link for it. + */ +func getShareLink(cmd *cobra.Command, args []string) (err error) { if len(args) != 1 { printShareLinkUsage() return @@ -55,7 +56,7 @@ func shareLink(cmd *cobra.Command, args []string) (err error) { return } - print("File / folder does not yet have a sharelink, creating one...\n") + // print("File / folder does not yet have a sharelink, creating one...\n") // The file had no share link, let's get it. getNewLink(dbx, path) @@ -64,9 +65,13 @@ func shareLink(cmd *cobra.Command, args []string) (err error) { } func printShareLinkUsage() { - fmt.Printf("Usage: %s share createlink [file / folder path]\n", os.Args[0]) + fmt.Printf("Usage: %s share getlink [file / folder path]\n", os.Args[0]) } +/* +** Try to get an existing share link for a file / folder. +** It returns true if the file / folder had a link. Otherwise it returns false. + */ func getExistingLink(dbx sharing.Client, path string) bool { // Remove the Dropbox folder from the start. path = strings.Replace(path, getDropboxFolder(), "", 1) @@ -83,8 +88,13 @@ func getExistingLink(dbx sharing.Client, path string) bool { return false } +/* +** Create and print a link for file / folder that doesn't yet have one. +** +** CreateSharedLinkWithSettings doesn't allow pending uploads, +** so we use the partially deprecated CreateSharedLink. + */ func getNewLink(dbx sharing.Client, path string) bool { - // CreateSharedLinkWithSettings is cooked, I won't use it. arg := sharing.NewCreateSharedLinkArg(strings.Replace(path, getDropboxFolder(), "", 1)) // Get the sharelink even if the file isn't fully uploaded yet. arg.PendingUpload = new(sharing.PendingUploadMode) @@ -105,10 +115,11 @@ func getNewLink(dbx sharing.Client, path string) bool { fmt.Printf("%+v\n", err) return false } - fmt.Printf("%s %s\n", res.Path[1:], res.Url) + fmt.Printf("%s\t%s\n", res.Path[1:], res.Url) return true } +/* Return the path of the Dropbox folder. */ func getDropboxFolder() string { // I should be using a JSON parser here but it's a pain in Go. usr, _ := user.Current() @@ -123,6 +134,7 @@ func getDropboxFolder() string { return strings.Split(strings.Split(string(raw), "\"path\": \"")[1], "\"")[0] } +/* Check whether a file / folder exists. */ func exists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { diff --git a/cmd/share.go b/cmd/share.go index af5d0ea..75cf26a 100644 --- a/cmd/share.go +++ b/cmd/share.go @@ -28,14 +28,14 @@ var shareListCmd = &cobra.Command{ Short: "List shared things", } -var shareLinkCmd = &cobra.Command{ - Use: "createlink", - Short: "Get share link for file / folder", - RunE: shareLink, +var shareGetLinkCmd = &cobra.Command{ + Use: "getlink", + Short: "Get share link for file / folder (create if it doesn't exist)", + RunE: getShareLink, } func init() { RootCmd.AddCommand(shareCmd) shareCmd.AddCommand(shareListCmd) - shareCmd.AddCommand(shareLinkCmd) + shareCmd.AddCommand(shareGetLinkCmd) } From d9631e4cc10172eb8f851833819e3d96a225d05d Mon Sep 17 00:00:00 2001 From: Daniel Porteous Date: Thu, 11 Jan 2018 19:10:05 +1100 Subject: [PATCH 7/7] Address issues raised regarding commenting and code structure --- cmd/share-createlink.go | 56 ++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/cmd/share-createlink.go b/cmd/share-createlink.go index fdc502d..8de1877 100644 --- a/cmd/share-createlink.go +++ b/cmd/share-createlink.go @@ -1,5 +1,4 @@ // Copyright © 2017 Dropbox, Inc. -// Author: Daniel Porteous // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -28,10 +27,8 @@ import ( "github.com/spf13/cobra" ) -/** -** Try to get the share link for a file if it already exists. -** If it doesn't make a new share link for it. - */ +// Try to get the share link for a file if it already exists. +// If it doesn't make a new share link for it. func getShareLink(cmd *cobra.Command, args []string) (err error) { if len(args) != 1 { printShareLinkUsage() @@ -47,17 +44,15 @@ func getShareLink(cmd *cobra.Command, args []string) (err error) { // Confirm that the file exists. exists, err := exists(path) if !exists || err != nil { - print("The file / folder specified does not exist.\n") + fmt.Printf("The file / folder specified (\"%s\") does not exist.\n", path) return } // Try to get a link if it already exists. - if getExistingLink(dbx, path) { + if getExistingLink(dbx, path) != nil { return } - // print("File / folder does not yet have a sharelink, creating one...\n") - // The file had no share link, let's get it. getNewLink(dbx, path) @@ -68,11 +63,9 @@ func printShareLinkUsage() { fmt.Printf("Usage: %s share getlink [file / folder path]\n", os.Args[0]) } -/* -** Try to get an existing share link for a file / folder. -** It returns true if the file / folder had a link. Otherwise it returns false. - */ -func getExistingLink(dbx sharing.Client, path string) bool { +// Try to get an existing share link for a file / folder. +// It returns true if the file / folder had a link. Otherwise it returns false. +func getExistingLink(dbx sharing.Client, path string) (err error) { // Remove the Dropbox folder from the start. path = strings.Replace(path, getDropboxFolder(), "", 1) @@ -80,30 +73,25 @@ func getExistingLink(dbx sharing.Client, path string) bool { // This method can be called with a path and just get that share link. res, err := dbx.ListSharedLinks(&arg) if err != nil || len(res.Links) == 0 { - - } else { - printLinks(res.Links) - return true + return err } - return false + printLinks(res.Links) + return nil } -/* -** Create and print a link for file / folder that doesn't yet have one. -** -** CreateSharedLinkWithSettings doesn't allow pending uploads, -** so we use the partially deprecated CreateSharedLink. - */ -func getNewLink(dbx sharing.Client, path string) bool { - arg := sharing.NewCreateSharedLinkArg(strings.Replace(path, getDropboxFolder(), "", 1)) - // Get the sharelink even if the file isn't fully uploaded yet. - arg.PendingUpload = new(sharing.PendingUploadMode) +// Create and print a link for file / folder that doesn't yet have one. +// CreateSharedLinkWithSettings doesn't allow pending uploads, +// so we use the partially deprecated CreateSharedLink. +func getNewLink(dbx sharing.Client, path string) (err error) { // Determine whether the target is a file or folder. fi, err := os.Stat(path) if err != nil { fmt.Println(err) - return false + return err } + arg := sharing.NewCreateSharedLinkArg(strings.Replace(path, getDropboxFolder(), "", 1)) + // Get the sharelink even if the file isn't fully uploaded yet. + arg.PendingUpload = new(sharing.PendingUploadMode) switch mode := fi.Mode(); { case mode.IsDir(): arg.PendingUpload.Tag = sharing.PendingUploadModeFolder @@ -113,13 +101,13 @@ func getNewLink(dbx sharing.Client, path string) bool { res, err := dbx.CreateSharedLink(arg) if err != nil { fmt.Printf("%+v\n", err) - return false + return err } fmt.Printf("%s\t%s\n", res.Path[1:], res.Url) - return true + return nil } -/* Return the path of the Dropbox folder. */ +// Return the path of the Dropbox folder. func getDropboxFolder() string { // I should be using a JSON parser here but it's a pain in Go. usr, _ := user.Current() @@ -134,7 +122,7 @@ func getDropboxFolder() string { return strings.Split(strings.Split(string(raw), "\"path\": \"")[1], "\"")[0] } -/* Check whether a file / folder exists. */ +// Check whether a file / folder exists. func exists(path string) (bool, error) { _, err := os.Stat(path) if err == nil {