| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // OS-agnostic helper functions which are called from run(). |
| 6 |
| 7 package firstrun |
| 8 |
| 9 import ( |
| 10 "errors" |
| 11 "os" |
| 12 "path/filepath" |
| 13 |
| 14 "infra/tools/cr/lib/terminal" |
| 15 ) |
| 16 |
| 17 var getenv = os.Getenv |
| 18 var stat = os.Stat |
| 19 |
| 20 // firstrunCheckNotInstalled is a sanity check to make sure that the user really |
| 21 // wants to do firstrun again, if it looks like cr is already installed. |
| 22 func firstrunCheckNotInstalled() (string, error) { |
| 23 terminal.Debug("Checking to make sure cr isn't already installed...") |
| 24 // Look for paths on $PATH that end in /cr/bin. |
| 25 pathenv := getenv("PATH") |
| 26 if pathenv == "" { |
| 27 return "", errors.New("no $PATH variable found in the environmen
t") |
| 28 } |
| 29 var possibleDirs []string |
| 30 for _, elem := range filepath.SplitList(pathenv) { |
| 31 head, bindir := filepath.Split(elem) |
| 32 crdir := filepath.Base(head) |
| 33 if crdir == "cr" && bindir == "bin" { |
| 34 possibleDirs = append(possibleDirs, elem) |
| 35 } |
| 36 } |
| 37 // Check to see if an executable called 'cr' is in those paths. |
| 38 var statErr error |
| 39 for _, dir := range possibleDirs { |
| 40 info, err := stat(filepath.Join(dir, executableName)) |
| 41 if os.IsNotExist(err) { |
| 42 continue |
| 43 } else if err != nil { |
| 44 statErr = err |
| 45 continue |
| 46 } |
| 47 if info.Mode()&0111 != 0 { |
| 48 return filepath.Join(dir, executableName), nil |
| 49 } |
| 50 } |
| 51 // We didn't find an executable; if we ran into an error, return that. |
| 52 return "", statErr |
| 53 } |
| OLD | NEW |