/
config.go
85 lines (71 loc) · 2.4 KB
/
config.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
// Copyright © 2019 Matt Konda <mkonda@jemurai.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/user"
"github.com/jemurai/s3s2/options"
log "github.com/sirupsen/logrus"
"github.com/c-bata/go-prompt"
"github.com/spf13/cobra"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Build a configuration file",
Long: `Build a configuration file so that we can run the tool with exactly the options we want.`,
Run: func(cmd *cobra.Command, args []string) {
fn, _ := cmd.PersistentFlags().GetString("file")
fmt.Println("Please specify a bucket.")
bucket := prompt.Input("> ", completer)
fmt.Println("Please specify a region.")
region := prompt.Input("> ", completer)
fmt.Println("Please specify an org.")
org := prompt.Input("> ", completer)
fmt.Println("Please specify a working directory.")
dir := prompt.Input("> ", completer)
fmt.Println("Please specify a file prefix (nothing sensitive).")
prefix := prompt.Input("> ", completer)
fmt.Println("Please specify a public key to use (file path or url).")
pubkey := prompt.Input("> ", completer)
bc := options.Options{
Directory: dir,
Bucket: bucket,
Org: org,
Region: region,
Prefix: prefix,
PubKey: pubkey,
}
data, _ := json.MarshalIndent(bc, "", " ")
err := ioutil.WriteFile(fn, data, 0644)
if err != nil {
log.Warn(err)
log.Panic(err)
} else {
log.Info("Your config was written to", fn, ". You can invoke with s3s2 --config", fn)
}
},
}
func init() {
rootCmd.AddCommand(configCmd)
usr, _ := user.Current()
defaultPath := usr.HomeDir + "/.s3s2"
configCmd.PersistentFlags().String("file", defaultPath, "The config file to write.")
}
func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}