Skip to content
This repository
Browse code

Refactor.

  • Loading branch information...
commit e5b3c85a6df8cf754fe22f9cd4f32578158d55ae 1 parent f030cca
Alexey Palazhchenko authored
108 nut/base.go
@@ -6,6 +6,7 @@ import (
6 6 "encoding/json"
7 7 "fmt"
8 8 "go/build"
  9 + "io"
9 10 "io/ioutil"
10 11 "log"
11 12 "net/url"
@@ -21,6 +22,7 @@ import (
21 22 type ConfigFile struct {
22 23 Token string
23 24 V bool
  25 + Debug bool
24 26 }
25 27
26 28 const (
@@ -94,25 +96,47 @@ func init() {
94 96 }
95 97 }
96 98
  99 + // set logger flags
  100 + if Config.Debug {
  101 + log.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)
  102 + }
  103 +
97 104 // for development
98 105 env := os.Getenv("GONUTS_IO_SERVER")
99 106 if env != "" {
100 107 u, err := url.Parse(env)
101   - PanicIfErr(err)
  108 + FatalIfErr(err)
102 109 NutImportPrefixes["gonuts.io"] = u.Host
103 110 }
104 111 }
105 112
106   -func PanicIfErr(err error) {
  113 +func FatalIfErr(err error) {
107 114 if err != nil {
108   - log.Panic(err)
  115 + if Config.Debug {
  116 + // show full backtraces
  117 + log.Panic(err)
  118 + } else {
  119 + log.Fatal(err)
  120 + }
109 121 }
110 122 }
111 123
112   -func FatalIfErr(err error) {
113   - if err != nil {
114   - log.Fatal(err)
  124 +// Call 'go install <path>'.
  125 +func InstallPackage(path string, verbose bool) {
  126 + args := []string{"install"}
  127 + if verbose {
  128 + args = append(args, "-v")
115 129 }
  130 + args = append(args, path)
  131 + c := exec.Command("go", args...)
  132 + if verbose {
  133 + log.Printf("Running %q", strings.Join(c.Args, " "))
  134 + }
  135 + out, err := c.CombinedOutput()
  136 + if verbose || err != nil {
  137 + log.Print(string(out))
  138 + }
  139 + FatalIfErr(err)
116 140 }
117 141
118 142 // TODO common functions below are mess for now
@@ -124,7 +148,7 @@ func ReadNut(fileName string) (b []byte, nf *NutFile) {
124 148 FatalIfErr(err)
125 149 nf = new(NutFile)
126 150 _, err = nf.ReadFrom(bytes.NewReader(b))
127   - PanicIfErr(err)
  151 + FatalIfErr(err)
128 152 return
129 153 }
130 154
@@ -132,18 +156,18 @@ func ReadNut(fileName string) (b []byte, nf *NutFile) {
132 156 func WriteNut(b []byte, prefix string, verbose bool) string {
133 157 nf := new(NutFile)
134 158 _, err := nf.ReadFrom(bytes.NewReader(b))
135   - PanicIfErr(err)
  159 + FatalIfErr(err)
136 160
137 161 // create GOPATH/nut/<prefix>
138 162 dir := filepath.Join(NutDir, prefix)
139   - PanicIfErr(os.MkdirAll(dir, WorkspaceDirPerm))
  163 + FatalIfErr(os.MkdirAll(dir, WorkspaceDirPerm))
140 164
141 165 // write file
142 166 dstFilepath := filepath.Join(dir, nf.FileName())
143 167 if verbose {
144 168 log.Printf("Writing %s ...", dstFilepath)
145 169 }
146   - PanicIfErr(ioutil.WriteFile(dstFilepath, b, NutFilePerm))
  170 + FatalIfErr(ioutil.WriteFile(dstFilepath, b, NutFilePerm))
147 171 return dstFilepath
148 172 }
149 173
@@ -151,17 +175,17 @@ func WriteNut(b []byte, prefix string, verbose bool) string {
151 175 func PackNut(fileName string, files []string, verbose bool) {
152 176 // write nut to temporary file first
153 177 nutFile, err := ioutil.TempFile("", "nut-")
154   - PanicIfErr(err)
  178 + FatalIfErr(err)
155 179 defer func() {
156 180 if nutFile != nil {
157   - PanicIfErr(os.Remove(nutFile.Name()))
  181 + FatalIfErr(os.Remove(nutFile.Name()))
158 182 }
159 183 }()
160 184
161 185 nutWriter := zip.NewWriter(nutFile)
162 186 defer func() {
163 187 if nutWriter != nil {
164   - PanicIfErr(nutWriter.Close())
  188 + FatalIfErr(nutWriter.Close())
165 189 }
166 190 }()
167 191
@@ -175,24 +199,24 @@ func PackNut(fileName string, files []string, verbose bool) {
175 199 FatalIfErr(err)
176 200
177 201 fh, err := zip.FileInfoHeader(fi)
178   - PanicIfErr(err)
  202 + FatalIfErr(err)
179 203 fh.Name = file
180 204
181 205 f, err := nutWriter.CreateHeader(fh)
182   - PanicIfErr(err)
  206 + FatalIfErr(err)
183 207
184 208 b, err := ioutil.ReadFile(file)
185   - PanicIfErr(err)
  209 + FatalIfErr(err)
186 210
187 211 _, err = f.Write(b)
188   - PanicIfErr(err)
  212 + FatalIfErr(err)
189 213 }
190 214
191 215 err = nutWriter.Close()
192 216 nutWriter = nil
193   - PanicIfErr(err)
  217 + FatalIfErr(err)
194 218
195   - PanicIfErr(nutFile.Close())
  219 + FatalIfErr(nutFile.Close())
196 220
197 221 // move file to specified location and fix permissions
198 222 if verbose {
@@ -201,14 +225,15 @@ func PackNut(fileName string, files []string, verbose bool) {
201 225 _, err = os.Stat(fileName)
202 226 if err == nil {
203 227 // required on Windows
204   - PanicIfErr(os.Remove(fileName))
  228 + FatalIfErr(os.Remove(fileName))
205 229 }
206   - PanicIfErr(os.Rename(nutFile.Name(), fileName))
  230 + FatalIfErr(os.Rename(nutFile.Name(), fileName))
207 231 nutFile = nil
208   - PanicIfErr(os.Chmod(fileName, NutFilePerm))
  232 + FatalIfErr(os.Chmod(fileName, NutFilePerm))
209 233 }
210 234
211   -// Unpack nut file with given fileName into dir. Creates dir if needed. Removes dir first if asked.
  235 +// Unpack nut file with given fileName into dir, overwriting files.
  236 +// Creates dir if needed. Removes dir first if asked.
212 237 func UnpackNut(fileName string, dir string, removeDir, verbose bool) {
213 238 // check dir
214 239 _, err := os.Stat(dir)
@@ -218,42 +243,29 @@ func UnpackNut(fileName string, dir string, removeDir, verbose bool) {
218 243 }
219 244 os.RemoveAll(dir)
220 245 }
221   - PanicIfErr(os.MkdirAll(dir, WorkspaceDirPerm))
  246 + FatalIfErr(os.MkdirAll(dir, WorkspaceDirPerm))
222 247
223   - _, nf := ReadNut(fileName)
  248 + nf := new(NutFile)
  249 + err = nf.ReadFile(fileName)
  250 + FatalIfErr(err)
224 251
225 252 for _, file := range nf.Reader.File {
226 253 if verbose {
227 254 log.Printf("Unpacking %s ...", file.Name)
228 255 }
229 256
230   - rc, err := file.Open()
231   - PanicIfErr(err)
232   - defer rc.Close()
  257 + src, err := file.Open()
  258 + FatalIfErr(err)
233 259
234   - b, err := ioutil.ReadAll(rc)
235   - PanicIfErr(err)
  260 + dst, err := os.OpenFile(filepath.Join(dir, file.Name), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
  261 + FatalIfErr(err)
236 262
237   - PanicIfErr(ioutil.WriteFile(filepath.Join(dir, file.Name), b, file.Mode()))
238   - }
239   -}
  263 + _, err = io.Copy(dst, src)
  264 + FatalIfErr(err)
240 265
241   -// Call 'go install <path>'.
242   -func InstallPackage(path string, verbose bool) {
243   - args := []string{"install"}
244   - if verbose {
245   - args = append(args, "-v")
  266 + src.Close()
  267 + dst.Close()
246 268 }
247   - args = append(args, path)
248   - c := exec.Command("go", args...)
249   - if verbose {
250   - log.Printf("Running %q", strings.Join(c.Args, " "))
251   - }
252   - out, err := c.CombinedOutput()
253   - if verbose || err != nil {
254   - log.Print(string(out))
255   - }
256   - FatalIfErr(err)
257 269 }
258 270
259 271 // Return imports present in NutImportPrefixes without altering them.
4 nut/check.go
@@ -58,7 +58,9 @@ func runCheck(cmd *Command) {
58 58 errors = append(errors, CheckPackage(pack)...)
59 59
60 60 case "nut":
61   - _, nf := ReadNut(arg)
  61 + nf := new(NutFile)
  62 + err := nf.ReadFile(arg)
  63 + FatalIfErr(err)
62 64 errors = nf.Check()
63 65
64 66 default:
6 nut/generate.go
@@ -76,17 +76,17 @@ func runGenerate(cmd *Command) {
76 76
77 77 for _, glob := range globs {
78 78 files, err := filepath.Glob(glob)
79   - PanicIfErr(err)
  79 + FatalIfErr(err)
80 80 spec.ExtraFiles = append(spec.ExtraFiles, files...)
81 81 }
82 82 }
83 83
84 84 // write spec
85 85 f, err := os.OpenFile(SpecFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, SpecFilePerm)
86   - PanicIfErr(err)
  86 + FatalIfErr(err)
87 87 defer f.Close()
88 88 _, err = spec.WriteTo(f)
89   - PanicIfErr(err)
  89 + FatalIfErr(err)
90 90
91 91 if generateV {
92 92 log.Printf("%s %s.", SpecFileName, action)
6 nut/get.go
@@ -69,7 +69,7 @@ func ArgToURL(s string) *url.URL {
69 69
70 70 parse:
71 71 u, err := url.Parse(s)
72   - PanicIfErr(err)
  72 + FatalIfErr(err)
73 73 return u
74 74 }
75 75
@@ -118,7 +118,7 @@ func runGet(cmd *Command) {
118 118 // zero arguments is a special case – install dependencies for package in current directory
119 119 if len(args) == 0 {
120 120 pack, err := build.ImportDir(".", 0)
121   - PanicIfErr(err)
  121 + FatalIfErr(err)
122 122 args = NutImports(pack.Imports)
123 123 if getV && len(args) != 0 {
124 124 log.Printf("%s depends on nuts: %s", pack.Name, strings.Join(args, ","))
@@ -160,7 +160,7 @@ func runGet(cmd *Command) {
160 160 if p == "" {
161 161 if strings.Contains(url.Host, ":") {
162 162 p, _, err = net.SplitHostPort(url.Host)
163   - PanicIfErr(err)
  163 + FatalIfErr(err)
164 164 } else {
165 165 p = url.Host
166 166 }
4 nut/install.go
@@ -68,8 +68,8 @@ func runInstall(cmd *Command) {
68 68 if installV {
69 69 log.Printf("Copying %s to %s ...", arg, dstFile)
70 70 }
71   - PanicIfErr(os.MkdirAll(filepath.Dir(dstFile), WorkspaceDirPerm))
72   - PanicIfErr(ioutil.WriteFile(dstFile, b, NutFilePerm))
  71 + FatalIfErr(os.MkdirAll(filepath.Dir(dstFile), WorkspaceDirPerm))
  72 + FatalIfErr(ioutil.WriteFile(dstFile, b, NutFilePerm))
73 73
74 74 srcPath := filepath.Join(SrcDir, nf.ImportPath(installP))
75 75 if installV {
2  nut/main.go
... ... @@ -1,4 +1,4 @@
1   -// Package main implements 'nut' command. Code there is not considered to be a public API, and may change without notice.
  1 +// Package main implements 'nut' command (NOT A PUBLIC API).
2 2 package main
3 3
4 4 import (
2  nut/pack.go
@@ -57,7 +57,7 @@ func runPack(cmd *Command) {
57 57 ctxt := build.Default
58 58 ctxt.UseAllFiles = true
59 59 pack, err := ctxt.ImportDir(".", 0)
60   - PanicIfErr(err)
  60 + FatalIfErr(err)
61 61
62 62 if pack.Name == "main" {
63 63 log.Fatal(`Binaries (package "main") are not supported yet.`)
12 nut/publish.go
@@ -43,7 +43,7 @@ func runPublish(cmd *Command) {
43 43 }
44 44
45 45 url, err := url.Parse("http://" + NutImportPrefixes["gonuts.io"])
46   - PanicIfErr(err)
  46 + FatalIfErr(err)
47 47
48 48 url.RawQuery = "token=" + publishToken
49 49
@@ -55,17 +55,17 @@ func runPublish(cmd *Command) {
55 55 log.Printf("Putting %s to %s ...", arg, url)
56 56 }
57 57 req, err := http.NewRequest("PUT", url.String(), bytes.NewReader(b))
58   - PanicIfErr(err)
  58 + FatalIfErr(err)
59 59 req.Header.Set("User-Agent", "nut publisher")
60 60 req.Header.Set("Content-Type", "application/zip")
61 61 req.ContentLength = int64(len(b))
62 62
63 63 res, err := http.DefaultClient.Do(req)
64   - PanicIfErr(err)
65   -
  64 + FatalIfErr(err)
66 65 b, err = ioutil.ReadAll(res.Body)
67   - PanicIfErr(err)
68   - res.Body.Close()
  66 + FatalIfErr(err)
  67 + err = res.Body.Close()
  68 + FatalIfErr(err)
69 69
70 70 var body map[string]interface{}
71 71 err = json.Unmarshal(b, &body)
2  nut/unpack.go
@@ -53,7 +53,7 @@ func runUnpack(cmd *Command) {
53 53
54 54 // unpack nut
55 55 dir, err := os.Getwd()
56   - PanicIfErr(err)
  56 + FatalIfErr(err)
57 57 UnpackNut(fileName, dir, false, unpackV)
58 58 if unpackV {
59 59 log.Printf("%s unpacked.", fileName)

0 comments on commit e5b3c85

Please sign in to comment.
Something went wrong with that request. Please try again.