forked from rakyll/drive
/
main.go
158 lines (133 loc) · 3.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2013 Google Inc. All Rights Reserved.
//
// 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 contains the main entry point of gd.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/rakyll/command"
"github.com/rakyll/drive/commands"
"github.com/rakyll/drive/config"
)
var context *config.Context
const (
descInit = "inits a directory and authenticates user"
descPull = "pulls remote changes from google drive"
descPush = "push local changes to google drive"
descDiff = "compares a local file with remote"
descPublish = "publishes a file and prints its publicly available url"
)
func main() {
command.On("init", descInit, &initCmd{})
command.On("pull", descPull, &pullCmd{})
command.On("push", descPush, &pushCmd{})
command.On("diff", descDiff, &diffCmd{})
command.On("pub", descPublish, &publishCmd{})
command.ParseAndRun()
}
type initCmd struct{}
func (cmd *initCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
return fs
}
func (cmd *initCmd) Run(args []string) {
exitWithError(commands.New(initContext(args), nil).Init())
}
type pullCmd struct {
isRecursive *bool
isNoPrompt *bool
}
func (cmd *pullCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
cmd.isRecursive = fs.Bool("r", true, "performs the pull action recursively")
cmd.isNoPrompt = fs.Bool("no-prompt", false, "shows no prompt before applying the pull action")
return fs
}
func (cmd *pullCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(commands.New(context, &commands.Options{
Path: path,
IsRecursive: *cmd.isRecursive,
IsNoPrompt: *cmd.isNoPrompt,
}).Pull())
}
type pushCmd struct {
isRecursive *bool
isNoPrompt *bool
}
func (cmd *pushCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
cmd.isRecursive = fs.Bool("r", true, "performs the push action recursively")
cmd.isNoPrompt = fs.Bool("no-prompt", false, "shows no prompt before applying the push action")
return fs
}
func (cmd *pushCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(commands.New(context, &commands.Options{
Path: path,
IsRecursive: *cmd.isRecursive,
IsNoPrompt: *cmd.isNoPrompt,
}).Push())
}
type diffCmd struct{}
func (cmd *diffCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
return fs
}
func (cmd *diffCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(commands.New(context, &commands.Options{
Path: path,
}).Diff())
}
type publishCmd struct{}
func (cmd *publishCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
return fs
}
func (cmd *publishCmd) Run(args []string) {
context, path := discoverContext(args)
exitWithError(commands.New(context, &commands.Options{
Path: path,
}).Publish())
}
func initContext(args []string) *config.Context {
var err error
context, err = config.Initialize(getContextPath(args))
exitWithError(err)
return context
}
func discoverContext(args []string) (*config.Context, string) {
var err error
context, err = config.Discover(getContextPath(args))
exitWithError(err)
relPath := ""
if len(args) > 0 {
relPath, err = filepath.Rel(context.AbsPath, args[0])
}
exitWithError(err)
return context, relPath
}
func getContextPath(args []string) (contextPath string) {
if len(args) > 0 {
contextPath = args[0]
}
if contextPath == "" {
contextPath, _ = os.Getwd()
}
return
}
func exitWithError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}