-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
47 lines (37 loc) · 1.1 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
package cmd
import (
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
// InitCmd - Create configuration file.
var InitCmd = &cobra.Command{
Use: "init",
Short: "Initialize a konnect.yml configuration file at the given directory.",
Long: "Initialize a konnect.yml configuration file at the given directory.",
Run: func(cmd *cobra.Command, args []string) {
// Directory for the configuration file.
// Default to the current directory.
dir := "."
// Check if too many args were given.
if len(args) > 1 {
log.Fatal("Too many args. Please specify a directory")
}
// If specified, then use the given directory.
if len(args) == 1 {
dir = args[0]
}
dir, _ = filepath.Abs(dir)
// Get the default name for the config file.
localFilename := getDefaultConfig()
// Join the resolved directory with
// the default config filename.
filename := filepath.Join(dir, localFilename)
// If the file already exists, then return error and exit.
if _, err := os.Stat(filename); err == nil {
log.Fatalf("File %v already exists.\n", filename)
}
makeDefaultConfig(filename)
},
}