Skip to content

Commit

Permalink
Add flag to disable writing header (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
rytswd committed Sep 28, 2021
1 parent c2f1f3e commit 8230587
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 5 additions & 3 deletions internal/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ This approach allows the input file to be full of Importer markes without actual
Args: cobra.MinimumNArgs(1),
RunE: executeGenerate,
}
generateTargetFile string
generateKeepMarkers bool
generateTargetFile string
generateKeepMarkers bool
generateDisableHeader bool
)

func init() {
generateCliCmd.Flags().StringVarP(&generateTargetFile, "out", "o", "", "write to `FILE`")
generateCliCmd.Flags().BoolVar(&generateKeepMarkers, "keep-markers", false, "keep Importer Markers from the generated result")
generateCliCmd.Flags().BoolVar(&generateDisableHeader, "disable-header", false, "disable automatically added header of Importer generated notice")
}

func executeGenerate(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -75,7 +77,7 @@ func generate(fileName string, targetFilepath string, keepMarkers bool) error {
}

if targetFilepath != "" {
return file.WriteAfterTo(targetFilepath)
return file.WriteAfterTo(targetFilepath, generateDisableHeader)
}

return file.PrintAfter()
Expand Down
8 changes: 7 additions & 1 deletion internal/file/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import (
)

// WriteAfterTo writes the processed content to the provided filepath.
func (f *File) WriteAfterTo(targetFilePath string) error {
func (f *File) WriteAfterTo(targetFilePath string, disableHeader bool) error {
file, err := os.OpenFile(targetFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()

// If no header is needed, simply write the updated content and complete.
if disableHeader {
_, err = file.Write(f.ContentAfter)
return err
}

content := []byte{}
content = append(content, f.prepareGeneratedHeader(targetFilePath)...)
content = append(content, f.ContentAfter...)
Expand Down

0 comments on commit 8230587

Please sign in to comment.