Skip to content

Commit

Permalink
Added --windows flag to write files with CRLF windows line endings, o…
Browse files Browse the repository at this point in the history
…therwise they are all unix based LF line endings
  • Loading branch information
Alex Jeannopoulos committed Jul 7, 2020
1 parent ae90072 commit 847e50b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
8 changes: 5 additions & 3 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions code_http.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func GetInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoices [post]
// echo '{"invoice_date": "2193-08-18T16:11:59.078188308-05:00","billing_address": "AqlNHbqeNoJOtnMJaPnzdILQa","billing_state": "tWuCMqsmCwdXbCINBdvKJRFLU","billing_postal_code": "SXZkYaGzotXoZOyOsikBMAFTZ","total": 0.5347164921866526,"invoice_id": 46,"customer_id": 8,"billing_city": "JdgoMLJqAcUmRgbXfqkCZaezO","billing_country": "bAWiKIZfJLnzNgUysoWJrhEkE"}' | http POST "http://127.0.0.1:8080/invoices" X-Api-User:user123
// echo '{"invoice_date": "2107-12-17T19:51:25.004788883-05:00","billing_address": "DvyYzGcuAeRAbdPBNFBlagepf","billing_city": "MAcHDoutMRvxAjKXaMFTplPFi","invoice_id": 98,"customer_id": 75,"billing_postal_code": "RxuAQTMTpGTgcFkuWHzosorWm","total": 0.04925881466860712,"billing_state": "guFrmbkzSZZCiqPmIvOwAMDuW","billing_country": "kLClnGHrwfzPehQfVmdbHusUp"}' | http POST "http://127.0.0.1:8080/invoices" X-Api-User:user123
func AddInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx := initializeContext(r)
invoices := &model.Invoices{}
Expand Down Expand Up @@ -192,7 +192,7 @@ func AddInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoices/{argInvoiceID} [patch]
// echo '{"invoice_date": "2193-08-18T16:11:59.078188308-05:00","billing_address": "AqlNHbqeNoJOtnMJaPnzdILQa","billing_state": "tWuCMqsmCwdXbCINBdvKJRFLU","billing_postal_code": "SXZkYaGzotXoZOyOsikBMAFTZ","total": 0.5347164921866526,"invoice_id": 46,"customer_id": 8,"billing_city": "JdgoMLJqAcUmRgbXfqkCZaezO","billing_country": "bAWiKIZfJLnzNgUysoWJrhEkE"}' | http PUT "http://127.0.0.1:8080/invoices/1" X-Api-User:user123
// echo '{"invoice_date": "2107-12-17T19:51:25.004788883-05:00","billing_address": "DvyYzGcuAeRAbdPBNFBlagepf","billing_city": "MAcHDoutMRvxAjKXaMFTplPFi","invoice_id": 98,"customer_id": 75,"billing_postal_code": "RxuAQTMTpGTgcFkuWHzosorWm","total": 0.04925881466860712,"billing_state": "guFrmbkzSZZCiqPmIvOwAMDuW","billing_country": "kLClnGHrwfzPehQfVmdbHusUp"}' | http PUT "http://127.0.0.1:8080/invoices/1" X-Api-User:user123
func UpdateInvoices(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ctx := initializeContext(r)

Expand Down
33 changes: 31 additions & 2 deletions dbmeta/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,20 @@ func (c *Config) WriteTemplate(genTemplate *GenTemplate, data map[string]interfa
fmt.Printf("Error in formatting template: %s outputfile: %s source: %s\n", genTemplate.Name, outputFile, err.Error())
formattedSource = buf.Bytes()
}
err = ioutil.WriteFile(outputFile, formattedSource, 0777)

fileContents := NormalizeNewlines(formattedSource)
if c.LineEndingCRLF {
fileContents = CRLFNewlines(formattedSource)
}

err = ioutil.WriteFile(outputFile, fileContents, 0777)
} else {
err = ioutil.WriteFile(outputFile, buf.Bytes(), 0777)
fileContents := NormalizeNewlines(buf.Bytes())
if c.LineEndingCRLF {
fileContents = CRLFNewlines(fileContents)
}

err = ioutil.WriteFile(outputFile, fileContents, 0777)
}

if err != nil {
Expand All @@ -502,6 +513,23 @@ func (c *Config) WriteTemplate(genTemplate *GenTemplate, data map[string]interfa
}
}

// NormalizeNewlines normalizes \r\n (windows) and \r (mac)
// into \n (unix)
func NormalizeNewlines(d []byte) []byte {
// replace CR LF \r\n (windows) with LF \n (unix)
d = bytes.Replace(d, []byte{13, 10}, []byte{10}, -1)
// replace CF \r (mac) with LF \n (unix)
d = bytes.Replace(d, []byte{13}, []byte{10}, -1)
return d
}

// CRLFNewlines transforms \n to \r\n (windows)
func CRLFNewlines(d []byte) []byte {
// replace LF (unix) with CR LF \r\n (windows)
d = bytes.Replace(d, []byte{10}, []byte{13, 10}, -1)
return d
}

// Exists reports whether the named file or directory exists.
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
Expand Down Expand Up @@ -579,6 +607,7 @@ type Config struct {
Verbose bool
OutDir string
Overwrite bool
LineEndingCRLF bool
CmdLine string
CmdLineWrapped string
CmdLineArgs []string
Expand Down
16 changes: 9 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ var (
fieldNamingTemplate = goopt.String([]string{"--field_naming"}, "{{FmtFieldName (stringifyFirstChar .) }}", "field naming template to name structs")
fileNamingTemplate = goopt.String([]string{"--file_naming"}, "{{.}}", "file_naming template to name files")

daoPackageName = goopt.String([]string{"--dao"}, "dao", "name to set for dao package")
apiPackageName = goopt.String([]string{"--api"}, "api", "name to set for api package")
grpcPackageName = goopt.String([]string{"--grpc"}, "grpc", "name to set for grpc package")
outDir = goopt.String([]string{"--out"}, ".", "output dir")
module = goopt.String([]string{"--module"}, "example.com/example", "module path")
overwrite = goopt.Flag([]string{"--overwrite"}, []string{"--no-overwrite"}, "Overwrite existing files (default)", "disable overwriting files")
daoPackageName = goopt.String([]string{"--dao"}, "dao", "name to set for dao package")
apiPackageName = goopt.String([]string{"--api"}, "api", "name to set for api package")
grpcPackageName = goopt.String([]string{"--grpc"}, "grpc", "name to set for grpc package")
outDir = goopt.String([]string{"--out"}, ".", "output dir")
module = goopt.String([]string{"--module"}, "example.com/example", "module path")
overwrite = goopt.Flag([]string{"--overwrite"}, []string{"--no-overwrite"}, "Overwrite existing files (default)", "disable overwriting files")
windows = goopt.Flag([]string{"--windows"}, []string{}, "use windows line endings in generated files", "")

contextFileName = goopt.String([]string{"--context"}, "", "context file (json) to populate context with")
mappingFileName = goopt.String([]string{"--mapping"}, "", "mapping file (json) to map sql types to golang/protobuf etc")
execCustomScript = goopt.String([]string{"--exec"}, "", "execute script for custom code generation")
Expand Down Expand Up @@ -332,11 +334,11 @@ func initialize(conf *dbmeta.Config) {
conf.Verbose = *verbose
conf.OutDir = *outDir
conf.Overwrite = *overwrite
conf.LineEndingCRLF = *windows

conf.SQLConnStr = *sqlConnStr
conf.ServerPort = *serverPort
conf.ServerHost = *serverHost
conf.Overwrite = *overwrite

conf.Module = *module
conf.ModelPackageName = *modelPackageName
Expand Down
Loading

0 comments on commit 847e50b

Please sign in to comment.