/
init.go
46 lines (38 loc) · 1.3 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
// Copyright © 2018 Tyler Spivey <tspivey@pcdesk.net> and Niko Carpenter <nikoacarpenter@gmail.com>
//
// This source code is governed by the MIT license, which can be found in the LICENSE file.
package commands
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/tspivey/books"
)
var overrideExistingLibrary = false
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize the library",
Long: `Initialize a new empty library`,
Run: func(cmd *cobra.Command, args []string) {
if _, err := os.Stat(libraryFile); err == nil {
if !overrideExistingLibrary {
fmt.Fprintf(os.Stderr, "A library already exists in %s. Use -f to forcefully override the existing library, or choose another configuration directory.\n", libraryFile)
os.Exit(1)
}
fmt.Println("Warning: overriding existing library")
if err := os.Remove(libraryFile); err != nil {
fmt.Fprintf(os.Stderr, "Cannot remove existing library: %s\n", err)
os.Exit(1)
}
}
if err := books.CreateLibrary(libraryFile); err != nil {
fmt.Fprintf(os.Stderr, "Cannot create library: %s\n", err)
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().BoolVarP(&overrideExistingLibrary, "forceOverride", "f", false, "Override a library if one already exists.")
}