Skip to content
This repository has been archived by the owner on Jul 7, 2020. It is now read-only.

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Feb 24, 2013
1 parent f3462b7 commit 9535cd3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
17 changes: 5 additions & 12 deletions nut/base.go
Expand Up @@ -62,6 +62,7 @@ func init() {
WorkspaceDir = filepath.Join(SrcDir, "..")
NutDir = filepath.Join(WorkspaceDir, "nut")

// detect home dir
u, err := user.Current()
if err == nil {
_, err = os.Stat(u.HomeDir)
Expand All @@ -71,6 +72,7 @@ func init() {
return
}

// load config if file exists
path := filepath.Join(u.HomeDir, ConfigFileName)
b, err := ioutil.ReadFile(path)
if err == nil {
Expand All @@ -81,6 +83,7 @@ func init() {
Config = ConfigFile{}
}

// write config if file exists
if !os.IsNotExist(err) {
b, err = json.MarshalIndent(Config, "", " ")
if err == nil {
Expand All @@ -91,6 +94,7 @@ func init() {
}
}

// for development
env := os.Getenv("GONUTS_IO_SERVER")
if env != "" {
u, err := url.Parse(env)
Expand All @@ -111,18 +115,7 @@ func FatalIfErr(err error) {
}
}

// TODO common functions there are mess for now

// Read spec file.
func ReadSpec(fileName string) (spec *Spec) {
f, err := os.Open(fileName)
PanicIfErr(err)
defer f.Close()
spec = new(Spec)
_, err = spec.ReadFrom(f)
PanicIfErr(err)
return
}
// TODO common functions below are mess for now

// Read nut file.
func ReadNut(fileName string) (b []byte, nf *NutFile) {
Expand Down
4 changes: 3 additions & 1 deletion nut/check.go
Expand Up @@ -49,7 +49,9 @@ func runCheck(cmd *Command) {
parts := strings.Split(arg, ".")
switch parts[len(parts)-1] {
case "json":
spec := ReadSpec(arg)
spec := new(Spec)
err := spec.ReadFile(arg)
FatalIfErr(err)
pack, err := build.ImportDir(".", 0)
FatalIfErr(err)
errors = spec.Check()
Expand Down
5 changes: 3 additions & 2 deletions nut/generate.go
Expand Up @@ -49,11 +49,12 @@ func runGenerate(cmd *Command) {
var spec *Spec

// read spec
spec = new(Spec)
if _, err = os.Stat(SpecFileName); os.IsNotExist(err) {
action = "generated"
spec = new(Spec)
} else {
spec = ReadSpec(SpecFileName)
err = spec.ReadFile(SpecFileName)
FatalIfErr(err)
}

// read package
Expand Down
4 changes: 3 additions & 1 deletion nut/pack.go
Expand Up @@ -64,7 +64,9 @@ func runPack(cmd *Command) {
}

var fileName string
spec := ReadSpec(SpecFileName)
spec := new(Spec)
err = spec.ReadFile(SpecFileName)
FatalIfErr(err)
nut := Nut{Spec: *spec, Package: *pack}
if packO == "" {
fileName = nut.FileName()
Expand Down

0 comments on commit 9535cd3

Please sign in to comment.