~nskaggs/+junk/xenial-test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2016 Canonical Ltd.
// Copyright 2016 Cloudbase Solutions SRL
// Licensed under the AGPLv3, see LICENCE file for details.

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
	"text/template"

	"github.com/juju/errors"
)

var fileTemplate = `
// Copyright {{.CopyrightYear}} Canonical Ltd.
// Copyright {{.CopyrightYear}} Cloudbase Solutions SRL
// Licensed under the AGPLv3, see LICENCE file for details.

package {{.Pkgname}}

// Generated code - do not edit.

const addJujuUser = {{.AddJujuUser}}
const windowsPowershellHelpers = {{.WindowsPowershellHelper}}
`[1:]

// compoundWinPowershellFunc returns the windows powershell helper
// functions declared under the windowsuserdatafiles/ dir
func compoundWinPowershellFuncs() (string, error) {
	var winPowershellFunc bytes.Buffer

	filenames := []string{
		"windowsuserdatafiles/retry.ps1",
		"windowsuserdatafiles/untar.ps1",
		"windowsuserdatafiles/filesha256.ps1",
		"windowsuserdatafiles/invokewebrequest.ps1",
	}
	for _, filename := range filenames {
		content, err := ioutil.ReadFile(filename)
		if err != nil {
			return "", errors.Trace(err)
		}
		winPowershellFunc.Write(content)
	}

	return winPowershellFunc.String(), nil
}

// CompoundJujuUser retuns the windows powershell funcs with c# bindings
// declared under the windowsuserdatafiles/ dir
func compoundJujuUser() (string, error) {

	// note that addJujuUser.ps1 has hinting format locations for sprintf
	// in that hinting locations we will add the c# scripts under the same file
	content, err := ioutil.ReadFile("windowsuserdatafiles/addJujuUser.ps1")
	if err != nil {
		return "", errors.Trace(err)
	}

	// take the two addJujuUser data c# scripts and construct the powershell
	// script for adding user for juju in windows
	cryptoAPICode, err := ioutil.ReadFile("windowsuserdatafiles/CryptoApi.cs")
	if err != nil {
		return "", errors.Trace(err)
	}
	carbonCode, err := ioutil.ReadFile("windowsuserdatafiles/Carbon.cs")
	if err != nil {
		return "", errors.Trace(err)
	}

	return fmt.Sprintf(string(content), string(cryptoAPICode), string(carbonCode)), nil
}

// This generator reads from a file and generates a Go file with the
// file's content assigned to a go constant.
func main() {
	if len(os.Args) < 4 {
		fmt.Println("Usage: winuserdata <copyrightyear> <gofile> <pkgname>")
	}

	addJujuUser, err := compoundJujuUser()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	winpowershell, err := compoundWinPowershellFuncs()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	var buf bytes.Buffer
	type content struct {
		AddJujuUser             string
		WindowsPowershellHelper string
		CopyrightYear           string
		Pkgname                 string
	}

	t, err := template.New("").Parse(fileTemplate)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	addJujuUser = fmt.Sprintf("\n%s", addJujuUser)
	winpowershell = fmt.Sprintf("\n%s", winpowershell)

	// Quote any ` in the data.
	addJujuUser = strings.Replace(addJujuUser, "`", "` + \"`\" + `", -1)
	winpowershell = strings.Replace(winpowershell, "`", "` + \"`\" + `", -1)

	t.Execute(&buf, content{
		AddJujuUser:             fmt.Sprintf("`%s`", addJujuUser),
		WindowsPowershellHelper: fmt.Sprintf("`%s`", winpowershell),
		Pkgname:                 os.Args[3],
		CopyrightYear:           os.Args[1],
	})

	err = ioutil.WriteFile(os.Args[2], buf.Bytes(), 0644)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}