configCreate is the callback function for create config command
(fileName string, isReplace bool, all bool)
| 10 | |
| 11 | // configCreate is the callback function for create config command |
| 12 | func configCreate(fileName string, isReplace bool, all bool) (retErr error) { |
| 13 | outPath := filepath.Join(".", fileName) |
| 14 | // if config file already exists skip creating or overwriting it |
| 15 | if _, err := os.Stat(outPath); !os.IsNotExist(err) { |
| 16 | if !isReplace { |
| 17 | return errConfigExist |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | outFilePath := filepath.Clean(outPath) |
| 22 | f, err := os.Create(outFilePath) |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | defer func() { |
| 27 | err := f.Close() |
| 28 | if retErr == nil && err != nil { |
| 29 | retErr = err |
| 30 | } |
| 31 | }() |
| 32 | |
| 33 | w := bufio.NewWriter(f) |
| 34 | defer func() { |
| 35 | err := w.Flush() |
| 36 | if retErr == nil && err != nil { |
| 37 | retErr = err |
| 38 | } |
| 39 | }() |
| 40 | |
| 41 | defConf := config.NewDefault() |
| 42 | if all { |
| 43 | return config.WriteTo(w, defConf) |
| 44 | } |
| 45 | return config.WriteCompactTo(w, defConf) |
| 46 | } |
| 47 | |
| 48 | // configCheck is the callback function for check/verify command |
| 49 | func configCheck(confPath string) []error { |
no test coverage detected