github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/artifacts/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 artifacts
    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  
    23  	"github.com/palantir/godel/apps/distgo/cmd"
    24  	"github.com/palantir/godel/apps/distgo/cmd/build"
    25  	"github.com/palantir/godel/apps/distgo/config"
    26  	"github.com/palantir/godel/apps/distgo/params"
    27  )
    28  
    29  const (
    30  	absPathFlagName       = "absolute"
    31  	requiresBuildFlagName = "requires-build"
    32  )
    33  
    34  var (
    35  	absPathFlag = flag.BoolFlag{
    36  		Name:  absPathFlagName,
    37  		Usage: "Print the absolute path for artifacts",
    38  	}
    39  	requiresBuildFlag = flag.BoolFlag{
    40  		Name:  requiresBuildFlagName,
    41  		Usage: "If true, only prints the artifacts that require building (omits artifacts that are already built and are up-to-date)",
    42  	}
    43  )
    44  
    45  func Command() cli.Command {
    46  	return cli.Command{
    47  		Name:  "artifacts",
    48  		Usage: "Print the artifacts for products",
    49  		Subcommands: []cli.Command{
    50  			buildArtifactsCommand("build", "Print the paths to the build artifacts for products"),
    51  			artifactsCommand("dist", "Print the paths to the distribution artifacts for products", distArtifactsAction),
    52  		},
    53  	}
    54  }
    55  
    56  func artifactsCommand(name, usage string, action artifactsAction) cli.Command {
    57  	return cli.Command{
    58  		Name:  name,
    59  		Usage: usage,
    60  		Flags: []flag.Flag{
    61  			cmd.ProductsParam,
    62  			absPathFlag,
    63  		},
    64  		Action: func(ctx cli.Context) error {
    65  			cfg, err := config.Load(cfgcli.ConfigPath, cfgcli.ConfigJSON)
    66  			if err != nil {
    67  				return err
    68  			}
    69  			wd, err := dirs.GetwdEvalSymLinks()
    70  			if err != nil {
    71  				return err
    72  			}
    73  
    74  			specs, err := build.SpecsWithDepsForArgs(cfg, ctx.Slice(cmd.ProductsParamName), wd)
    75  			if err != nil {
    76  				return err
    77  			}
    78  
    79  			artifacts, err := action(ctx, specs, ctx.Bool(absPathFlagName))
    80  			if err != nil {
    81  				return err
    82  			}
    83  
    84  			for _, spec := range specs {
    85  				if v, ok := artifacts[spec.Spec.ProductName]; ok {
    86  					for _, k := range v.Keys() {
    87  						for _, currPath := range v.Get(k) {
    88  							ctx.Println(currPath)
    89  						}
    90  					}
    91  				}
    92  			}
    93  			return nil
    94  		},
    95  	}
    96  }
    97  
    98  // buildArtifactsCommand returns the result of calling artifactsCommand for the "build" command and adding flags to the
    99  // command that are only relevant for the "build" action.
   100  func buildArtifactsCommand(name, usage string) cli.Command {
   101  	buildCmd := artifactsCommand(name, usage, buildArtifactsAction)
   102  	buildCmd.Flags = append(buildCmd.Flags, cmd.OSArchFlag, requiresBuildFlag)
   103  	return buildCmd
   104  }
   105  
   106  type artifactsAction func(ctx cli.Context, specs []params.ProductBuildSpecWithDeps, absPath bool) (map[string]OrderedStringSliceMap, error)
   107  
   108  func buildArtifactsAction(ctx cli.Context, specs []params.ProductBuildSpecWithDeps, absPath bool) (map[string]OrderedStringSliceMap, error) {
   109  	osArchsFilter, err := cmd.NewOSArchFilter(ctx.String(cmd.OSArchFlagName))
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	return BuildArtifacts(specs, BuildArtifactsParams{
   114  		AbsPath:       absPath,
   115  		RequiresBuild: ctx.Bool(requiresBuildFlagName),
   116  		OSArchs:       osArchsFilter,
   117  	})
   118  }
   119  
   120  func distArtifactsAction(ctx cli.Context, specs []params.ProductBuildSpecWithDeps, absPath bool) (map[string]OrderedStringSliceMap, error) {
   121  	return DistArtifacts(specs, absPath)
   122  }