github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/generate/winuserdata/winuserdata.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Copyright 2016 Cloudbase Solutions SRL 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package main 6 7 import ( 8 "bytes" 9 "fmt" 10 "io/ioutil" 11 "os" 12 "strings" 13 "text/template" 14 15 "github.com/juju/errors" 16 ) 17 18 var fileTemplate = ` 19 // Copyright {{.CopyrightYear}} Canonical Ltd. 20 // Copyright {{.CopyrightYear}} Cloudbase Solutions SRL 21 // Licensed under the AGPLv3, see LICENCE file for details. 22 23 package {{.Pkgname}} 24 25 // Generated code - do not edit. 26 27 const addJujuUser = {{.AddJujuUser}} 28 const windowsPowershellHelpers = {{.WindowsPowershellHelper}} 29 `[1:] 30 31 // compoundWinPowershellFunc returns the windows powershell helper 32 // functions declared under the windowsuserdatafiles/ dir 33 func compoundWinPowershellFuncs() (string, error) { 34 var winPowershellFunc bytes.Buffer 35 36 filenames := []string{ 37 "windowsuserdatafiles/retry.ps1", 38 "windowsuserdatafiles/untar.ps1", 39 "windowsuserdatafiles/filesha256.ps1", 40 "windowsuserdatafiles/invokewebrequest.ps1", 41 } 42 for _, filename := range filenames { 43 content, err := ioutil.ReadFile(filename) 44 if err != nil { 45 return "", errors.Trace(err) 46 } 47 winPowershellFunc.Write(content) 48 } 49 50 return winPowershellFunc.String(), nil 51 } 52 53 // CompoundJujuUser retuns the windows powershell funcs with c# bindings 54 // declared under the windowsuserdatafiles/ dir 55 func compoundJujuUser() (string, error) { 56 57 // note that addJujuUser.ps1 has hinting format locations for sprintf 58 // in that hinting locations we will add the c# scripts under the same file 59 content, err := ioutil.ReadFile("windowsuserdatafiles/addJujuUser.ps1") 60 if err != nil { 61 return "", errors.Trace(err) 62 } 63 64 // take the two addJujuUser data c# scripts and construct the powershell 65 // script for adding user for juju in windows 66 cryptoAPICode, err := ioutil.ReadFile("windowsuserdatafiles/CryptoApi.cs") 67 if err != nil { 68 return "", errors.Trace(err) 69 } 70 carbonCode, err := ioutil.ReadFile("windowsuserdatafiles/Carbon.cs") 71 if err != nil { 72 return "", errors.Trace(err) 73 } 74 75 return fmt.Sprintf(string(content), string(cryptoAPICode), string(carbonCode)), nil 76 } 77 78 // This generator reads from a file and generates a Go file with the 79 // file's content assigned to a go constant. 80 func main() { 81 if len(os.Args) < 4 { 82 fmt.Println("Usage: winuserdata <copyrightyear> <gofile> <pkgname>") 83 } 84 85 addJujuUser, err := compoundJujuUser() 86 if err != nil { 87 fmt.Println(err) 88 os.Exit(1) 89 } 90 winpowershell, err := compoundWinPowershellFuncs() 91 if err != nil { 92 fmt.Println(err) 93 os.Exit(1) 94 } 95 96 var buf bytes.Buffer 97 type content struct { 98 AddJujuUser string 99 WindowsPowershellHelper string 100 CopyrightYear string 101 Pkgname string 102 } 103 104 t, err := template.New("").Parse(fileTemplate) 105 if err != nil { 106 fmt.Println(err) 107 os.Exit(1) 108 } 109 110 addJujuUser = fmt.Sprintf("\n%s", addJujuUser) 111 winpowershell = fmt.Sprintf("\n%s", winpowershell) 112 113 // Quote any ` in the data. 114 addJujuUser = strings.Replace(addJujuUser, "`", "` + \"`\" + `", -1) 115 winpowershell = strings.Replace(winpowershell, "`", "` + \"`\" + `", -1) 116 117 t.Execute(&buf, content{ 118 AddJujuUser: fmt.Sprintf("`%s`", addJujuUser), 119 WindowsPowershellHelper: fmt.Sprintf("`%s`", winpowershell), 120 Pkgname: os.Args[3], 121 CopyrightYear: os.Args[1], 122 }) 123 124 err = ioutil.WriteFile(os.Args[2], buf.Bytes(), 0644) 125 if err != nil { 126 fmt.Println(err) 127 os.Exit(1) 128 } 129 }