forked from simplesurance/baur
/
appinit.go
62 lines (49 loc) · 1.32 KB
/
appinit.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
package command
import (
"os"
"path"
"strings"
"github.com/simplesurance/baur"
"github.com/simplesurance/baur/cfg"
"github.com/simplesurance/baur/log"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(appInitCmd)
}
const appInitLongHelp = `
Create an application config file in the current directory.
If no name is passed, the application name will be the name of the current directory.`
const appInitExample = `
baur appinit shop-ui create a new application config with the app name set to shop-ui`
var appInitCmd = &cobra.Command{
Use: "appinit [APP-NAME]",
Short: "creates an application config file in the current directory",
Long: strings.TrimSpace(appInitLongHelp),
Example: strings.TrimSpace(appInitExample),
Run: appInit,
Args: cobra.MaximumNArgs(1),
}
func appInit(cmd *cobra.Command, args []string) {
var appName string
mustFindRepository()
cwd, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
if len(args) > 0 {
appName = args[0]
} else {
appName = path.Base(cwd)
}
appCfg := cfg.ExampleApp(appName)
err = appCfg.ToFile(path.Join(cwd, baur.AppCfgFile))
if err != nil {
if os.IsExist(err) {
log.Fatalf("%s already exist\n", baur.AppCfgFile)
}
log.Fatalln(err)
}
log.Infof("configuration file for %s was written to %s\n",
appName, baur.AppCfgFile)
}