mirror of
https://github.com/Luzifer/cloudbox.git
synced 2024-12-22 18:51:21 +00:00
Implement share command
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
e98a3dc32d
commit
8f77f5dd6d
2 changed files with 58 additions and 0 deletions
|
@ -15,11 +15,13 @@ type commandFunc func() error
|
||||||
|
|
||||||
const (
|
const (
|
||||||
cmdHelp command = "help"
|
cmdHelp command = "help"
|
||||||
|
cmdShare command = "share"
|
||||||
cmdSync command = "sync"
|
cmdSync command = "sync"
|
||||||
cmdWriteConfig command = "write-config"
|
cmdWriteConfig command = "write-config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdFuncs = map[command]commandFunc{
|
var cmdFuncs = map[command]commandFunc{
|
||||||
|
cmdShare: execShare,
|
||||||
cmdSync: execSync,
|
cmdSync: execSync,
|
||||||
cmdWriteConfig: execWriteSampleConfig,
|
cmdWriteConfig: execWriteSampleConfig,
|
||||||
}
|
}
|
||||||
|
|
56
cmd/cloudbox/share.go
Normal file
56
cmd/cloudbox/share.go
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/Luzifer/cloudbox/providers"
|
||||||
|
"github.com/Luzifer/rconfig"
|
||||||
|
)
|
||||||
|
|
||||||
|
func execShare() error {
|
||||||
|
conf, err := loadConfig(false)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to load config")
|
||||||
|
}
|
||||||
|
|
||||||
|
remote, err := providerFromURI(conf.Sync.RemoteURI)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to initialize remote provider")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !remote.Capabilities().Has(providers.CapShare) {
|
||||||
|
return errors.New("Remote provider does not support sharing")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rconfig.Args()) < 3 {
|
||||||
|
return errors.New("No filename provided to share")
|
||||||
|
}
|
||||||
|
|
||||||
|
relativeName := rconfig.Args()[2]
|
||||||
|
providerURL, err := remote.Share(relativeName)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to share file")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !conf.Share.OverrideURI {
|
||||||
|
fmt.Println(providerURL)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl, err := template.New("share_uri").Parse(conf.Share.URITemplate)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to parse URI template")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tpl.Execute(os.Stdout, map[string]interface{}{
|
||||||
|
"file": relativeName,
|
||||||
|
}); err != nil {
|
||||||
|
return errors.Wrap(err, "Unable to render share URI")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in a new issue