-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrivet.go
133 lines (110 loc) · 3.66 KB
/
rivet.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
package main
import (
"fmt"
"os"
"github.com/danlamanna/rivet/commands"
"github.com/danlamanna/rivet/config"
"github.com/danlamanna/rivet/girder"
"github.com/danlamanna/rivet/templates"
"github.com/danlamanna/rivet/version"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("rivet", "sync files to girder")
auth = app.Flag("auth", "Authentication credentials, can be username:password, a token, or an api key`").Envar("RIVET_AUTH").Short('a').String()
url = app.Flag("url", "URL of the girder instance, e.g. data.kitware.com, somedomain.com/api/v1").Envar("RIVET_URL").Short('u').String()
verbose = app.Flag("verbose", "Increase verbosity, can be passed up to two times.").Short('v').Counter()
// hidden global flags
noConfigFile = app.Flag("no-config", "Skip loading a configuration file").Bool()
// configure command
configure = app.Command("configure", "")
// sync command
sync = app.Command("sync", "sync a local directory to or from a girder folder")
source = sync.Arg("source", "source directory or girder folder").Required().String()
dest = sync.Arg("dest", "dest directory or girder folder").Required().String()
// version command
versionCmd = app.Command("version", "")
apiCreateFolderCmd = app.Command("api-create-folder", "")
apiDest = apiCreateFolderCmd.Arg("dest", "").Required().String()
apiPath = apiCreateFolderCmd.Arg("path", "").Required().String()
)
func main() {
app.HelpFlag.Short('h')
app.UsageTemplate(templates.DefaultUsageTemplate)
app.Version(version.Version)
// kingpin doesn't allow help for subcommands, so we hook in before parsing
// to possible show help pages
if len(os.Args) >= 3 && os.Args[1] == "help" && os.Args[2] == "configure" {
fmt.Print(fmt.Errorf(templates.ConfigureUsageTemplate))
os.Exit(1)
} else if len(os.Args) >= 3 && os.Args[1] == "help" && os.Args[2] == "sync" {
fmt.Print(fmt.Errorf(templates.SyncUsageTemplate))
os.Exit(1)
}
res, _ := app.Parse(os.Args[1:])
// setup context/logger
ctx := new(girder.Context)
ctx.Logger = logrus.New()
ctx.Logger.SetFormatter(&log.TextFormatter{
DisableLevelTruncation: true,
FullTimestamp: true,
})
if *verbose >= 2 {
ctx.Logger.Level = logrus.TraceLevel
} else if *verbose == 1 {
ctx.Logger.Level = logrus.DebugLevel
} else {
ctx.Logger.Level = logrus.InfoLevel
}
// fill in auth/url, defaulting to the "default" profile
if !*noConfigFile {
profile, err := config.ReadDefaultProfile(ctx)
if err != nil {
log.Fatal(err)
}
if profile != nil {
ctx.Logger.Debug("loaded credentials from configuration file")
ctx.Auth = profile.Auth
ctx.URL = profile.URL
}
}
// override default profile with envvars/flags
if *auth != "" {
ctx.Auth = *auth
}
if *url != "" {
ctx.URL = *url
}
if res == "" {
fmt.Printf(`usage: rivet [options] [subcommand] [arguments]
To see help text, you can run:
rivet help
rivet help <subcommand>`)
os.Exit(1)
}
if newerVersion, _ := version.IsNewVersionAvailable(); newerVersion != "" {
fmt.Printf("Your version of rivet (v%s) is out of date.\n", version.Version)
fmt.Printf("Download the newest version (v%s) from https://github.com/danlamanna/rivet/releases.\n\n", newerVersion)
}
switch res {
case "configure":
commands.Configure(ctx)
case "sync":
if ctx.URL == "" {
fmt.Println("See --url flag")
os.Exit(1)
}
validURL, err := girder.GetValidURL(ctx.URL)
ctx.URL = validURL
if err != nil {
log.Fatal(err)
}
commands.Sync(ctx, source, dest)
case "version":
commands.Version()
case "api-create-folder":
commands.APICreateFolder(ctx, *apiDest, *apiPath)
}
}