How to use getArgOrFail method of main Package

Best Toxiproxy code snippet using main.getArgOrFail

cli.go

Source:cli.go Github

copy

Full Screen

...296 if proxyName == "" {297 cli.ShowSubcommandHelp(c)298 return errorf("Proxy name is required as the first argument.\n")299 }300 listen, err := getArgOrFail(c, "listen")301 if err != nil {302 return err303 }304 upstream, err := getArgOrFail(c, "upstream")305 if err != nil {306 return err307 }308 _, err = t.CreateProxy(proxyName, listen, upstream)309 if err != nil {310 return errorf("Failed to create proxy: %s\n", err.Error())311 }312 fmt.Printf("Created new proxy %s\n", proxyName)313 return nil314}315func deleteProxy(c *cli.Context, t *toxiproxy.Client) error {316 proxyName := c.Args().First()317 if proxyName == "" {318 cli.ShowSubcommandHelp(c)319 return errorf("Proxy name is required as the first argument.\n")320 }321 p, err := t.Proxy(proxyName)322 if err != nil {323 return errorf("Failed to retrieve proxy %s: %s\n", proxyName, err.Error())324 }325 err = p.Delete()326 if err != nil {327 return errorf("Failed to delete proxy: %s\n", err.Error())328 }329 fmt.Printf("Deleted proxy %s\n", proxyName)330 return nil331}332func parseToxicity(c *cli.Context, defaultToxicity float32) (float32, error) {333 var toxicity = defaultToxicity334 toxicityString := c.String("toxicity")335 if toxicityString != "" {336 tox, err := strconv.ParseFloat(toxicityString, 32)337 if err != nil || tox > 1 || tox < 0 {338 return 0, errorf("toxicity should be a float between 0 and 1.\n")339 }340 toxicity = float32(tox)341 }342 return toxicity, nil343}344func addToxic(c *cli.Context, t *toxiproxy.Client) error {345 proxyName := c.Args().First()346 if proxyName == "" {347 cli.ShowSubcommandHelp(c)348 return errorf("Proxy name is required as the first argument.\n")349 }350 toxicName := c.String("toxicName")351 toxicType, err := getArgOrFail(c, "type")352 if err != nil {353 return err354 }355 upstream := c.Bool("upstream")356 downstream := c.Bool("downstream")357 toxicity, err := parseToxicity(c, 1.0)358 if err != nil {359 return err360 }361 attributes := parseAttributes(c, "attribute")362 p, err := t.Proxy(proxyName)363 if err != nil {364 return errorf("Failed to retrieve proxy %s: %s\n", proxyName, err.Error())365 }366 addToxic := func(stream string) error {367 t, err := p.AddToxic(toxicName, toxicType, stream, toxicity, attributes)368 if err != nil {369 return errorf("Failed to add toxic: %s\n", err.Error())370 }371 toxicName = t.Name372 fmt.Printf("Added %s %s toxic '%s' on proxy '%s'\n", stream, toxicType, toxicName, proxyName)373 return nil374 }375 if upstream {376 err := addToxic("upstream")377 if err != nil {378 return err379 }380 }381 // Default to downstream.382 if downstream || (!downstream && !upstream) {383 return addToxic("downstream")384 }385 return nil386}387func updateToxic(c *cli.Context, t *toxiproxy.Client) error {388 proxyName := c.Args().First()389 if proxyName == "" {390 cli.ShowSubcommandHelp(c)391 return errorf("Proxy name is required as the first argument.\n")392 }393 toxicName, err := getArgOrFail(c, "toxicName")394 if err != nil {395 return err396 }397 attributes := parseAttributes(c, "attribute")398 p, err := t.Proxy(proxyName)399 if err != nil {400 return errorf("Failed to retrieve proxy %s: %s\n", proxyName, err.Error())401 }402 toxicity, err := parseToxicity(c, -1)403 if err != nil {404 return nil405 }406 _, err = p.UpdateToxic(toxicName, toxicity, attributes)407 if err != nil {408 return errorf("Failed to update toxic: %s\n", err.Error())409 }410 fmt.Printf("Updated toxic '%s' on proxy '%s'\n", toxicName, proxyName)411 return nil412}413func removeToxic(c *cli.Context, t *toxiproxy.Client) error {414 proxyName := c.Args().First()415 if proxyName == "" {416 cli.ShowSubcommandHelp(c)417 return errorf("Proxy name is required as the first argument.\n")418 }419 toxicName, err := getArgOrFail(c, "toxicName")420 if err != nil {421 return err422 }423 p, err := t.Proxy(proxyName)424 if err != nil {425 return errorf("Failed to retrieve proxy %s: %s\n", proxyName, err.Error())426 }427 err = p.RemoveToxic(toxicName)428 if err != nil {429 return errorf("Failed to remove toxic: %s\n", err.Error())430 }431 fmt.Printf("Removed toxic '%s' on proxy '%s'\n", toxicName, proxyName)432 return nil433}434func parseAttributes(c *cli.Context, name string) toxiproxy.Attributes {435 parsed := map[string]interface{}{}436 args := c.StringSlice(name)437 for _, raw := range args {438 kv := strings.SplitN(raw, "=", 2)439 if len(kv) < 2 {440 continue441 }442 if float, err := strconv.ParseFloat(kv[1], 64); err == nil {443 parsed[kv[0]] = float444 } else {445 parsed[kv[0]] = kv[1]446 }447 }448 return parsed449}450func colorEnabled(enabled bool) string {451 if enabled {452 return color(GREEN)453 }454 return color(RED)455}456func enabledText(enabled bool) string {457 if enabled {458 return "enabled"459 }460 return "disabled"461}462type attribute struct {463 key string464 value interface{}465}466type attributeList []attribute467func (a attributeList) Len() int { return len(a) }468func (a attributeList) Less(i, j int) bool { return a[i].key < a[j].key }469func (a attributeList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }470func sortedAttributes(attrs toxiproxy.Attributes) attributeList {471 li := make(attributeList, 0, len(attrs))472 for k, v := range attrs {473 li = append(li, attribute{k, v.(float64)})474 }475 sort.Sort(li)476 return li477}478func listToxics(toxics toxiproxy.Toxics, stream string) {479 if isTTY {480 fmt.Printf("%s%s toxics:\n%s", color(GREEN), stream, color(NONE))481 if len(toxics) == 0 {482 fmt.Printf("%sProxy has no %s toxics enabled.\n%s", color(RED), stream, color(NONE))483 return484 }485 }486 for _, t := range toxics {487 if isTTY {488 fmt.Printf("%s%s:%s\t", color(BLUE), t.Name, color(NONE))489 } else {490 fmt.Printf("%s\t", t.Name)491 }492 fmt.Printf("type=%s\t", t.Type)493 fmt.Printf("stream=%s\t", t.Stream)494 fmt.Printf("toxicity=%.2f\t", t.Toxicity)495 fmt.Printf("attributes=[")496 sorted := sortedAttributes(t.Attributes)497 for _, a := range sorted {498 fmt.Printf("\t%s=", a.key)499 fmt.Print(a.value)500 }501 fmt.Printf("\t]\n")502 }503}504func getArgOrFail(c *cli.Context, name string) (string, error) {505 arg := c.String(name)506 if arg == "" {507 cli.ShowSubcommandHelp(c)508 return "", errorf("Required argument '%s' was empty.\n", name)509 }510 return arg, nil511}512func hint(m string) {513 if isTTY {514 fmt.Printf("\n%sHint: %s\n", color(NONE), m)515 }516}517func errorf(m string, args ...interface{}) error {518 return cli.NewExitError(fmt.Sprintf(m, args...), 1)...

Full Screen

Full Screen

getArgOrFail

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 args := []string{"1", "2", "3"}4 arg := getArgOrFail(args, 2)5 fmt.Println(arg)6}7import (8func getArgOrFail(args []string, index int) string {9 if index > len(args) {10 fmt.Println("Index out of range")11 os.Exit(1)12 }13}

Full Screen

Full Screen

getArgOrFail

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(getArgOrFail(1))4}5import (6func main() {7 fmt.Println(getArgOrFail(1))8}9import (10func main() {11 fmt.Println(getArgOrFail(1))12}13import (14func main() {15 fmt.Println(getArgOrFail(1))16}17import (18func main() {19 fmt.Println(getArgOrFail(1))20}21import (22func main() {23 fmt.Println(getArgOrFail(1))24}25import (26func main() {27 fmt.Println(getArgOrFail(1))28}29import (30func main() {31 fmt.Println(getArgOrFail(1))32}33import (34func main() {35 fmt.Println(getArgOrFail(1))36}

Full Screen

Full Screen

getArgOrFail

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(getArgOrFail(1, "Please provide a filename"))4}5import (6func main() {7 fmt.Println(getArgOrFail(1, "Please provide a filename"))8}9import (10func main() {11 fmt.Println(getArgOrFail(1, "Please provide a filename"))12}13import (14func main() {15 fmt.Println(getArgOrFail(1, "Please provide a filename"))16}17import (18func main() {19 fmt.Println(getArgOrFail(1, "Please provide a filename"))20}21import (22func main() {23 fmt.Println(getArgOrFail(1, "Please provide a filename"))24}25import (26func main() {27 fmt.Println(getArgOrFail(1, "Please provide a filename"))28}29import (30func main() {31 fmt.Println(getArgOrFail(1, "Please provide a filename"))32}33import (34func main() {35 fmt.Println(getArgOrFail(1, "Please provide a filename"))36}37import (38func main() {39 fmt.Println(getArgOrFail(1, "Please provide a filename"))40}

Full Screen

Full Screen

getArgOrFail

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4 fmt.Println("Argument is", os.Args[1])5}6import (7func main() {8 fmt.Println("Hello World")9 fmt.Println("Argument is", os.Args[1])10}11import (12func main() {13 fmt.Println("Hello World")14 fmt.Println("Argument is", os.Args[1])15}16import (17func main() {18 fmt.Println("Hello World")19 fmt.Println("Argument is", os.Args[1])20}21import (22func main() {23 fmt.Println("Hello World")24 fmt.Println("Argument is", os.Args[1])25}26import (27func main() {28 fmt.Println("Hello World")29 fmt.Println("Argument is", os.Args[1])30}31import (32func main() {33 fmt.Println("Hello World")34 fmt.Println("Argument is", os.Args[1])35}36import (37func main() {38 fmt.Println("Hello World")39 fmt.Println("Argument is", os.Args[1])40}41import (42func main() {43 fmt.Println("Hello World")44 fmt.Println("Argument is", os.Args[1])45}

Full Screen

Full Screen

getArgOrFail

Using AI Code Generation

copy

Full Screen

1func main() {2 arg := getArgOrFail(1)3 fmt.Println(arg)4}5func getArgOrFail(index int) string {6 length := len(args)7 if length > index {8 }9 os.Exit(1)10}11func main() {12 arg := getArgOrFail(1)13 fmt.Println(arg)14}15func getArgOrFail(index int) string {16 length := len(args)17 if length > index {18 }19 os.Exit(1)20}21func main() {22 arg := getArgOrFail(1)23 fmt.Println(arg)24}25func getArgOrFail(index int) string {26 length := len(args)27 if length > index {28 }29 os.Exit(1)30}

Full Screen

Full Screen

getArgOrFail

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 arg := getArgOrFail(2)4 fmt.Println(arg)5}6import (7func main() {8 arg := getArgOrFail(2)9 fmt.Println(arg)10}11import (12func main() {13 arg := getArgOrFail(2)14 fmt.Println(arg)15}16import (17func main() {18 arg := getArgOrFail(2)19 fmt.Println(arg)20}21import (22func main() {23 arg := getArgOrFail(2)24 fmt.Println(arg)25}26import (27func main() {28 arg := getArgOrFail(2)29 fmt.Println(arg)30}31import (32func main() {33 arg := getArgOrFail(2)34 fmt.Println(arg)35}

Full Screen

Full Screen

getArgOrFail

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 arg := getArgOrFail()4 fmt.Println("Argument passed is: ", arg)5}6import (7func main() {8 arg := getArgOrFail()9 fmt.Println("Argument passed is: ", arg)10}11import (12func main() {13 arg := getArgOrFail()14 fmt.Println("Argument passed is: ", arg)15}16import (17func main() {18 arg := getArgOrFail()19 fmt.Println("Argument passed is: ", arg)20}21import (22func main() {23 arg := getArgOrFail()24 fmt.Println("Argument passed is: ", arg)25}26import (27func main() {28 arg := getArgOrFail()29 fmt.Println("Argument passed is: ", arg)30}31import (32func main() {33 arg := getArgOrFail()34 fmt.Println("Argument passed is: ", arg)35}36import (37func main() {38 arg := getArgOrFail()39 fmt.Println("Argument passed is: ",

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful