v.io/jiri: tidying up utilities routines

- move AtomicAction and friends into profiles/profilesutils

MultiPart: 1/2
Change-Id: I3dea2cc5a525c1f5a1898fe74595d516f8d380bb
diff --git a/gitutil/git.go b/gitutil/git.go
index 75a99e7..879cabd 100644
--- a/gitutil/git.go
+++ b/gitutil/git.go
@@ -16,6 +16,19 @@
 	"v.io/jiri/runutil"
 )
 
+// PlatformSpecificGitArgs returns a git command line with platform specific,
+// if any, modifications. The code is duplicated here because of the dependency
+// structure in the jiri tool.
+// TODO(cnicolaou,bprosnitz): remove this once ssl certs are installed.
+func platformSpecificGitArgs(args ...string) []string {
+	if os.Getenv("FNL_SYSTEM") != "" {
+		// TODO(bprosnitz) Remove this after certificates are installed on FNL
+		// Disable SSL verification because certificates are not present on FNL.func
+		return append([]string{"-c", "http.sslVerify=false"}, args...)
+	}
+	return args
+}
+
 type GitError struct {
 	args        []string
 	output      string
@@ -625,15 +638,6 @@
 	return major, minor, nil
 }
 
-func fnlArgs(args []string) []string {
-	if runutil.IsFNLHost() {
-		// TODO(bprosnitz) Remove this after certificates are installed on FNL
-		// Disable SSL verification because certificates are not present on FNL.
-		return append([]string{"-c", "http.sslVerify=false"}, args...)
-	}
-	return args
-}
-
 func (g *Git) run(args ...string) error {
 	var stdout, stderr bytes.Buffer
 	capture := func(s runutil.Sequence) runutil.Sequence { return s.Capture(&stdout, &stderr) }
@@ -688,7 +692,7 @@
 
 func (g *Git) runWithFn(fn func(s runutil.Sequence) runutil.Sequence, args ...string) error {
 	g.s.Dir(g.rootDir)
-	args = fnlArgs(args)
+	args = platformSpecificGitArgs(args...)
 	if fn == nil {
 		fn = func(s runutil.Sequence) runutil.Sequence { return s }
 	}
diff --git a/profiles/.api b/profiles/.api
index c315f3c..46d8e96 100644
--- a/profiles/.api
+++ b/profiles/.api
@@ -1,18 +1,13 @@
-pkg profiles, const DefaultDirPerm os.FileMode
-pkg profiles, const DefaultFilePerm os.FileMode
 pkg profiles, const Install Action
 pkg profiles, const Original Version
 pkg profiles, const Uninstall Action
 pkg profiles, const V2 Version
 pkg profiles, const V3 Version
 pkg profiles, const V4 Version
-pkg profiles, func AtomicAction(*jiri.X, func() error, string, string) error
 pkg profiles, func DefaultTarget() Target
-pkg profiles, func Fetch(*jiri.X, string, string) error
 pkg profiles, func FindTarget(Targets, *Target) *Target
 pkg profiles, func FindTargetWithDefault(Targets, *Target) *Target
 pkg profiles, func InsertTarget(Targets, *Target) Targets
-pkg profiles, func InstallPackages(*jiri.X, []string) error
 pkg profiles, func NativeTarget() Target
 pkg profiles, func NewDB() *DB
 pkg profiles, func NewTarget(string, ...string) (Target, error)
@@ -21,7 +16,6 @@
 pkg profiles, func RegisterTargetAndEnvFlags(*flag.FlagSet, *Target)
 pkg profiles, func RegisterTargetFlag(*flag.FlagSet, *Target)
 pkg profiles, func RemoveTarget(Targets, *Target) Targets
-pkg profiles, func Unzip(*jiri.X, string, string) error
 pkg profiles, method (*DB) AddProfileTarget(string, Target) error
 pkg profiles, method (*DB) EnvFromProfile(string, Target) []string
 pkg profiles, method (*DB) Filename() string
diff --git a/profiles/flags.go b/profiles/flags.go
new file mode 100644
index 0000000..53c1449
--- /dev/null
+++ b/profiles/flags.go
@@ -0,0 +1,33 @@
+// Copyright 2015 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package profiles
+
+import "flag"
+
+const (
+	targetDefValue = "<runtime.GOARCH>-<runtime.GOOS>"
+)
+
+// RegisterTargetAndEnvFlags registers the commonly used --target and --env
+// flags with the supplied FlagSet
+func RegisterTargetAndEnvFlags(flags *flag.FlagSet, target *Target) {
+	*target = DefaultTarget()
+	flags.Var(target, "target", target.Usage())
+	flags.Lookup("target").DefValue = targetDefValue
+	flags.Var(&target.commandLineEnv, "env", target.commandLineEnv.Usage())
+}
+
+// RegisterProfilesFlag registers the --profiles flag
+func RegisterProfilesFlag(flags *flag.FlagSet, profiles *string) {
+	flags.StringVar(profiles, "profiles", "base,jiri", "a comma separated list of profiles to use")
+}
+
+// RegisterTargetFlag registers the commonly used --target flag with
+// the supplied FlagSet.
+func RegisterTargetFlag(flags *flag.FlagSet, target *Target) {
+	*target = DefaultTarget()
+	flags.Var(target, "target", target.Usage())
+	flags.Lookup("target").DefValue = targetDefValue
+}
diff --git a/profiles/profilesutil/.api b/profiles/profilesutil/.api
new file mode 100644
index 0000000..b7d25d7
--- /dev/null
+++ b/profiles/profilesutil/.api
@@ -0,0 +1,7 @@
+pkg profilesutil, const DefaultDirPerm os.FileMode
+pkg profilesutil, const DefaultFilePerm os.FileMode
+pkg profilesutil, func AtomicAction(*jiri.X, func() error, string, string) error
+pkg profilesutil, func Fetch(*jiri.X, string, string) error
+pkg profilesutil, func InstallPackages(*jiri.X, []string) error
+pkg profilesutil, func IsFNLHost() bool
+pkg profilesutil, func Unzip(*jiri.X, string, string) error
diff --git a/profiles/util.go b/profiles/profilesutil/util.go
similarity index 78%
rename from profiles/util.go
rename to profiles/profilesutil/util.go
index 3a906b2..36f8065 100644
--- a/profiles/util.go
+++ b/profiles/profilesutil/util.go
@@ -2,13 +2,13 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package profiles
+// package profilesutil provides utility routines for implementing profiles.
+package profilesutil
 
 import (
 	"archive/zip"
 	"bufio"
 	"bytes"
-	"flag"
 	"fmt"
 	"net/http"
 	"os"
@@ -17,37 +17,22 @@
 	"strings"
 
 	"v.io/jiri/jiri"
-	"v.io/jiri/runutil"
 	"v.io/jiri/tool"
 )
 
 const (
-	// TODO(cnicolaou): move these to runutil
 	DefaultDirPerm  = os.FileMode(0755)
 	DefaultFilePerm = os.FileMode(0644)
-	targetDefValue  = "<runtime.GOARCH>-<runtime.GOOS>"
 )
 
-// RegisterTargetAndEnvFlags registers the commonly used --target and --env
-// flags with the supplied FlagSet
-func RegisterTargetAndEnvFlags(flags *flag.FlagSet, target *Target) {
-	*target = DefaultTarget()
-	flags.Var(target, "target", target.Usage())
-	flags.Lookup("target").DefValue = targetDefValue
-	flags.Var(&target.commandLineEnv, "env", target.commandLineEnv.Usage())
-}
-
-// RegisterProfilesFlag registers the --profiles flag
-func RegisterProfilesFlag(flags *flag.FlagSet, profiles *string) {
-	flags.StringVar(profiles, "profiles", "base,jiri", "a comma separated list of profiles to use")
-}
-
-// RegisterTargetFlag registers the commonly used --target flag with
-// the supplied FlagSet.
-func RegisterTargetFlag(flags *flag.FlagSet, target *Target) {
-	*target = DefaultTarget()
-	flags.Var(target, "target", target.Usage())
-	flags.Lookup("target").DefValue = targetDefValue
+// IsFNLHost returns true iff the host machine is running FNL
+// TODO(bprosnitz) We should find a better way to detect that the machine is
+// running FNL
+// TODO(bprosnitz) This is needed in part because fnl is not currently a
+// GOHOSTOS. This should probably be handled by having hosts that are separate
+// from GOHOSTOSs similarly to how targets are defined.
+func IsFNLHost() bool {
+	return os.Getenv("FNL_SYSTEM") != ""
 }
 
 // AtomicAction performs an action 'atomically' by keeping track of successfully
@@ -55,7 +40,6 @@
 // are not successfully logged therein after deleting the entire contents of the
 // dir parameter. Consequently it does not make sense to apply AtomicAction to
 // the same directory in sequence.
-// TODO(cnicolaou): move this to runutil
 func AtomicAction(jirix *jiri.X, installFn func() error, dir, message string) error {
 	atomicFn := func() error {
 		completionLogPath := filepath.Join(dir, ".complete")
@@ -102,13 +86,12 @@
 
 // InstallPackages identifies the packages that need to be installed
 // and installs them using the OS-specific package manager.
-// TODO(cnicolaou): move these to runutil
 func InstallPackages(jirix *jiri.X, pkgs []string) error {
 	installDepsFn := func() error {
 		s := jirix.NewSeq()
 		switch runtime.GOOS {
 		case "linux":
-			if runutil.IsFNLHost() {
+			if IsFNLHost() {
 				fmt.Fprintf(jirix.Stdout(), "skipping installation of %v on FNL host", pkgs)
 				fmt.Fprintf(jirix.Stdout(), "success\n")
 				break
@@ -155,9 +138,6 @@
 }
 
 // Fetch downloads the specified url and saves it to dst.
-// TODO(nlacasse, cnicoloau): Move this to a package for profile-implementors
-// so it does not pollute the profile package namespace.
-// TODO(cnicolaou): move these to runutil
 func Fetch(jirix *jiri.X, dst, url string) error {
 	s := jirix.NewSeq()
 	s.Output([]string{"fetching " + url})
@@ -178,9 +158,6 @@
 }
 
 // Unzip unzips the file in srcFile and puts resulting files in directory dstDir.
-// TODO(nlacasse, cnicoloau): Move this to a package for profile-implementors
-// so it does not pollute the profile package namespace.
-// TODO(cnicolaou): move these to runutil
 func Unzip(jirix *jiri.X, srcFile, dstDir string) error {
 	r, err := zip.OpenReader(srcFile)
 	if err != nil {
diff --git a/runutil/.api b/runutil/.api
index 8595b6f..fab8522 100644
--- a/runutil/.api
+++ b/runutil/.api
@@ -1,6 +1,5 @@
 pkg runutil, func GetOriginalError(error) error
 pkg runutil, func IsExist(error) bool
-pkg runutil, func IsFNLHost() bool
 pkg runutil, func IsNotExist(error) bool
 pkg runutil, func IsPermission(error) bool
 pkg runutil, func IsTimeout(error) bool
diff --git a/runutil/misc.go b/runutil/misc.go
deleted file mode 100644
index 478745b..0000000
--- a/runutil/misc.go
+++ /dev/null
@@ -1,16 +0,0 @@
-// Copyright 2015 The Vanadium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package runutil
-
-import "os"
-
-// IsFNLHost returns true iff the host machine is running FNL
-// TODO(bprosnitz) We should find a better way to detect that the machine is running FNL
-// TODO(bprosnitz) This is needed in part because fnl is not currently a GOHOSTOS. This should
-// probably be handled by having hosts that are separate from GOHOSTOSs similarly to how targets
-// are defined.
-func IsFNLHost() bool {
-	return os.Getenv("FNL_SYSTEM") != ""
-}