...

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