From 03a7df63ce7007224ab7c0778e11580bbbea4cdb Mon Sep 17 00:00:00 2001 From: Manpreet Singh Date: Mon, 17 Nov 2014 16:21:28 -0800 Subject: [PATCH] Relax cgo dependency (os/user) to enable cross platform goxc builds to work --- cmd/cindex/cindex.go | 12 +++--------- index/read.go | 21 +++++++++++++++------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/cmd/cindex/cindex.go b/cmd/cindex/cindex.go index eac400a..2129f86 100644 --- a/cmd/cindex/cindex.go +++ b/cmd/cindex/cindex.go @@ -1,5 +1,5 @@ // Copyright 2011 The Go Authors. All rights reserved. -// Copyright 2013 Manpreet Singh ( junkblocker@yahoo.com ). All rights reserved. +// Copyright 2013-2014 Manpreet Singh ( junkblocker@yahoo.com ). All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -12,7 +12,6 @@ import ( "io/ioutil" "log" "os" - "os/user" "path/filepath" "runtime/pprof" "sort" @@ -260,19 +259,14 @@ func main() { if stat != nil && !stat.IsDir() && stat.Mode().IsRegular() { os.Remove(master) return - } else { - log.Fatal("Invalid index path " + master) } + log.Fatal("Invalid index path " + master) } if *exclude != "" { var excludePath string if (*exclude)[:2] == "~/" { - usr, err := user.Current() - if err != nil { - log.Fatal(err) - } - excludePath = filepath.Join(usr.HomeDir, (*exclude)[2:]) + excludePath = filepath.Join(index.HomeDir(), (*exclude)[2:]) } else { excludePath = *exclude } diff --git a/index/read.go b/index/read.go index 63ae92e..dd96e90 100644 --- a/index/read.go +++ b/index/read.go @@ -1,5 +1,5 @@ // Copyright 2011 The Go Authors. All rights reserved. -// Copyright 2013 Manpreet Singh ( junkblocker@yahoo.com ). All rights reserved. +// Copyright 2013-2014 Manpreet Singh ( junkblocker@yahoo.com ). All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -434,6 +434,19 @@ func (ix Index) Close() { ix.data.f.Close() } +// find home directory without cgo if needed +func HomeDir() string { + u, err := user.Current() + if err != nil { + h := os.Getenv("HOME") + if h == "" { + log.Fatal("Could not determine home directory") + } + return h + } + return u.HomeDir +} + // File returns the name of the index file to use. // It is either $CSEARCHINDEX or $HOME/.csearchindex. func File() string { @@ -441,9 +454,5 @@ func File() string { if f != "" { return f } - u, err := user.Current() - if err != nil { - log.Fatal(err) - } - return filepath.Join(u.HomeDir, ".csearchindex") + return filepath.Join(HomeDir(), ".csearchindex") }