Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Commit

Permalink
Code reorg.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Nov 11, 2014
1 parent 66e3d0e commit 3144e06
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 98 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

## Installation

go get github.com/rakyll/drive
go get github.com/rakyll/drive/cmd/drive

Use `drive help` for further reference.

$ drive init [path]
$ drive init [path]
$ drive pull [-r -no-prompt path] # pulls from remote
$ drive push [-r -no-prompt path] # pushes to the remote
$ drive diff [path] # outputs a diff of local and remote
Expand All @@ -27,7 +27,7 @@ Background sync is not just hard, it's stupid. My technical and philosophical ra

`drive` is not a sync deamon, it provides:

* Upstreaming and downstreaming. Unlike a sync command, we provide pull and push actions. User has opportunity to decide what to do with their local copy and when. Do some changes, either push it to remote or revert it to the remote version. Perform these actions with user prompt.
* Upstreaming and downstreaming. Unlike a sync command, we provide pull and push actions. User has opportunity to decide what to do with their local copy and when. Do some changes, either push it to remote or revert it to the remote version. Perform these actions with user prompt.

$ echo "hello" > hello.txt
$ drive push # pushes hello.txt to Google Drive
Expand Down
32 changes: 15 additions & 17 deletions commands/changes.go → changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package commands
package drive

import (
"fmt"
"path"
"sync"
"strings"

"github.com/rakyll/drive/types"
"sync"
)

type dirList struct {
remote *types.File
local *types.File
remote *File
local *File
}

func (d *dirList) Name() string {
Expand All @@ -36,14 +34,14 @@ func (d *dirList) Name() string {
}

func (g *Commands) resolveChangeListRecv(
isPush bool, p string, r *types.File, l *types.File) (cl []*types.Change, err error) {
var change *types.Change
isPush bool, p string, r *File, l *File) (cl []*Change, err error) {
var change *Change
if isPush {
change = &types.Change{Path: p, Src: l, Dest: r}
change = &Change{Path: p, Src: l, Dest: r}
} else {
change = &types.Change{Path: p, Src: r, Dest: l}
change = &Change{Path: p, Src: r, Dest: l}
}
if change.Op() != types.OpNone {
if change.Op() != OpNone {
cl = append(cl, change)
}
if !g.opts.IsRecursive {
Expand All @@ -59,15 +57,15 @@ func (g *Commands) resolveChangeListRecv(
}

// look-up for children
var localChildren []*types.File
var localChildren []*File
if l != nil {
localChildren, err = list(g.context, p)
if err != nil {
return
}
}

var remoteChildren []*types.File
var remoteChildren []*File
if r != nil {
if remoteChildren, err = g.rem.FindByParentId(r.Id); err != nil {
return
Expand All @@ -79,7 +77,7 @@ func (g *Commands) resolveChangeListRecv(
var wg sync.WaitGroup
wg.Add(len(dirlist))
for _, l := range dirlist {
go func(wg *sync.WaitGroup, isPush bool, cl *[]*types.Change, p string, l *dirList) {
go func(wg *sync.WaitGroup, isPush bool, cl *[]*Change, p string, l *dirList) {
defer wg.Done()
childChanges, _ := g.resolveChangeListRecv(isPush, path.Join(p, l.Name()), l.remote, l.local)
*cl = append(*cl, childChanges...)
Expand All @@ -89,7 +87,7 @@ func (g *Commands) resolveChangeListRecv(
return cl, nil
}

func merge(remotes, locals []*types.File) (merged []*dirList) {
func merge(remotes, locals []*File) (merged []*dirList) {
for _, r := range remotes {
list := &dirList{remote: r}
// look for local
Expand All @@ -109,9 +107,9 @@ func merge(remotes, locals []*types.File) (merged []*dirList) {
return
}

func printChangeList(changes []*types.Change, isNoPrompt bool) bool {
func printChangeList(changes []*Change, isNoPrompt bool) bool {
for _, c := range changes {
if c.Op() != types.OpNone {
if c.Op() != OpNone {
fmt.Println(c.Symbol(), c.Path)
}
}
Expand Down
12 changes: 6 additions & 6 deletions main.go → cmd/drive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"path/filepath"

"github.com/rakyll/command"
"github.com/rakyll/drive/commands"
"github.com/rakyll/drive"
"github.com/rakyll/drive/config"
)

Expand Down Expand Up @@ -52,7 +52,7 @@ func (cmd *initCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
}

func (cmd *initCmd) Run(args []string) {
exitWithError(commands.New(initContext(args), nil).Init())
exitWithError(drive.New(initContext(args), nil).Init())
}

type pullCmd struct {
Expand All @@ -68,7 +68,7 @@ func (cmd *pullCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *pullCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(commands.New(context, &commands.Options{
exitWithError(drive.New(context, &drive.Options{
Path: path,
IsRecursive: *cmd.isRecursive,
IsNoPrompt: *cmd.isNoPrompt,
Expand All @@ -88,7 +88,7 @@ func (cmd *pushCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *pushCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(commands.New(context, &commands.Options{
exitWithError(drive.New(context, &drive.Options{
Path: path,
IsRecursive: *cmd.isRecursive,
IsNoPrompt: *cmd.isNoPrompt,
Expand All @@ -103,7 +103,7 @@ func (cmd *diffCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *diffCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(commands.New(context, &commands.Options{
exitWithError(drive.New(context, &drive.Options{
Path: path,
}).Diff())
}
Expand All @@ -116,7 +116,7 @@ func (cmd *publishCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *publishCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(commands.New(context, &commands.Options{
exitWithError(drive.New(context, &drive.Options{
Path: path,
}).Publish())
}
Expand Down
9 changes: 4 additions & 5 deletions commands/commands.go → commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package commands
package drive

import (
"errors"
"path"

"github.com/cheggaaa/pb"
"github.com/rakyll/drive/config"
"github.com/rakyll/drive/remote"
)

var (
Expand All @@ -36,16 +35,16 @@ type Options struct {

type Commands struct {
context *config.Context
rem *remote.Remote
rem *Remote
opts *Options

progress *pb.ProgressBar
}

func New(context *config.Context, opts *Options) *Commands {
var r *remote.Remote
var r *Remote
if context != nil {
r = remote.New(context)
r = NewRemoteContext(context)
}
if opts != nil {
// should always start with /
Expand Down
2 changes: 1 addition & 1 deletion commands/diff.go → diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package commands
package drive

func (g *Commands) Diff() error {
panic("not implemented")
Expand Down
8 changes: 2 additions & 6 deletions commands/init.go → init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
"github.com/rakyll/drive/remote"
)
package drive

func (g *Commands) Init() (err error) {
var refresh string
// TODO: read from env variable.
g.context.ClientId = "354790962074-7rrlnuanmamgg1i4feed12dpuq871bvd.apps.googleusercontent.com"
g.context.ClientSecret = "RHjKdah8RrHFwu6fcc0uEVCw"
if refresh, err = remote.RetrieveRefreshToken(g.context); err != nil {
if refresh, err = RetrieveRefreshToken(g.context); err != nil {
return
}
g.context.RefreshToken = refresh
Expand Down
6 changes: 2 additions & 4 deletions commands/publish.go → publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package commands
package drive

import (
"fmt"

"github.com/rakyll/drive/types"
)

func (c *Commands) Publish() (err error) {
var file *types.File
var file *File
var link string
if file, err = c.rem.FindByPath(c.opts.Path); err != nil {
return
Expand Down
30 changes: 14 additions & 16 deletions commands/pull.go → pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package commands
package drive

import (
"fmt"
"io"
"os"
"path/filepath"
"sync"

"github.com/rakyll/drive/types"
)

const (
Expand All @@ -32,17 +30,17 @@ const (
// directory, it recursively pulls from the remote if there are remote changes.
// It doesn't check if there are remote changes if isForce is set.
func (g *Commands) Pull() (err error) {
var r, l *types.File
var r, l *File
if r, err = g.rem.FindByPath(g.opts.Path); err != nil {
return
}
absPath := g.context.AbsPathOf(g.opts.Path)
localinfo, _ := os.Stat(absPath)
if localinfo != nil {
l = types.NewLocalFile(absPath, localinfo)
l = NewLocalFile(absPath, localinfo)
}

var cl []*types.Change
var cl []*Change
fmt.Println("Resolving...")
if cl, err = g.resolveChangeListRecv(false, g.opts.Path, r, l); err != nil {
return
Expand All @@ -54,15 +52,15 @@ func (g *Commands) Pull() (err error) {
return
}

func (g *Commands) playPullChangeList(cl []*types.Change) (err error) {
var next []*types.Change
func (g *Commands) playPullChangeList(cl []*Change) (err error) {
var next []*Change
g.taskStart(len(cl))

for {
if len(cl) > maxNumOfConcPullTasks {
next, cl = cl[:maxNumOfConcPullTasks], cl[maxNumOfConcPullTasks:len(cl)]
} else {
next, cl = cl, []*types.Change{}
next, cl = cl, []*Change{}
}
if len(next) == 0 {
break
Expand All @@ -73,11 +71,11 @@ func (g *Commands) playPullChangeList(cl []*types.Change) (err error) {
// TODO: add timeouts
for _, c := range next {
switch c.Op() {
case types.OpMod:
case OpMod:
go g.localMod(&wg, c)
case types.OpAdd:
case OpAdd:
go g.localAdd(&wg, c)
case types.OpDelete:
case OpDelete:
go g.localDelete(&wg, c)
}
}
Expand All @@ -88,7 +86,7 @@ func (g *Commands) playPullChangeList(cl []*types.Change) (err error) {
return err
}

func (g *Commands) localMod(wg *sync.WaitGroup, change *types.Change) (err error) {
func (g *Commands) localMod(wg *sync.WaitGroup, change *Change) (err error) {
defer g.taskDone()
defer wg.Done()
destAbsPath := g.context.AbsPathOf(change.Path)
Expand All @@ -101,7 +99,7 @@ func (g *Commands) localMod(wg *sync.WaitGroup, change *types.Change) (err error
return os.Chtimes(destAbsPath, change.Src.ModTime, change.Src.ModTime)
}

func (g *Commands) localAdd(wg *sync.WaitGroup, change *types.Change) (err error) {
func (g *Commands) localAdd(wg *sync.WaitGroup, change *Change) (err error) {
defer g.taskDone()
defer wg.Done()
destAbsPath := g.context.AbsPathOf(change.Path)
Expand All @@ -119,13 +117,13 @@ func (g *Commands) localAdd(wg *sync.WaitGroup, change *types.Change) (err error
return os.Chtimes(destAbsPath, change.Src.ModTime, change.Src.ModTime)
}

func (g *Commands) localDelete(wg *sync.WaitGroup, change *types.Change) (err error) {
func (g *Commands) localDelete(wg *sync.WaitGroup, change *Change) (err error) {
defer g.taskDone()
defer wg.Done()
return os.RemoveAll(change.Dest.BlobAt)
}

func (g *Commands) download(change *types.Change) (err error) {
func (g *Commands) download(change *Change) (err error) {
destAbsPath := g.context.AbsPathOf(change.Path)
var fo *os.File
fo, err = os.Create(destAbsPath)
Expand Down
Loading

0 comments on commit 3144e06

Please sign in to comment.