go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/cmd/isolate/isolateimpl/remap.go (about)

     1  // Copyright 2020 The LUCI Authors.
     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 isolateimpl
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/maruel/subcommands"
    22  	"go.chromium.org/luci/client/isolate"
    23  	"go.chromium.org/luci/common/errors"
    24  )
    25  
    26  // CmdRemap returns an object for the `remap` subcommand.
    27  func CmdRemap() *subcommands.Command {
    28  	return &subcommands.Command{
    29  		UsageLine: "remap <options>",
    30  
    31  		ShortDesc: "Creates a directory with all the dependencies mapped into it.",
    32  		LongDesc: `Creates a directory with all the dependencies mapped into it.
    33  
    34  Useful to test manually why a test is failing. The target executable is not run.`,
    35  		CommandRun: func() subcommands.CommandRun {
    36  			r := remapRun{}
    37  			r.baseCommandRun.Init()
    38  			r.isolateFlags.Init(&r.Flags)
    39  			r.Flags.StringVar(&r.outdir, "outdir", "", "Directory used to recreate the tree.")
    40  			return &r
    41  		},
    42  	}
    43  }
    44  
    45  type remapRun struct {
    46  	baseCommandRun
    47  	isolateFlags
    48  
    49  	outdir string
    50  }
    51  
    52  func (r *remapRun) Parse(a subcommands.Application, args []string) error {
    53  	if err := r.baseCommandRun.Parse(); err != nil {
    54  		return err
    55  	}
    56  	cwd, err := os.Getwd()
    57  	if err != nil {
    58  		return err
    59  	}
    60  	if err := r.isolateFlags.Parse(cwd); err != nil {
    61  		return err
    62  	}
    63  
    64  	if len(args) != 0 {
    65  		return errors.Reason("position arguments not expected").Err()
    66  	}
    67  
    68  	if r.outdir == "" {
    69  		return errors.Reason("-outdir is not specified").Err()
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func (r *remapRun) Run(a subcommands.Application, args []string, _ subcommands.Env) int {
    76  	if err := r.Parse(a, args); err != nil {
    77  		fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
    78  		return 1
    79  	}
    80  	if err := r.main(a, args); err != nil {
    81  		fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
    82  		return 1
    83  	}
    84  	return 0
    85  }
    86  
    87  func (r *remapRun) main(a subcommands.Application, args []string) error {
    88  	deps, rootDir, err := isolate.ProcessIsolate(&r.ArchiveOptions)
    89  	if err != nil {
    90  		return errors.Annotate(err, "failed to process isolate").Err()
    91  	}
    92  
    93  	return recreateTree(r.outdir, rootDir, deps)
    94  }