github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/gunit/cmd/clean/clean.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 clean
    16  
    17  import (
    18  	"os"
    19  	"path"
    20  
    21  	"github.com/nmiyake/pkg/dirs"
    22  	"github.com/palantir/pkg/cli"
    23  	"github.com/palantir/pkg/cli/cfgcli"
    24  	"github.com/palantir/pkg/cli/flag"
    25  	"github.com/pkg/errors"
    26  
    27  	"github.com/palantir/godel/apps/gunit/cmd"
    28  	"github.com/palantir/godel/apps/gunit/config"
    29  )
    30  
    31  var pkgsParamName = "packages"
    32  
    33  func Command() cli.Command {
    34  	return cli.Command{
    35  		Name:  "clean",
    36  		Usage: "Remove any 'tmp_placeholder_test.go' files in the project",
    37  		Flags: []flag.Flag{
    38  			flag.StringSlice{
    39  				Name:     pkgsParamName,
    40  				Usage:    "Packages for which 'tmp_placeholder_test.go' files should be cleaned",
    41  				Optional: true,
    42  			},
    43  		},
    44  		Action: func(ctx cli.Context) error {
    45  			cfg, err := config.Load(cfgcli.ConfigPath, cfgcli.ConfigJSON)
    46  			if err != nil {
    47  				return err
    48  			}
    49  			wd, err := dirs.GetwdEvalSymLinks()
    50  			if err != nil {
    51  				return err
    52  			}
    53  			pkgs, err := cmd.PkgPaths(ctx.Slice(pkgsParamName), wd, cfg.Exclude)
    54  			if err != nil {
    55  				return err
    56  			}
    57  			return run(pkgs, wd)
    58  		},
    59  	}
    60  }
    61  
    62  func run(pkgDirs []string, wd string) error {
    63  	for _, currPkg := range pkgDirs {
    64  		tmpPlaceholder := path.Join(wd, currPkg, "tmp_placeholder_test.go")
    65  		if err := os.Remove(tmpPlaceholder); err != nil && !os.IsNotExist(err) {
    66  			return errors.Wrapf(err, "failed to delete placeholder file %s", tmpPlaceholder)
    67  		}
    68  	}
    69  	return nil
    70  }