/
file_push.go
167 lines (148 loc) · 4.14 KB
/
file_push.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
159
160
161
162
163
164
165
166
167
package topics
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/OnitiFR/barry/cmd/barry/client"
"github.com/OnitiFR/barry/common"
"github.com/briandowns/spinner"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
)
var filePushVars struct {
loop bool
path string
filename string
destination string
spinner *spinner.Spinner
previousStatus string
expireDuration time.Duration
}
// filePushCmd represents the file push command
var filePushCmd = &cobra.Command{
Use: "push <project> <file> <destination>",
Short: "Push a project file directly to a remote destination",
Long: `Push any file of any project directly to a remote destination (destination example: a mulch server) without the need for a local copy.`,
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
expire, _ := cmd.Flags().GetString("expire")
expireDuration, err := client.ParseExpiration(expire)
if err != nil {
log.Fatalf("unable to parse expiration: %s", err)
}
filePushVars.filename = args[1]
filePushVars.path = args[0] + "/" + args[1]
filePushVars.destination = args[2]
filePushVars.expireDuration = expireDuration
call := client.GlobalAPI.NewCall("GET", "/file/status", map[string]string{
"file": filePushVars.path,
})
call.JSONCallback = filePushStatusCB
// first request can be slow, let's inform the user we understood what he want
fmt.Printf("requesting %s…\n", filePushVars.filename)
filePushVars.loop = true
for {
call.Do()
if !filePushVars.loop {
break // exit the whole command
}
time.Sleep(3 * time.Second)
}
},
}
func newPushSpinner(subject string) {
if !isatty.IsTerminal(os.Stdout.Fd()) {
return
}
if filePushVars.spinner != nil {
filePushVars.spinner.Stop()
fmt.Println()
}
s := spinner.New(spinner.CharSets[37], 200*time.Millisecond)
s.FinalMSG = fmt.Sprintf("%s: completed", subject)
s.Start()
filePushVars.spinner = s
}
func filePushStatusCB(reader io.Reader, headers http.Header) {
var data common.APIFileStatus
dec := json.NewDecoder(reader)
err := dec.Decode(&data)
if err != nil {
log.Fatal(err.Error())
}
if data.Status == common.APIFileStatusAvailable {
pushDo()
filePushVars.loop = false
return
}
if filePushVars.previousStatus != data.Status {
newPushSpinner(data.Status)
filePushVars.previousStatus = data.Status
}
end := time.Now().Add(data.ETA).Format("2006-01-02 15:04")
msg := fmt.Sprintf("%s: %s (%s)", data.Status, data.ETA, end)
if isatty.IsTerminal(os.Stdout.Fd()) {
filePushVars.spinner.Suffix = " " + msg
} else {
fmt.Println(msg)
}
}
func pushDo() {
call := client.GlobalAPI.NewCall("GET", "/file/push/status", map[string]string{
"file": filePushVars.path,
"destination": filePushVars.destination,
"expire": client.DurationAsSecondsString(filePushVars.expireDuration),
})
call.JSONCallback = pushStatusCB
filePushVars.loop = true
for {
call.Do()
if !filePushVars.loop {
break // exit the whole command
}
time.Sleep(3 * time.Second)
}
}
func pushStatusCB(reader io.Reader, headers http.Header) {
var data common.APIPushStatus
dec := json.NewDecoder(reader)
err := dec.Decode(&data)
if err != nil {
log.Fatal(err.Error())
}
if data.Status != common.APIPushStatusPushing {
filePushVars.loop = false
if filePushVars.spinner != nil {
filePushVars.spinner.Stop()
fmt.Println()
}
switch data.Status {
case common.APIPushStatusError:
log.Fatal(data.Error)
case common.APIPushStatusSuccess:
fmt.Printf("File successfully pushed to '%s'\n", filePushVars.destination)
default:
log.Fatalf("unknown status '%s'", data.Status)
}
return
}
if filePushVars.previousStatus != data.Status {
newPushSpinner(data.Status)
filePushVars.previousStatus = data.Status
}
end := time.Now().Add(data.ETA).Format("2006-01-02 15:04")
msg := fmt.Sprintf("%s: %s (%s)", data.Status, data.ETA, end)
if isatty.IsTerminal(os.Stdout.Fd()) {
filePushVars.spinner.Suffix = " " + msg
} else {
fmt.Println(msg)
}
}
func init() {
fileCmd.AddCommand(filePushCmd)
filePushCmd.Flags().StringP("expire", "e", "", "expiration delay (ex: 2h, 10d, 1y)")
}