/
init_include.go
57 lines (45 loc) · 1.18 KB
/
init_include.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
package command
import (
"os"
"strings"
"github.com/spf13/cobra"
"github.com/simplesurance/baur/v3/internal/command/term"
"github.com/simplesurance/baur/v3/pkg/cfg"
)
func init() {
initCmd.AddCommand(initIncludeCmd)
}
const defIncludeFilename = "includes.toml"
const initIncludeLongHelp = `
Create an include config file.
If no FILENAME argument is passed, the filename will be '` + defIncludeFilename + `'.`
var initIncludeCmd = &cobra.Command{
Use: "include [FILENAME]",
Short: "create an include config file",
Long: strings.TrimSpace(initIncludeLongHelp),
Run: initInclude,
Args: cobra.MaximumNArgs(1),
}
func initInclude(_ *cobra.Command, args []string) {
var filename string
if len(args) == 1 {
filename = args[0]
if !strings.HasSuffix(filename, ".toml") {
filename += ".toml"
}
} else {
filename = defIncludeFilename
}
cfgInclude := cfg.ExampleInclude()
err := cfgInclude.ToFile(filename, cfg.ToFileOptCommented())
if err != nil {
if os.IsExist(err) {
stderr.Printf("%s already exist\n", filename)
exitFunc(1)
}
stderr.Println(err)
exitFunc(1)
}
stdout.Printf("Include configuration file was written to %s\n",
term.Highlight(filename))
}