| 96 | } |
| 97 | |
| 98 | func getOutput(c *commandPolicyExport) (io.Writer, error) { |
| 99 | var err error |
| 100 | |
| 101 | if c.filePath == "" { |
| 102 | if c.overwrite { |
| 103 | return nil, errors.New("overwrite was passed but no file path was given") |
| 104 | } |
| 105 | |
| 106 | return c.svc.stdout(), nil |
| 107 | } |
| 108 | |
| 109 | var file *os.File |
| 110 | |
| 111 | if c.overwrite { |
| 112 | file, err = os.Create(c.filePath) |
| 113 | } else { |
| 114 | file, err = os.OpenFile(c.filePath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, exportFilePerms) |
| 115 | if os.IsExist(err) { |
| 116 | return nil, errors.Wrap(err, "file already exists and overwrite flag is not set") |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if err != nil { |
| 121 | return nil, errors.Wrap(err, "error opening file to write to") |
| 122 | } |
| 123 | |
| 124 | return file, nil |
| 125 | } |