Exemple #1
0
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
	var context *cli.Context

	a := cli.NewApp()
	a.Commands = []cli.Command{
		{
			Name: "foo",
			Action: func(c *cli.Context) {
				context = c
			},
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "lang",
					Value: "english",
					Usage: "language for the greeting",
				},
			},
			Before: func(_ *cli.Context) error { return nil },
		},
	}
	a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})

	expect(t, context.Args().Get(0), "abcd")
	expect(t, context.String("lang"), "spanish")
}
Exemple #2
0
func CLIopts(c *cli.Context, have map[string]Cred, need map[string]string) {
	for opt := range need {
		if c.IsSet(opt) {
			have[opt] = Cred{Value: c.String(opt), From: "command-line"}
			delete(need, opt)
		}
	}
}
Exemple #3
0
func ConfigFile(c *cli.Context, have map[string]Cred, need map[string]string) error {
	var profile string
	if c.IsSet("profile") {
		profile = c.String("profile")
	} else {
		sections, err := ProfileSections()
		if err != nil {
			return err
		}

		for _, section := range sections {
			if section.KeysHash()["enabled"] == "true" {
				profile = section.Name()
			}
		}
	}

	section, err := ProfileSection(profile)
	if err != nil {
		return err
	}

	if section == nil {
		return nil
	}

	for opt := range need {
		if val := section.Key(opt).String(); val != "" {
			have[opt] = Cred{Value: val, From: fmt.Sprintf("config file (profile: %s)", section.Name())}
			delete(need, opt)
		}
	}

	if profile != "" {
		section, err := ProfileSection("")
		if err != nil {
			return err
		}

		for opt := range need {
			if val := section.Key(opt).String(); val != "" {
				have[opt] = Cred{Value: val, From: fmt.Sprintf("config file (profile: default)")}
				delete(need, opt)
			}
		}
	}

	return nil
}