github.com/rohankumardubey/proxyfs@v0.0.0-20210108201508-653efa9ab00e/make-static-content/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 ) 8 9 const bytesPerLine = 16 10 11 func usage() { 12 fmt.Println("make-static-content -?") 13 fmt.Println(" Prints this help text") 14 fmt.Println("make-static-content <packageName> <contentName> <contentType> <contentFormat> <srcFile> <dstFile.go>") 15 fmt.Println(" <packageName> is the name of the ultimate package for <dstFile.go>") 16 fmt.Println(" <contentName> is the basename of the desired content resource") 17 fmt.Println(" <contentType> is the string to record as the static content's Content-Type") 18 fmt.Println(" <contentFormat> indicates whether the static content is a string (\"s\") or a []byte (\"b\")") 19 fmt.Println(" <srcFile> is the path to the static content to be embedded") 20 fmt.Println(" <dstFile.go> is the name of the generated .go source file containing:") 21 fmt.Println(" <contentName>ContentType string holding value of <contentType>") 22 fmt.Println(" <contentName>Content string or []byte holding contents of <srcFile>") 23 } 24 25 var bs = []byte{} 26 27 func main() { 28 var ( 29 contentFormat string 30 contentName string 31 contentType string 32 dstFile *os.File 33 dstFileName string 34 err error 35 packageName string 36 srcFileContentByte byte 37 srcFileContentIndex int 38 srcFileContents []byte 39 srcFileName string 40 ) 41 42 if (2 == len(os.Args)) && ("-?" == os.Args[1]) { 43 usage() 44 os.Exit(0) 45 } 46 47 if 7 != len(os.Args) { 48 usage() 49 os.Exit(1) 50 } 51 52 packageName = os.Args[1] 53 contentName = os.Args[2] 54 contentType = os.Args[3] 55 contentFormat = os.Args[4] 56 srcFileName = os.Args[5] 57 dstFileName = os.Args[6] 58 59 srcFileContents, err = ioutil.ReadFile(srcFileName) 60 if nil != err { 61 panic(err.Error()) 62 } 63 64 dstFile, err = os.Create(dstFileName) 65 if nil != err { 66 panic(err.Error()) 67 } 68 69 _, err = dstFile.Write([]byte(fmt.Sprintf("// Code generated by \"go run make_static_content.go %v %v %v %v %v %v\" - DO NOT EDIT\n\n", packageName, contentName, contentType, contentFormat, srcFileName, dstFileName))) 70 if nil != err { 71 panic(err.Error()) 72 } 73 _, err = dstFile.Write([]byte(fmt.Sprintf("package %v\n\n", packageName))) 74 if nil != err { 75 panic(err.Error()) 76 } 77 78 _, err = dstFile.Write([]byte(fmt.Sprintf("const %vContentType = \"%v\"\n\n", contentName, contentType))) 79 if nil != err { 80 panic(err.Error()) 81 } 82 83 switch contentFormat { 84 case "s": 85 _, err = dstFile.Write([]byte(fmt.Sprintf("const %vContent = `%v`\n", contentName, string(srcFileContents[:])))) 86 if nil != err { 87 panic(err.Error()) 88 } 89 case "b": 90 _, err = dstFile.Write([]byte(fmt.Sprintf("var %vContent = []byte{", contentName))) 91 if nil != err { 92 panic(err.Error()) 93 } 94 for srcFileContentIndex, srcFileContentByte = range srcFileContents { 95 if 0 == (srcFileContentIndex % bytesPerLine) { 96 _, err = dstFile.Write([]byte(fmt.Sprintf("\n\t0x%02X,", srcFileContentByte))) 97 } else { 98 _, err = dstFile.Write([]byte(fmt.Sprintf(" 0x%02X,", srcFileContentByte))) 99 } 100 if nil != err { 101 panic(err.Error()) 102 } 103 } 104 _, err = dstFile.Write([]byte("\n}\n")) 105 if nil != err { 106 panic(err.Error()) 107 } 108 default: 109 usage() 110 os.Exit(1) 111 } 112 113 err = dstFile.Close() 114 if nil != err { 115 panic(err.Error()) 116 } 117 118 os.Exit(0) 119 }