-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.go
137 lines (124 loc) · 4.38 KB
/
init.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
package cli
import (
"errors"
"fmt"
"github.com/google/logger"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/discentem/cavorite/config"
"github.com/discentem/cavorite/program"
"github.com/discentem/cavorite/stores"
)
func initCmd() *cobra.Command {
initCmd := &cobra.Command{
Use: "init",
Short: fmt.Sprintf("Initialize a new %s repo", program.Name),
Long: fmt.Sprintf("Initialize a new %s repo", program.Name),
Args: func(cmd *cobra.Command, args []string) error {
fn := cobra.ExactArgs(1)
err := fn(cmd, args)
if err != nil {
return fmt.Errorf("you must specify a path to a repo you want %s to track", program.Name)
}
return nil
},
PreRunE: initPreExecFn,
RunE: initFn,
}
initCmd.PersistentFlags().String("backend_address", "", "Address for the storage backend")
initCmd.PersistentFlags().String("region", "us-east-1", "Default region for the storage backend")
initCmd.PersistentFlags().String("store_type", "", "Storage backend to use")
initCmd.PersistentFlags().String("plugin_address", "", "Address for go-plugin that provides implementation for Store")
initCmd.PersistentFlags().String("object_key_prefix",
"",
"Prefixed added to backend upload and retrieve requests. This does not affect the location of written metadata for objects.")
return initCmd
}
// initPreExecFn is the pre-execution runtime for the initCmd functionality
// in Cobra, PreRunE is a concept of Cobra. It runs before RunE, similar to init() running before
/*
// The *Run functions are executed in the following order:
// * PersistentPreRunE()
// * PreRunE() [X]
// * RunE()
// * PostRunE()
// * PersistentPostRunE()
// All functions get the same args, the arguments after the command name.
*/
func initPreExecFn(cmd *cobra.Command, _ []string) error {
// Bind all the flags to a viper setting so we can use viper everywhere without thinking about it
if err := viper.BindPFlag("backend_address", cmd.PersistentFlags().Lookup("backend_address")); err != nil {
return errors.New("Failed to bind backend_address to viper")
}
if err := viper.BindPFlag("region", cmd.PersistentFlags().Lookup("region")); err != nil {
return errors.New("Failed to bind region to viper")
}
if err := viper.BindPFlag("store_type", cmd.PersistentFlags().Lookup("store_type")); err != nil {
return errors.New("Failed to bind store_type to viper")
}
if err := viper.BindPFlag("plugin_address", cmd.PersistentFlags().Lookup("plugin_address")); err != nil {
return errors.New("Failed to bind plugin_address to viper")
}
if err := viper.BindPFlag("object_key_prefix", cmd.PersistentFlags().Lookup("object_key_prefix")); err != nil {
return errors.New("Failed to bind object_key_prefix to viper")
}
return nil
}
// initFn is the execution runtime for the initCmd functionality
// in Cobra, this is the RunE phase
/*
// The *Run functions are executed in the following order:
// * PersistentPreRunE()
// * PreRunE() []
// * RunE() [X]
// * PostRunE()
// * PersistentPostRunE()
// All functions get the same args, the arguments after the command name.
*/
func initFn(cmd *cobra.Command, args []string) error {
repoToInit := args[0]
backend := viper.GetString("store_type")
fileExt := viper.GetString("metadata_file_extension")
backendAddress := viper.GetString("backend_address")
region := viper.GetString("region")
keyPrefix := viper.GetString("object_key_prefix")
opts := stores.Options{
BackendAddress: backendAddress,
MetadataFileExtension: fileExt,
Region: region,
ObjectKeyPrefix: keyPrefix,
}
pluginAddress := viper.GetString("plugin_address")
if pluginAddress != "" {
logger.Infof("pluginAddress: %s", pluginAddress)
opts.PluginAddress = pluginAddress
}
fsys := afero.NewOsFs()
sb := stores.StoreType(backend)
getConfig := func() config.Config {
return config.InitalizeStoreTypeOf(
cmd.Context(),
sb,
fsys,
repoToInit,
opts,
)
}
switch sb {
case stores.StoreTypeS3:
config.Cfg = getConfig()
case stores.StoreTypeGCS:
config.Cfg = getConfig()
case stores.StoreTypeAzureBlob:
config.Cfg = getConfig()
case stores.StoreTypeGoPlugin:
if pluginAddress == "" {
return fmt.Errorf("--store_type was %q but --plugin_address was not specified", string(sb))
}
config.Cfg = getConfig()
default:
return config.ErrUnsupportedStore
}
return config.Cfg.Write(fsys, repoToInit)
}