Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor.

  • Loading branch information...
commit e5b3c85a6df8cf754fe22f9cd4f32578158d55ae 1 parent f030cca
Alexey Palazhchenko authored
108 nut/base.go
View
@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"go/build"
+ "io"
"io/ioutil"
"log"
"net/url"
@@ -21,6 +22,7 @@ import (
type ConfigFile struct {
Token string
V bool
+ Debug bool
}
const (
@@ -94,25 +96,47 @@ func init() {
}
}
+ // set logger flags
+ if Config.Debug {
+ log.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)
+ }
+
// for development
env := os.Getenv("GONUTS_IO_SERVER")
if env != "" {
u, err := url.Parse(env)
- PanicIfErr(err)
+ FatalIfErr(err)
NutImportPrefixes["gonuts.io"] = u.Host
}
}
-func PanicIfErr(err error) {
+func FatalIfErr(err error) {
if err != nil {
- log.Panic(err)
+ if Config.Debug {
+ // show full backtraces
+ log.Panic(err)
+ } else {
+ log.Fatal(err)
+ }
}
}
-func FatalIfErr(err error) {
- if err != nil {
- log.Fatal(err)
+// Call 'go install <path>'.
+func InstallPackage(path string, verbose bool) {
+ args := []string{"install"}
+ if verbose {
+ args = append(args, "-v")
}
+ args = append(args, path)
+ c := exec.Command("go", args...)
+ if verbose {
+ log.Printf("Running %q", strings.Join(c.Args, " "))
+ }
+ out, err := c.CombinedOutput()
+ if verbose || err != nil {
+ log.Print(string(out))
+ }
+ FatalIfErr(err)
}
// TODO common functions below are mess for now
@@ -124,7 +148,7 @@ func ReadNut(fileName string) (b []byte, nf *NutFile) {
FatalIfErr(err)
nf = new(NutFile)
_, err = nf.ReadFrom(bytes.NewReader(b))
- PanicIfErr(err)
+ FatalIfErr(err)
return
}
@@ -132,18 +156,18 @@ func ReadNut(fileName string) (b []byte, nf *NutFile) {
func WriteNut(b []byte, prefix string, verbose bool) string {
nf := new(NutFile)
_, err := nf.ReadFrom(bytes.NewReader(b))
- PanicIfErr(err)
+ FatalIfErr(err)
// create GOPATH/nut/<prefix>
dir := filepath.Join(NutDir, prefix)
- PanicIfErr(os.MkdirAll(dir, WorkspaceDirPerm))
+ FatalIfErr(os.MkdirAll(dir, WorkspaceDirPerm))
// write file
dstFilepath := filepath.Join(dir, nf.FileName())
if verbose {
log.Printf("Writing %s ...", dstFilepath)
}
- PanicIfErr(ioutil.WriteFile(dstFilepath, b, NutFilePerm))
+ FatalIfErr(ioutil.WriteFile(dstFilepath, b, NutFilePerm))
return dstFilepath
}
@@ -151,17 +175,17 @@ func WriteNut(b []byte, prefix string, verbose bool) string {
func PackNut(fileName string, files []string, verbose bool) {
// write nut to temporary file first
nutFile, err := ioutil.TempFile("", "nut-")
- PanicIfErr(err)
+ FatalIfErr(err)
defer func() {
if nutFile != nil {
- PanicIfErr(os.Remove(nutFile.Name()))
+ FatalIfErr(os.Remove(nutFile.Name()))
}
}()
nutWriter := zip.NewWriter(nutFile)
defer func() {
if nutWriter != nil {
- PanicIfErr(nutWriter.Close())
+ FatalIfErr(nutWriter.Close())
}
}()
@@ -175,24 +199,24 @@ func PackNut(fileName string, files []string, verbose bool) {
FatalIfErr(err)
fh, err := zip.FileInfoHeader(fi)
- PanicIfErr(err)
+ FatalIfErr(err)
fh.Name = file
f, err := nutWriter.CreateHeader(fh)
- PanicIfErr(err)
+ FatalIfErr(err)
b, err := ioutil.ReadFile(file)
- PanicIfErr(err)
+ FatalIfErr(err)
_, err = f.Write(b)
- PanicIfErr(err)
+ FatalIfErr(err)
}
err = nutWriter.Close()
nutWriter = nil
- PanicIfErr(err)
+ FatalIfErr(err)
- PanicIfErr(nutFile.Close())
+ FatalIfErr(nutFile.Close())
// move file to specified location and fix permissions
if verbose {
@@ -201,14 +225,15 @@ func PackNut(fileName string, files []string, verbose bool) {
_, err = os.Stat(fileName)
if err == nil {
// required on Windows
- PanicIfErr(os.Remove(fileName))
+ FatalIfErr(os.Remove(fileName))
}
- PanicIfErr(os.Rename(nutFile.Name(), fileName))
+ FatalIfErr(os.Rename(nutFile.Name(), fileName))
nutFile = nil
- PanicIfErr(os.Chmod(fileName, NutFilePerm))
+ FatalIfErr(os.Chmod(fileName, NutFilePerm))
}
-// Unpack nut file with given fileName into dir. Creates dir if needed. Removes dir first if asked.
+// Unpack nut file with given fileName into dir, overwriting files.
+// Creates dir if needed. Removes dir first if asked.
func UnpackNut(fileName string, dir string, removeDir, verbose bool) {
// check dir
_, err := os.Stat(dir)
@@ -218,42 +243,29 @@ func UnpackNut(fileName string, dir string, removeDir, verbose bool) {
}
os.RemoveAll(dir)
}
- PanicIfErr(os.MkdirAll(dir, WorkspaceDirPerm))
+ FatalIfErr(os.MkdirAll(dir, WorkspaceDirPerm))
- _, nf := ReadNut(fileName)
+ nf := new(NutFile)
+ err = nf.ReadFile(fileName)
+ FatalIfErr(err)
for _, file := range nf.Reader.File {
if verbose {
log.Printf("Unpacking %s ...", file.Name)
}
- rc, err := file.Open()
- PanicIfErr(err)
- defer rc.Close()
+ src, err := file.Open()
+ FatalIfErr(err)
- b, err := ioutil.ReadAll(rc)
- PanicIfErr(err)
+ dst, err := os.OpenFile(filepath.Join(dir, file.Name), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
+ FatalIfErr(err)
- PanicIfErr(ioutil.WriteFile(filepath.Join(dir, file.Name), b, file.Mode()))
- }
-}
+ _, err = io.Copy(dst, src)
+ FatalIfErr(err)
-// Call 'go install <path>'.
-func InstallPackage(path string, verbose bool) {
- args := []string{"install"}
- if verbose {
- args = append(args, "-v")
+ src.Close()
+ dst.Close()
}
- args = append(args, path)
- c := exec.Command("go", args...)
- if verbose {
- log.Printf("Running %q", strings.Join(c.Args, " "))
- }
- out, err := c.CombinedOutput()
- if verbose || err != nil {
- log.Print(string(out))
- }
- FatalIfErr(err)
}
// Return imports present in NutImportPrefixes without altering them.
4 nut/check.go
View
@@ -58,7 +58,9 @@ func runCheck(cmd *Command) {
errors = append(errors, CheckPackage(pack)...)
case "nut":
- _, nf := ReadNut(arg)
+ nf := new(NutFile)
+ err := nf.ReadFile(arg)
+ FatalIfErr(err)
errors = nf.Check()
default:
6 nut/generate.go
View
@@ -76,17 +76,17 @@ func runGenerate(cmd *Command) {
for _, glob := range globs {
files, err := filepath.Glob(glob)
- PanicIfErr(err)
+ FatalIfErr(err)
spec.ExtraFiles = append(spec.ExtraFiles, files...)
}
}
// write spec
f, err := os.OpenFile(SpecFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, SpecFilePerm)
- PanicIfErr(err)
+ FatalIfErr(err)
defer f.Close()
_, err = spec.WriteTo(f)
- PanicIfErr(err)
+ FatalIfErr(err)
if generateV {
log.Printf("%s %s.", SpecFileName, action)
6 nut/get.go
View
@@ -69,7 +69,7 @@ func ArgToURL(s string) *url.URL {
parse:
u, err := url.Parse(s)
- PanicIfErr(err)
+ FatalIfErr(err)
return u
}
@@ -118,7 +118,7 @@ func runGet(cmd *Command) {
// zero arguments is a special case – install dependencies for package in current directory
if len(args) == 0 {
pack, err := build.ImportDir(".", 0)
- PanicIfErr(err)
+ FatalIfErr(err)
args = NutImports(pack.Imports)
if getV && len(args) != 0 {
log.Printf("%s depends on nuts: %s", pack.Name, strings.Join(args, ","))
@@ -160,7 +160,7 @@ func runGet(cmd *Command) {
if p == "" {
if strings.Contains(url.Host, ":") {
p, _, err = net.SplitHostPort(url.Host)
- PanicIfErr(err)
+ FatalIfErr(err)
} else {
p = url.Host
}
4 nut/install.go
View
@@ -68,8 +68,8 @@ func runInstall(cmd *Command) {
if installV {
log.Printf("Copying %s to %s ...", arg, dstFile)
}
- PanicIfErr(os.MkdirAll(filepath.Dir(dstFile), WorkspaceDirPerm))
- PanicIfErr(ioutil.WriteFile(dstFile, b, NutFilePerm))
+ FatalIfErr(os.MkdirAll(filepath.Dir(dstFile), WorkspaceDirPerm))
+ FatalIfErr(ioutil.WriteFile(dstFile, b, NutFilePerm))
srcPath := filepath.Join(SrcDir, nf.ImportPath(installP))
if installV {
2  nut/main.go
View
@@ -1,4 +1,4 @@
-// Package main implements 'nut' command. Code there is not considered to be a public API, and may change without notice.
+// Package main implements 'nut' command (NOT A PUBLIC API).
package main
import (
2  nut/pack.go
View
@@ -57,7 +57,7 @@ func runPack(cmd *Command) {
ctxt := build.Default
ctxt.UseAllFiles = true
pack, err := ctxt.ImportDir(".", 0)
- PanicIfErr(err)
+ FatalIfErr(err)
if pack.Name == "main" {
log.Fatal(`Binaries (package "main") are not supported yet.`)
12 nut/publish.go
View
@@ -43,7 +43,7 @@ func runPublish(cmd *Command) {
}
url, err := url.Parse("http://" + NutImportPrefixes["gonuts.io"])
- PanicIfErr(err)
+ FatalIfErr(err)
url.RawQuery = "token=" + publishToken
@@ -55,17 +55,17 @@ func runPublish(cmd *Command) {
log.Printf("Putting %s to %s ...", arg, url)
}
req, err := http.NewRequest("PUT", url.String(), bytes.NewReader(b))
- PanicIfErr(err)
+ FatalIfErr(err)
req.Header.Set("User-Agent", "nut publisher")
req.Header.Set("Content-Type", "application/zip")
req.ContentLength = int64(len(b))
res, err := http.DefaultClient.Do(req)
- PanicIfErr(err)
-
+ FatalIfErr(err)
b, err = ioutil.ReadAll(res.Body)
- PanicIfErr(err)
- res.Body.Close()
+ FatalIfErr(err)
+ err = res.Body.Close()
+ FatalIfErr(err)
var body map[string]interface{}
err = json.Unmarshal(b, &body)
2  nut/unpack.go
View
@@ -53,7 +53,7 @@ func runUnpack(cmd *Command) {
// unpack nut
dir, err := os.Getwd()
- PanicIfErr(err)
+ FatalIfErr(err)
UnpackNut(fileName, dir, false, unpackV)
if unpackV {
log.Printf("%s unpacked.", fileName)
Please sign in to comment.
Something went wrong with that request. Please try again.