-
Notifications
You must be signed in to change notification settings - Fork 4
/
createIndex.go
53 lines (44 loc) · 1.33 KB
/
createIndex.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"github.com/dnnrly/s3backup"
"github.com/spf13/cobra"
)
// createIndexCmd represents the createIndex command
var createIndexCmd = &cobra.Command{
Use: "create-index",
Short: "Generates an index file for known files in a location",
Long: `This command generates an index file that sits at the root of
your S3 bucket, avoiding all of the index performance issues with
scanning all the files. It identifies all of the meta data you
need to manage the files that have been backed up.`,
Run: func(cmd *cobra.Command, args []string) {
data, err := createIndex()
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
err = ioutil.WriteFile(optIndexFile, []byte(data), 0644)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(createIndexCmd)
createIndexCmd.Flags().StringVar(&optIndexFile, "file", optIndexFile, "Location of the index file to write")
}
func createIndex() (string, error) {
index, err := s3backup.NewIndexFromRoot("", optIndexDirectory, s3backup.FilePathWalker, s3backup.FileHasher)
if err != nil {
return "", fmt.Errorf("Unable to read files for index: %w", err)
}
data, err := index.Encode()
if err != nil {
return "", fmt.Errorf("Unable to create index: %w", err)
}
return data, nil
}