...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/curl/parser.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/curl

     1  // Copyright 2021 Chaos Mesh 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  
    16  package curl
    17  
    18  import (
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/pkg/errors"
    23  	flag "github.com/spf13/pflag"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    26  )
    27  
    28  func parseCommands(command Commands) (*CommandFlags, error) {
    29  	flagset := flag.NewFlagSet("curl", flag.ContinueOnError)
    30  	flagset.ParseErrorsWhitelist.UnknownFlags = true
    31  
    32  	// these parts of flags are referenced to the manual of curl
    33  	flagset.BoolP("silent", "s", true, "silent mode")
    34  	location := flagset.BoolP("location", "L", false, "follow the location")
    35  	requestMethod := flagset.StringP("request", "X", "GET", "request method")
    36  	rawHeader := flagset.StringArrayP("header", "H", []string{}, "HTTP extra header")
    37  	data := flagset.StringP("data", "d", "", "data")
    38  	err := flagset.Parse(command)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	// first non-flag arg is the command itself, use the second non-flag arg as the url.
    44  	if flag.NArg() > 1 {
    45  		return nil, fmt.Errorf("can not find the url")
    46  	}
    47  	url := flagset.Arg(1)
    48  
    49  	isJson := false
    50  	var header Header
    51  	if len(*rawHeader) > 0 {
    52  		header = Header{}
    53  	}
    54  	for _, item := range *rawHeader {
    55  		k, v := parseHeader(item)
    56  		if k == HeaderContentType && v == ApplicationJson {
    57  			isJson = true
    58  			continue
    59  		}
    60  		header[k] = append(header[k], v)
    61  	}
    62  	if len(header) == 0 {
    63  		header = nil
    64  	}
    65  
    66  	return &CommandFlags{
    67  		Method:         *requestMethod,
    68  		URL:            url,
    69  		Header:         header,
    70  		Body:           *data,
    71  		FollowLocation: *location,
    72  		JsonContent:    isJson,
    73  	}, nil
    74  }
    75  
    76  func ParseWorkflowTaskTemplate(template *v1alpha1.Template) (*RequestForm, error) {
    77  	if !IsValidRenderedTask(template) {
    78  		return nil, errors.Errorf("invalid request, this task is not rendered by curl-render")
    79  	}
    80  	parsedFlags, err := parseCommands(template.Task.Container.Command)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	return &RequestForm{
    85  		CommandFlags: *parsedFlags,
    86  		Name:         template.Name,
    87  	}, nil
    88  }
    89  
    90  func IsValidRenderedTask(template *v1alpha1.Template) bool {
    91  	return template.Type == v1alpha1.TypeTask && strings.HasSuffix(template.Task.Container.Name, nameSuffix)
    92  }
    93  
    94  func parseHeader(headerKV string) (string, string) {
    95  	substring := strings.SplitN(headerKV, ":", 2)
    96  	return strings.TrimSpace(substring[0]), strings.TrimSpace(substring[1])
    97  }
    98