-
Notifications
You must be signed in to change notification settings - Fork 5
/
entrypoints.go
152 lines (128 loc) · 3.39 KB
/
entrypoints.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
// Client for accessing Varasto server
package stoclient
import (
"context"
"fmt"
"os"
"time"
"github.com/function61/gokit/logex"
"github.com/function61/gokit/osutil"
"github.com/spf13/cobra"
)
func cloneEntrypoint() *cobra.Command {
rev := ""
cmd := &cobra.Command{
Use: "clone [collectionId] [dirName]",
Short: "Downloads a collection from server to workdir",
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
osutil.ExitIfError(wrapWithStopSupport(func(ctx context.Context) error {
dirName := ""
if len(args) > 1 {
dirName = args[1]
}
parentDir, err := os.Getwd()
if err != nil {
return err
}
return clone(ctx, args[0], rev, parentDir, dirName)
}))
},
}
cmd.Flags().StringVarP(&rev, "rev", "r", rev, "Revision to clone")
return cmd
}
func logEntrypoint() *cobra.Command {
return &cobra.Command{
Use: "log",
Short: "Show changeset log",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cwd, err := os.Getwd()
osutil.ExitIfError(err)
wd, err := NewWorkdirLocation(cwd)
osutil.ExitIfError(err)
for _, item := range wd.manifest.Collection.Changesets {
fmt.Printf(
"changeset: %s\ndate: %s\nsummary: %s\n\n",
item.ID,
item.Created.Format(time.RFC822Z),
fmt.Sprintf(
"(no description) %d create(s) %d update(s) %d delete(s)",
len(item.FilesCreated),
len(item.FilesUpdated),
len(item.FilesDeleted)))
}
},
}
}
func pushEntrypoint() *cobra.Command {
cmd := &cobra.Command{
Use: "push",
Short: "Uploads a collection from workdir to server",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
osutil.ExitIfError(wrapWithStopSupport(pushCurrentWorkdir))
},
}
cmd.AddCommand(&cobra.Command{
Use: "one [collectionId] [file]",
Short: "Uploads a single file to a collection",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
osutil.ExitIfError(wrapWithStopSupport(func(ctx context.Context) error {
return pushOne(ctx, args[0], args[1])
}))
},
})
cmd.AddCommand(bulkUploadScriptEntrypoint())
return cmd
}
func statusEntrypoint() *cobra.Command {
return &cobra.Command{
Use: "status",
Aliases: []string{"st"},
Short: "Shows working directory status compared to the parent revision",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
osutil.ExitIfError(wrapWithStopSupport(func(ctx context.Context) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
wd, err := NewWorkdirLocation(cwd)
if err != nil {
return err
}
ch, err := computeChangeset(ctx, wd, NewBlobDiscoveredNoopListener())
if err != nil {
return err
}
for _, created := range ch.FilesCreated {
fmt.Printf("+ %s\n", created.Path)
}
for _, updated := range ch.FilesUpdated {
fmt.Printf("M %s\n", updated.Path)
}
for _, deleted := range ch.FilesDeleted {
fmt.Printf("- %s\n", deleted)
}
return nil
}))
},
}
}
func Entrypoints() []*cobra.Command {
return []*cobra.Command{
adoptEntrypoint(),
statusEntrypoint(),
rmEntrypoint(),
cloneEntrypoint(),
logEntrypoint(),
pushEntrypoint(),
configEntrypoint(),
}
}
func wrapWithStopSupport(fn func(ctx context.Context) error) error {
return fn(osutil.CancelOnInterruptOrTerminate(logex.StandardLogger()))
}