-
Notifications
You must be signed in to change notification settings - Fork 10
/
raw.go
167 lines (143 loc) · 3.46 KB
/
raw.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 main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
func cmdRawBech32(cobraCmd *cobra.Command, args []string) error {
bech32String := args[0]
bech32Prefix := args[1]
newbech32String, err := bech32ify(bech32String, bech32Prefix)
if err != nil {
return err
}
fmt.Println(newbech32String)
return nil
}
// copy a local file to the bucket
func cmdRawUp(cobraCmd *cobra.Command, args []string) error {
local := args[0]
remote := args[1]
conf, err := loadConfig(flagConfigPath)
if err != nil {
return err
}
sess := awsSession(conf.AWS)
// read the local file
localBytes, err := ioutil.ReadFile(local)
if err != nil {
return err
}
// upload it
dir := filepath.Dir(remote)
fileName := filepath.Base(remote)
if err := awsUpload(sess, conf.AWS, dir, fileName, localBytes); err != nil {
return err
}
return nil
}
// copy a file from the bucket to the local machine
func cmdRawDown(cobraCmd *cobra.Command, args []string) error {
remote := args[0]
local := args[1]
conf, err := loadConfig(flagConfigPath)
if err != nil {
return err
}
sess := awsSession(conf.AWS)
// if remote ends in /, fetch the whole directory and return
if strings.HasSuffix(remote, "/") {
if err := os.Mkdir(local, 0777); err != nil {
return err
}
if err := os.Chdir(local); err != nil {
return err
}
_, err = awsDownloadFilesInDir(sess, conf.AWS, remote)
return err
}
// otherwise, just download the one file
dir := filepath.Dir(remote)
fileName := filepath.Base(remote)
file, err := awsDownload(sess, conf.AWS, dir, fileName)
if err != nil {
return err
}
// rename if necessary
oldName := file.Name()
newName := local
if oldName != newName {
return os.Rename(oldName, newName)
}
return nil
}
// dump content of all files in a dir
func cmdRawCat(cobraCmd *cobra.Command, args []string) error {
chainName := args[0]
keyName := args[1]
conf, err := loadConfig(flagConfigPath)
if err != nil {
return err
}
sess := awsSession(conf.AWS)
txDir := filepath.Join(chainName, keyName)
// Get all the files in S3 bucket and display the contents
// of the files
filesInS3, err := awsListFilesInDir(sess, conf.AWS, chainName, keyName)
if err != nil {
return err
} else {
if len(filesInS3) == 0 {
fmt.Println("No files in", txDir)
return nil
}
for _, file := range filesInS3 {
parts := strings.Split(file, "/")
path := strings.Join([]string{parts[0], parts[1], parts[2]}, "/")
filename := parts[3]
f, err := awsDownload(sess, conf.AWS, path, filename)
if err != nil {
return err
} else {
b, err := ioutil.ReadFile(f.Name())
if err != nil {
return err
}
fmt.Printf("\n|------------| %s |------------|", file)
fmt.Println("\n")
fmt.Println(string(b))
os.Remove(f.Name())
}
}
}
return nil
}
// delete a file from the bucket
func cmdRawDelete(cobraCmd *cobra.Command, args []string) error {
filePath := args[0]
conf, err := loadConfig(flagConfigPath)
if err != nil {
return err
}
sess := awsSession(conf.AWS)
awsDelete(sess, conf.AWS, filePath)
return nil
}
// create an empty object with the given name
func cmdRawMkdir(cobraCmd *cobra.Command, args []string) error {
dirName := args[0]
if !strings.HasSuffix(dirName, "/") {
fmt.Println("directory paths must end with a '/'")
return nil
}
conf, err := loadConfig(flagConfigPath)
if err != nil {
return err
}
sess := awsSession(conf.AWS)
awsMkdir(sess, conf.AWS, dirName)
return nil
}