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

     1  // Copyright 2015 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  // CmdCheck returns an object for the `check` subcommand.
    27  func CmdCheck() *subcommands.Command {
    28  	return &subcommands.Command{
    29  		UsageLine: "check <options>",
    30  		ShortDesc: "checks that all the inputs are present",
    31  		LongDesc:  "",
    32  		CommandRun: func() subcommands.CommandRun {
    33  			c := checkRun{}
    34  			c.baseCommandRun.Init()
    35  			c.isolateFlags.Init(&c.Flags)
    36  			return &c
    37  		},
    38  	}
    39  }
    40  
    41  type checkRun struct {
    42  	baseCommandRun
    43  	isolateFlags
    44  }
    45  
    46  func (c *checkRun) Parse(a subcommands.Application, args []string) error {
    47  	if err := c.baseCommandRun.Parse(); err != nil {
    48  		return err
    49  	}
    50  	cwd, err := os.Getwd()
    51  	if err != nil {
    52  		return err
    53  	}
    54  	if err := c.isolateFlags.Parse(cwd); err != nil {
    55  		return err
    56  	}
    57  	if len(args) != 0 {
    58  		return errors.Reason("position arguments not expected").Err()
    59  	}
    60  	return nil
    61  }
    62  
    63  func (c *checkRun) main(a subcommands.Application, args []string) error {
    64  	if !c.defaultFlags.Quiet {
    65  		fmt.Printf("Isolate:   %s\n", c.Isolate)
    66  		fmt.Printf("Config:    %s\n", c.ConfigVariables)
    67  		fmt.Printf("Path:      %s\n", c.PathVariables)
    68  	}
    69  
    70  	deps, _, err := isolate.ProcessIsolate(&c.ArchiveOptions)
    71  	if err != nil {
    72  		return errors.Annotate(err, "failed to process isolate").Err()
    73  	}
    74  
    75  	for _, dep := range deps {
    76  		_, err := os.Stat(dep)
    77  		if err != nil {
    78  			return errors.Annotate(err, "failed to call stat").Err()
    79  		}
    80  	}
    81  	return nil
    82  }
    83  
    84  func (c *checkRun) Run(a subcommands.Application, args []string, _ subcommands.Env) int {
    85  	if err := c.Parse(a, args); err != nil {
    86  		fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
    87  		return 1
    88  	}
    89  	if err := c.main(a, args); err != nil {
    90  		fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
    91  		return 1
    92  	}
    93  	return 0
    94  }