github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/run/cmd.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package run
    16  
    17  import (
    18  	"github.com/nmiyake/pkg/dirs"
    19  	"github.com/palantir/pkg/cli"
    20  	"github.com/palantir/pkg/cli/cfgcli"
    21  	"github.com/palantir/pkg/cli/flag"
    22  	"github.com/pkg/errors"
    23  
    24  	"github.com/palantir/godel/apps/distgo/cmd/build"
    25  	"github.com/palantir/godel/apps/distgo/config"
    26  )
    27  
    28  const (
    29  	productFlagName = "product"
    30  	argsFlagName    = "args"
    31  )
    32  
    33  func Command() cli.Command {
    34  	return cli.Command{
    35  		Name:  "run",
    36  		Usage: "Run a product in the project",
    37  		Flags: []flag.Flag{
    38  			flag.StringFlag{
    39  				Name:     productFlagName,
    40  				Usage:    "Product to run",
    41  				Required: false,
    42  			},
    43  			flag.StringSlice{
    44  				Name:     argsFlagName,
    45  				Usage:    "arguments to pass to product",
    46  				Optional: true,
    47  			},
    48  		},
    49  		Action: func(ctx cli.Context) error {
    50  			cfg, err := config.Load(cfgcli.ConfigPath, cfgcli.ConfigJSON)
    51  			if err != nil {
    52  				return err
    53  			}
    54  
    55  			var products []string
    56  			product := ctx.String(productFlagName)
    57  			if product != "" {
    58  				products = append(products, product)
    59  			}
    60  
    61  			wd, err := dirs.GetwdEvalSymLinks()
    62  			if err != nil {
    63  				return err
    64  			}
    65  
    66  			buildSpecsWithDeps, err := build.SpecsWithDepsForArgs(cfg, products, wd)
    67  			if err != nil {
    68  				return err
    69  			}
    70  
    71  			if len(buildSpecsWithDeps) > 1 {
    72  				programNames := make([]string, len(buildSpecsWithDeps))
    73  				for i, currSpec := range buildSpecsWithDeps {
    74  					programNames[i] = currSpec.Spec.ProductName
    75  				}
    76  				return errors.Errorf("more than one product exists, so product to run must be specified using the '--%v' flag: %v", productFlagName, programNames)
    77  			}
    78  
    79  			return DoRun(buildSpecsWithDeps[0].Spec, ctx.Slice(argsFlagName), ctx.App.Stdout, ctx.App.Stderr)
    80  		},
    81  	}
    82  }