This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
forked from gofunct/fsctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
129 lines (111 loc) 路 2.12 KB
/
load.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
package fsctl
import (
"context"
"fmt"
"github.com/hashicorp/go-getter"
"log"
"os"
"os/signal"
"strconv"
"sync"
)
type Mode int
const _Mode_name = "ANYFILEDIR"
const (
ANY Mode = iota
FILE
DIR
)
var _Mode_index = [...]uint8{0, 3, 7, 10}
func (f *Fs) GetDir(src, dst string) {
var moder = getter.ClientModeDir
// Get the pwd
pwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error getting wd: %v", err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
// Build the client
client := &getter.Client{
Ctx: ctx,
Src: src,
Dst: dst,
Pwd: pwd,
Mode: moder,
}
wg := sync.WaitGroup{}
wg.Add(1)
errChan := make(chan error, 2)
go func() {
defer wg.Done()
defer cancel()
if err := client.Get(); err != nil {
errChan <- err
}
}()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
select {
case sig := <-c:
signal.Reset(os.Interrupt)
cancel()
wg.Wait()
log.Printf("signal %v", sig)
case <-ctx.Done():
wg.Wait()
log.Printf("success!")
case err := <-errChan:
wg.Wait()
log.Fatalf("Error downloading: %s", err)
}
}
func (f *Fs) GetFile(src, dst string) {
var moder = getter.ClientModeFile
// Get the pwd
pwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error getting wd: %v", err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
// Build the client
client := &getter.Client{
Ctx: ctx,
Src: src,
Dst: dst,
Pwd: pwd,
Mode: moder,
}
wg := sync.WaitGroup{}
wg.Add(1)
errChan := make(chan error, 2)
go func() {
defer wg.Done()
defer cancel()
if err := client.Get(); err != nil {
errChan <- err
}
}()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
select {
case sig := <-c:
signal.Reset(os.Interrupt)
cancel()
wg.Wait()
log.Printf("signal %v", sig)
case <-ctx.Done():
wg.Wait()
log.Printf("success!")
case err := <-errChan:
wg.Wait()
log.Fatalf("Error downloading: %s", err)
}
}
func (i Mode) String() string {
if i < 0 || i >= Mode(len(_Mode_index)-1) {
return "Mode(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Mode_name[_Mode_index[i]:_Mode_index[i+1]]
}