github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/d8/flagsparser.go (about)

     1  // Copyright 2023 Google LLC
     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 d8
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/args"
    22  	"github.com/bazelbuild/reclient/internal/pkg/inputprocessor/flags"
    23  )
    24  
    25  // parseFlags is used to transform an d8 command into a CommandFlags structure.
    26  func parseFlags(ctx context.Context, command []string, workingDir, execRoot string) (*flags.CommandFlags, error) {
    27  	numArgs := len(command)
    28  	if numArgs < 2 {
    29  		return nil, fmt.Errorf("insufficient number of arguments in command: %v", command)
    30  	}
    31  
    32  	res := &flags.CommandFlags{
    33  		ExecutablePath:   command[0],
    34  		WorkingDirectory: workingDir,
    35  		TargetFilePaths:  []string{command[numArgs-1]},
    36  		ExecRoot:         execRoot,
    37  	}
    38  
    39  	s := args.Scanner{
    40  		Args: command[1 : len(command)-1],
    41  		Flags: map[string]int{
    42  			"--lib":           1,
    43  			"--main-dex-list": 1,
    44  			"--output":        1,
    45  		},
    46  		Joined: []args.PrefixOption{
    47  			{"--main-dex-list=", 0},
    48  		},
    49  	}
    50  	for s.HasNext() {
    51  		flag, args, values, _ := s.Next()
    52  		switch flag {
    53  		case "--lib",
    54  			"--main-dex-list",
    55  			"--main-dex-list=":
    56  			res.Dependencies = append(res.Dependencies, values[0])
    57  			continue
    58  		case "--output":
    59  			res.OutputDirPaths = append(res.OutputDirPaths, values[0])
    60  			continue
    61  		}
    62  		res.Flags = append(res.Flags, &flags.Flag{Value: args[0]})
    63  	}
    64  	return res, nil
    65  }