-
Notifications
You must be signed in to change notification settings - Fork 11
/
init_app.go
64 lines (50 loc) · 1.34 KB
/
init_app.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 command
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/simplesurance/baur"
"github.com/simplesurance/baur/cfg"
"github.com/simplesurance/baur/log"
)
func init() {
initCmd.AddCommand(initAppCmd)
}
const initAppLongHelp = `
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 initAppExample = `
baur init app shop-ui create an application config with the app name set to shop-ui`
var initAppCmd = &cobra.Command{
Use: "app [APP-NAME]",
Short: "creates an application config file in the current directory",
Long: strings.TrimSpace(initAppLongHelp),
Example: strings.TrimSpace(initAppExample),
Run: initApp,
Args: cobra.MaximumNArgs(1),
}
func initApp(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 = filepath.Base(cwd)
}
appCfg := cfg.ExampleApp(appName)
err = appCfg.ToFile(filepath.Join(cwd, baur.AppCfgFile))
if err != nil {
if os.IsExist(err) {
log.Fatalf("%s already exist\n", baur.AppCfgFile)
}
log.Fatalln(err)
}
fmt.Printf("Application configuration file was written to %s\n",
highlight(baur.AppCfgFile))
}