github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/cmd/jiri/package.go (about)

     1  // Copyright 2019 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  	"bytes"
     9  	"fmt"
    10  	"path/filepath"
    11  	"regexp"
    12  	"sort"
    13  	"text/template"
    14  
    15  	"github.com/btwiuse/jiri"
    16  	"github.com/btwiuse/jiri/cipd"
    17  	"github.com/btwiuse/jiri/cmdline"
    18  	"github.com/btwiuse/jiri/project"
    19  )
    20  
    21  // cmd represents the "jiri project" command.
    22  var cmdPackage = &cmdline.Command{
    23  	Runner: jiri.RunnerFunc(runPackageInfo),
    24  	Name:   "package",
    25  	Short:  "Display the jiri packages",
    26  	Long: `Display structured info on the existing
    27  	packages and branches. Packages are specified using either names or	regular
    28  	expressions that are matched against package names. If no command line
    29  	arguments are provided all projects will be used.`,
    30  	ArgsName: "<package ...>",
    31  	ArgsLong: "<package ...> is a list of packages to give info about.",
    32  }
    33  
    34  // packageInfoOutput defines JSON format for 'project info' output.
    35  type packageInfoOutput struct {
    36  	Name     string `json:"name"`
    37  	Path     string `json:"path"`
    38  	Version  string `json:"version"`
    39  	Manifest string `json:"manifest,omitempty"`
    40  }
    41  
    42  func init() {
    43  	cmdPackage.Flags.StringVar(&jsonOutputFlag, "json-output", "", "Path to write operation results to.")
    44  	cmdPackage.Flags.BoolVar(&regexpFlag, "regexp", false, "Use argument as regular expression.")
    45  }
    46  
    47  // runPackageInfo provides structured info on packages.
    48  func runPackageInfo(jirix *jiri.X, args []string) error {
    49  	var err error
    50  
    51  	regexps := make([]*regexp.Regexp, 0)
    52  	for _, arg := range args {
    53  		if !regexpFlag {
    54  			arg = "^" + regexp.QuoteMeta(arg) + "$"
    55  		}
    56  		if re, err := regexp.Compile(arg); err != nil {
    57  			return fmt.Errorf("failed to compile regexp %v: %v", arg, err)
    58  		} else {
    59  			regexps = append(regexps, re)
    60  		}
    61  	}
    62  
    63  	projects, err := project.LocalProjects(jirix, project.FastScan)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	_, _, pkgs, err := project.LoadManifestFile(jirix, jirix.JiriManifestFile(), projects, true)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	var keys project.PackageKeys
    72  	for k, v := range pkgs {
    73  		if len(args) == 0 {
    74  			keys = append(keys, k)
    75  		} else {
    76  			for _, re := range regexps {
    77  				if re.MatchString(v.Name) {
    78  					keys = append(keys, k)
    79  					break
    80  				}
    81  			}
    82  		}
    83  	}
    84  
    85  	sort.Sort(keys)
    86  
    87  	info := make([]packageInfoOutput, 0)
    88  	for _, key := range keys {
    89  		pkg := pkgs[key]
    90  		pkgPath, err := pkg.GetPath()
    91  		if err != nil {
    92  			return err
    93  		}
    94  		tmpl, err := template.New("pack").Parse(pkgPath)
    95  		if err != nil {
    96  			return fmt.Errorf("parsing package path %q failed", pkgPath)
    97  		}
    98  		var subdirBuf bytes.Buffer
    99  		// subdir is using fuchsia platform format instead of
   100  		// using cipd platform format
   101  		tmpl.Execute(&subdirBuf, cipd.FuchsiaPlatform(cipd.CipdPlatform))
   102  		pkgPath = filepath.Join(jirix.Root, subdirBuf.String())
   103  
   104  		info = append(info, packageInfoOutput{
   105  			Name:     pkg.Name,
   106  			Path:     pkgPath,
   107  			Version:  pkg.Version,
   108  			Manifest: pkg.ManifestPath,
   109  		})
   110  	}
   111  
   112  	for _, i := range info {
   113  		fmt.Printf("* package %s\n", i.Name)
   114  		fmt.Printf("  Path:     %s\n", i.Path)
   115  		fmt.Printf("  Version:  %s\n", i.Version)
   116  		fmt.Printf("  Manifest: %s\n", i.Manifest)
   117  	}
   118  
   119  	if jsonOutputFlag != "" {
   120  		if err := writeJSONOutput(info); err != nil {
   121  			return err
   122  		}
   123  	}
   124  
   125  	return nil
   126  }