github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/cmd/jiri/which.go (about)

     1  // Copyright 2015 The Vanadium Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"os/exec"
    11  	"path/filepath"
    12  
    13  	"v.io/x/lib/cmdline"
    14  )
    15  
    16  var cmdWhich = &cmdline.Command{
    17  	Runner: cmdline.RunnerFunc(runWhich),
    18  	Name:   "which",
    19  	Short:  "Show path to the jiri tool",
    20  	Long: `
    21  Which behaves similarly to the unix commandline tool.  It is useful in
    22  determining whether the jiri binary is being run directly, or run via the jiri
    23  shim script.
    24  
    25  If the binary is being run directly, the output looks like this:
    26  
    27    # binary
    28    /path/to/binary/jiri
    29  
    30  If the script is being run, the output looks like this:
    31  
    32    # script
    33    /path/to/script/jiri
    34  `,
    35  }
    36  
    37  func runWhich(env *cmdline.Env, args []string) error {
    38  	if len(args) == 0 {
    39  		fmt.Fprintln(env.Stdout, "# binary")
    40  		path, err := exec.LookPath(os.Args[0])
    41  		if err != nil {
    42  			return err
    43  		}
    44  		abs, err := filepath.Abs(path)
    45  		if err != nil {
    46  			return err
    47  		}
    48  		fmt.Fprintln(env.Stdout, abs)
    49  		return nil
    50  	}
    51  	// TODO(toddw): Look up the path to each argument.  This will only be helpful
    52  	// after the profiles are moved back into the main jiri tool.
    53  	return fmt.Errorf("unexpected arguments")
    54  }