/
init.go
51 lines (40 loc) 路 1.06 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
48
49
50
51
package cmd
import (
"io/ioutil"
"path/filepath"
"strings"
toml "github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/srvc/ery/pkg/app/command"
"github.com/srvc/ery/pkg/ery/di"
"go.uber.org/zap"
)
func newCmdInit(c di.AppComponent) *cobra.Command {
wd := c.Config().WorkingDir
var (
proj = filepath.Base(wd)
org = filepath.Base(filepath.Dir(wd))
tld = c.Config().TLD
)
hostname := strings.Join([]string{proj, org, tld}, ".")
cmd := &cobra.Command{
Use: "init",
Short: "Genereate an ery's configuration file",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := command.Config{Hostname: hostname}
data, err := toml.Marshal(cfg)
if err != nil {
return errors.WithStack(err)
}
err = ioutil.WriteFile(filepath.Join(wd, ".ery.toml"), data, 0644)
if err != nil {
return errors.WithStack(err)
}
zap.L().Info(".ery.toml is geneerated", zap.Any("config", cfg))
return nil
},
}
cmd.PersistentFlags().StringVar(&hostname, "host", hostname, "Hostname")
return cmd
}