|
1 | 1 | package ig_homebrew
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 |
| - "errors" |
6 |
| - "github.com/watermint/toolbox/domain/github/api/gh_conn" |
7 |
| - "github.com/watermint/toolbox/domain/github/model/mo_commit" |
8 |
| - "github.com/watermint/toolbox/domain/github/service/sv_content" |
9 |
| - "github.com/watermint/toolbox/essentials/file/es_filehash" |
10 |
| - "github.com/watermint/toolbox/essentials/go/es_project" |
11 |
| - "github.com/watermint/toolbox/essentials/log/esl" |
12 |
| - "github.com/watermint/toolbox/essentials/model/mo_path" |
13 |
| - "github.com/watermint/toolbox/infra/control/app_control" |
14 |
| - "github.com/watermint/toolbox/infra/control/app_definitions" |
15 |
| - "github.com/watermint/toolbox/infra/control/app_resource" |
16 |
| - "github.com/watermint/toolbox/infra/recipe/rc_exec" |
17 |
| - "github.com/watermint/toolbox/infra/recipe/rc_recipe" |
18 |
| - "github.com/watermint/toolbox/infra/report/rp_model" |
19 |
| - "path/filepath" |
20 |
| - "text/template" |
21 |
| - "time" |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "github.com/watermint/toolbox/domain/github/api/gh_conn" |
| 7 | + "github.com/watermint/toolbox/domain/github/model/mo_commit" |
| 8 | + "github.com/watermint/toolbox/domain/github/service/sv_content" |
| 9 | + "github.com/watermint/toolbox/essentials/file/es_filehash" |
| 10 | + "github.com/watermint/toolbox/essentials/go/es_project" |
| 11 | + "github.com/watermint/toolbox/essentials/log/esl" |
| 12 | + "github.com/watermint/toolbox/essentials/model/mo_path" |
| 13 | + "github.com/watermint/toolbox/infra/control/app_control" |
| 14 | + "github.com/watermint/toolbox/infra/control/app_definitions" |
| 15 | + "github.com/watermint/toolbox/infra/control/app_resource" |
| 16 | + "github.com/watermint/toolbox/infra/recipe/rc_exec" |
| 17 | + "github.com/watermint/toolbox/infra/recipe/rc_recipe" |
| 18 | + "github.com/watermint/toolbox/infra/report/rp_model" |
| 19 | + "path/filepath" |
| 20 | + "text/template" |
| 21 | + "time" |
22 | 22 | )
|
23 | 23 |
|
24 | 24 | var (
|
25 |
| - ErrorNotAFile = errors.New("not a file") |
| 25 | + ErrorNotAFile = errors.New("not a file") |
26 | 26 | )
|
27 | 27 |
|
28 | 28 | type Formula struct {
|
29 |
| - AssetPathLinuxArm mo_path.ExistingFileSystemPath |
30 |
| - AssetPathLinuxIntel mo_path.ExistingFileSystemPath |
31 |
| - AssetPathMacArm mo_path.ExistingFileSystemPath |
32 |
| - AssetPathMacIntel mo_path.ExistingFileSystemPath |
33 |
| - DownloadUrlLinuxArm string |
34 |
| - DownloadUrlLinuxIntel string |
35 |
| - DownloadUrlMacArm string |
36 |
| - DownloadUrlMacIntel string |
37 |
| - Message string |
38 |
| - Branch string |
39 |
| - Owner string |
40 |
| - Repository string |
41 |
| - FormulaName string |
42 |
| - Peer gh_conn.ConnGithubRepo |
43 |
| - Commit rp_model.RowReport |
| 29 | + AssetPathLinuxArm mo_path.ExistingFileSystemPath |
| 30 | + AssetPathLinuxIntel mo_path.ExistingFileSystemPath |
| 31 | + AssetPathMacArm mo_path.ExistingFileSystemPath |
| 32 | + DownloadUrlLinuxArm string |
| 33 | + DownloadUrlLinuxIntel string |
| 34 | + DownloadUrlMacArm string |
| 35 | + Message string |
| 36 | + Branch string |
| 37 | + Owner string |
| 38 | + Repository string |
| 39 | + FormulaName string |
| 40 | + Peer gh_conn.ConnGithubRepo |
| 41 | + Commit rp_model.RowReport |
44 | 42 | }
|
45 | 43 |
|
46 | 44 | func (z *Formula) Preset() {
|
47 |
| - z.Commit.SetModel(&mo_commit.Commit{}) |
| 45 | + z.Commit.SetModel(&mo_commit.Commit{}) |
48 | 46 | }
|
49 | 47 |
|
50 | 48 | func (z *Formula) makeFormula(c app_control.Control) (formula string, err error) {
|
51 |
| - l := c.Log() |
52 |
| - h := es_filehash.NewHash(l) |
53 |
| - sha256LinuxIntel, err := h.SHA256(z.AssetPathLinuxIntel.Path()) |
54 |
| - if err != nil { |
55 |
| - l.Debug("Unable to calculate SHA sum of the asset", esl.Error(err)) |
56 |
| - return "", err |
57 |
| - } |
58 |
| - sha256LinuxArm, err := h.SHA256(z.AssetPathLinuxArm.Path()) |
59 |
| - if err != nil { |
60 |
| - l.Debug("Unable to calculate SHA sum of the asset", esl.Error(err)) |
61 |
| - return "", err |
62 |
| - } |
63 |
| - sha256MacIntel, err := h.SHA256(z.AssetPathMacIntel.Path()) |
64 |
| - if err != nil { |
65 |
| - l.Debug("Unable to calculate SHA sum of the asset", esl.Error(err)) |
66 |
| - return "", err |
67 |
| - } |
68 |
| - sha256MacArm, err := h.SHA256(z.AssetPathMacArm.Path()) |
69 |
| - if err != nil { |
70 |
| - l.Debug("Unable to calculate SHA sum of the asset", esl.Error(err)) |
71 |
| - return "", err |
72 |
| - } |
73 |
| - |
74 |
| - resBundle := app_resource.Bundle() |
75 |
| - formulaSrc, err := resBundle.Templates().Bytes("homebrew-toolbox.rb.tmpl") |
76 |
| - if err != nil { |
77 |
| - l.Debug("Unable to find a template resource", esl.Error(err)) |
78 |
| - return "", err |
79 |
| - } |
80 |
| - |
81 |
| - formulaTmpl, err := template.New("formula").Parse(string(formulaSrc)) |
82 |
| - if err != nil { |
83 |
| - l.Debug("Unable to parse", esl.Error(err)) |
84 |
| - return "", err |
85 |
| - } |
86 |
| - |
87 |
| - var buf bytes.Buffer |
88 |
| - err = formulaTmpl.Execute(&buf, map[string]string{ |
89 |
| - "Version": app_definitions.BuildId, |
90 |
| - "DownloadUrlLinuxArm": z.DownloadUrlLinuxArm, |
91 |
| - "DownloadUrlLinuxIntel": z.DownloadUrlLinuxIntel, |
92 |
| - "DownloadUrlMacArm": z.DownloadUrlMacArm, |
93 |
| - "DownloadUrlMacIntel": z.DownloadUrlMacIntel, |
94 |
| - "Sha256LinuxArm": sha256LinuxArm, |
95 |
| - "Sha256LinuxIntel": sha256LinuxIntel, |
96 |
| - "Sha256MacArm": sha256MacArm, |
97 |
| - "Sha256MacIntel": sha256MacIntel, |
98 |
| - }) |
99 |
| - if err != nil { |
100 |
| - l.Debug("Unable to compile template", esl.Error(err)) |
101 |
| - return "", err |
102 |
| - } |
103 |
| - return buf.String(), nil |
| 49 | + l := c.Log() |
| 50 | + h := es_filehash.NewHash(l) |
| 51 | + sha256LinuxIntel, err := h.SHA256(z.AssetPathLinuxIntel.Path()) |
| 52 | + if err != nil { |
| 53 | + l.Debug("Unable to calculate SHA sum of the asset", esl.Error(err)) |
| 54 | + return "", err |
| 55 | + } |
| 56 | + sha256LinuxArm, err := h.SHA256(z.AssetPathLinuxArm.Path()) |
| 57 | + if err != nil { |
| 58 | + l.Debug("Unable to calculate SHA sum of the asset", esl.Error(err)) |
| 59 | + return "", err |
| 60 | + } |
| 61 | + sha256MacArm, err := h.SHA256(z.AssetPathMacArm.Path()) |
| 62 | + if err != nil { |
| 63 | + l.Debug("Unable to calculate SHA sum of the asset", esl.Error(err)) |
| 64 | + return "", err |
| 65 | + } |
| 66 | + |
| 67 | + resBundle := app_resource.Bundle() |
| 68 | + formulaSrc, err := resBundle.Templates().Bytes("homebrew-toolbox.rb.tmpl") |
| 69 | + if err != nil { |
| 70 | + l.Debug("Unable to find a template resource", esl.Error(err)) |
| 71 | + return "", err |
| 72 | + } |
| 73 | + |
| 74 | + formulaTmpl, err := template.New("formula").Parse(string(formulaSrc)) |
| 75 | + if err != nil { |
| 76 | + l.Debug("Unable to parse", esl.Error(err)) |
| 77 | + return "", err |
| 78 | + } |
| 79 | + |
| 80 | + var buf bytes.Buffer |
| 81 | + err = formulaTmpl.Execute(&buf, map[string]string{ |
| 82 | + "Version": app_definitions.BuildId, |
| 83 | + "DownloadUrlLinuxArm": z.DownloadUrlLinuxArm, |
| 84 | + "DownloadUrlLinuxIntel": z.DownloadUrlLinuxIntel, |
| 85 | + "DownloadUrlMacArm": z.DownloadUrlMacArm, |
| 86 | + "Sha256LinuxArm": sha256LinuxArm, |
| 87 | + "Sha256LinuxIntel": sha256LinuxIntel, |
| 88 | + "Sha256MacArm": sha256MacArm, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + l.Debug("Unable to compile template", esl.Error(err)) |
| 92 | + return "", err |
| 93 | + } |
| 94 | + return buf.String(), nil |
104 | 95 | }
|
105 | 96 |
|
106 | 97 | func (z *Formula) getCurrentSha(c app_control.Control) (sha string, err error) {
|
107 |
| - l := c.Log() |
108 |
| - svc := sv_content.New(z.Peer.Client(), z.Owner, z.Repository) |
109 |
| - opts := make([]sv_content.ContentOpt, 0) |
110 |
| - opts = append(opts, sv_content.Ref(z.Branch)) |
111 |
| - |
112 |
| - cts, err := svc.Get(z.FormulaName, opts...) |
113 |
| - if err != nil { |
114 |
| - l.Debug("Unable to retrieve content metadata", esl.Error(err)) |
115 |
| - return "", err |
116 |
| - } |
117 |
| - if f, ok := cts.File(); ok { |
118 |
| - l.Debug("Content metadata", esl.Any("file", f)) |
119 |
| - return f.Sha, nil |
120 |
| - } |
121 |
| - l.Debug("not a file", esl.Any("cts", cts)) |
122 |
| - return "", ErrorNotAFile |
| 98 | + l := c.Log() |
| 99 | + svc := sv_content.New(z.Peer.Client(), z.Owner, z.Repository) |
| 100 | + opts := make([]sv_content.ContentOpt, 0) |
| 101 | + opts = append(opts, sv_content.Ref(z.Branch)) |
| 102 | + |
| 103 | + cts, err := svc.Get(z.FormulaName, opts...) |
| 104 | + if err != nil { |
| 105 | + l.Debug("Unable to retrieve content metadata", esl.Error(err)) |
| 106 | + return "", err |
| 107 | + } |
| 108 | + if f, ok := cts.File(); ok { |
| 109 | + l.Debug("Content metadata", esl.Any("file", f)) |
| 110 | + return f.Sha, nil |
| 111 | + } |
| 112 | + l.Debug("not a file", esl.Any("cts", cts)) |
| 113 | + return "", ErrorNotAFile |
123 | 114 | }
|
124 | 115 |
|
125 | 116 | func (z *Formula) updateFormula(c app_control.Control, formula, sha string) error {
|
126 |
| - l := c.Log() |
127 |
| - svc := sv_content.New(z.Peer.Client(), z.Owner, z.Repository) |
128 |
| - opts := make([]sv_content.ContentOpt, 0) |
129 |
| - opts = append(opts, sv_content.Branch(z.Branch)) |
130 |
| - opts = append(opts, sv_content.Sha(sha)) |
131 |
| - |
132 |
| - cts, commit, err := svc.Put(z.FormulaName, z.Message, formula, opts...) |
133 |
| - if err != nil { |
134 |
| - l.Debug("Unable to commit the change", esl.Error(err)) |
135 |
| - return err |
136 |
| - } |
137 |
| - l.Debug("contents metadata", esl.Any("contents", cts)) |
138 |
| - l.Debug("commit metadata", esl.Any("commit", commit)) |
139 |
| - |
140 |
| - z.Commit.Row(commit) |
141 |
| - |
142 |
| - return nil |
| 117 | + l := c.Log() |
| 118 | + svc := sv_content.New(z.Peer.Client(), z.Owner, z.Repository) |
| 119 | + opts := make([]sv_content.ContentOpt, 0) |
| 120 | + opts = append(opts, sv_content.Branch(z.Branch)) |
| 121 | + opts = append(opts, sv_content.Sha(sha)) |
| 122 | + |
| 123 | + cts, commit, err := svc.Put(z.FormulaName, z.Message, formula, opts...) |
| 124 | + if err != nil { |
| 125 | + l.Debug("Unable to commit the change", esl.Error(err)) |
| 126 | + return err |
| 127 | + } |
| 128 | + l.Debug("contents metadata", esl.Any("contents", cts)) |
| 129 | + l.Debug("commit metadata", esl.Any("commit", commit)) |
| 130 | + |
| 131 | + z.Commit.Row(commit) |
| 132 | + |
| 133 | + return nil |
143 | 134 | }
|
144 | 135 |
|
145 | 136 | func (z *Formula) Exec(c app_control.Control) error {
|
146 |
| - if err := z.Commit.Open(); err != nil { |
147 |
| - return err |
148 |
| - } |
| 137 | + if err := z.Commit.Open(); err != nil { |
| 138 | + return err |
| 139 | + } |
149 | 140 |
|
150 |
| - formula, err := z.makeFormula(c) |
151 |
| - if err != nil { |
152 |
| - return err |
153 |
| - } |
| 141 | + formula, err := z.makeFormula(c) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
154 | 145 |
|
155 |
| - sha, err := z.getCurrentSha(c) |
156 |
| - if err != nil { |
157 |
| - return err |
158 |
| - } |
| 146 | + sha, err := z.getCurrentSha(c) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
159 | 150 |
|
160 |
| - return z.updateFormula(c, formula, sha) |
| 151 | + return z.updateFormula(c, formula, sha) |
161 | 152 | }
|
162 | 153 |
|
163 | 154 | func (z *Formula) Test(c app_control.Control) error {
|
164 |
| - root, err := es_project.DetectRepositoryRoot() |
165 |
| - if err != nil { |
166 |
| - return err |
167 |
| - } |
168 |
| - |
169 |
| - return rc_exec.Exec(c, &Formula{}, func(r rc_recipe.Recipe) { |
170 |
| - m := r.(*Formula) |
171 |
| - m.AssetPathLinuxArm = mo_path.NewExistingFileSystemPath(filepath.Join(root, "SECURITY_AND_PRIVACY.md")) |
172 |
| - m.AssetPathLinuxIntel = mo_path.NewExistingFileSystemPath(filepath.Join(root, "README.md")) |
173 |
| - m.AssetPathMacArm = mo_path.NewExistingFileSystemPath(filepath.Join(root, "LICENSE.md")) |
174 |
| - m.AssetPathMacIntel = mo_path.NewExistingFileSystemPath(filepath.Join(root, "CONTRIBUTING.md")) |
175 |
| - m.DownloadUrlLinuxArm = "https://raw.githubusercontent.com/watermint/toolbox/master/SECURITY_AND_PRIVACY.md" |
176 |
| - m.DownloadUrlLinuxIntel = "https://raw.githubusercontent.com/watermint/toolbox/master/README.md" |
177 |
| - m.DownloadUrlMacArm = "https://raw.githubusercontent.com/watermint/toolbox/master/LICENSE.md" |
178 |
| - m.DownloadUrlMacIntel = "https://raw.githubusercontent.com/watermint/toolbox/master/CONTRIBUTING.md" |
179 |
| - m.Message = "Release:" + time.Now().Format(time.RFC3339) |
180 |
| - m.FormulaName = "toolbox.rb" |
181 |
| - m.Branch = "current" |
182 |
| - m.Owner = "watermint" |
183 |
| - m.Repository = "toolbox_sandbox" |
184 |
| - }) |
| 155 | + root, err := es_project.DetectRepositoryRoot() |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + |
| 160 | + return rc_exec.Exec(c, &Formula{}, func(r rc_recipe.Recipe) { |
| 161 | + m := r.(*Formula) |
| 162 | + m.AssetPathLinuxArm = mo_path.NewExistingFileSystemPath(filepath.Join(root, "SECURITY_AND_PRIVACY.md")) |
| 163 | + m.AssetPathLinuxIntel = mo_path.NewExistingFileSystemPath(filepath.Join(root, "README.md")) |
| 164 | + m.AssetPathMacArm = mo_path.NewExistingFileSystemPath(filepath.Join(root, "LICENSE.md")) |
| 165 | + m.DownloadUrlLinuxArm = "https://raw.githubusercontent.com/watermint/toolbox/master/SECURITY_AND_PRIVACY.md" |
| 166 | + m.DownloadUrlLinuxIntel = "https://raw.githubusercontent.com/watermint/toolbox/master/README.md" |
| 167 | + m.DownloadUrlMacArm = "https://raw.githubusercontent.com/watermint/toolbox/master/LICENSE.md" |
| 168 | + m.Message = "Release:" + time.Now().Format(time.RFC3339) |
| 169 | + m.FormulaName = "toolbox.rb" |
| 170 | + m.Branch = "current" |
| 171 | + m.Owner = "watermint" |
| 172 | + m.Repository = "toolbox_sandbox" |
| 173 | + }) |
185 | 174 | }
|
0 commit comments