-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
64 lines (55 loc) · 1.27 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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"os"
)
var (
keyFileName string
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initializes Firebase authentication via JSON key file",
Long: `Initializes Firestore connection with service account key file`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Initializing with: ", keyFileName)
addServiceAccount(keyFileName)
fmt.Println("Done")
},
}
func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().StringVarP(&keyFileName, "file", "f", "", "JSON key file location")
}
func addServiceAccount(keyFileName string) {
dest := "/.ripfire/key.json"
input, err := ioutil.ReadFile(keyFileName)
if err != nil {
fmt.Println(err)
return
}
homedir, err := os.UserHomeDir()
if err != nil {
log.Fatal( err )
return
}
destinationFile := homedir + dest
if _, err := os.Stat(homedir + "/.ripfire"); err != nil {
err = os.Mkdir(homedir + "/.ripfire", 0755)
if err != nil {
log.Fatal(err)
return
}
}
if _, err := os.Stat(destinationFile); err != nil {
err = ioutil.WriteFile(destinationFile, input, 0644)
if err != nil {
fmt.Println("Error copying file to: ", destinationFile)
fmt.Println(err)
return
}
}
}