Example #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")
}
Example #2
0
func cmdDecrypt(c *cli.Context) {
	if len(c.Args()) < 1 {
		Usage(c, "decrypt")
		return
	}

	key := c.Args()[0]

	var data []byte
	var err error

	if len(c.Args()) == 1 {
		data, err = ioutil.ReadAll(os.Stdin)
	} else {
		data, err = ioutil.ReadFile(c.Args()[1])
	}

	if err != nil {
		panic(err)
	}

	cr, err := buildCrypt(c)

	if err != nil {
		panic(err)
	}

	dec, err := cr.Decrypt(key, data)

	if err != nil {
		panic(err)
	}

	fmt.Print(string(dec))
}
Example #3
0
func cmdEncrypt(c *cli.Context) {
	if len(c.Args()) < 1 {
		Usage(c, "encrypt")
		return
	}

	key := c.Args()[0]

	var env []byte
	var err error

	if len(c.Args()) == 1 {
		env, err = ioutil.ReadAll(os.Stdin)
	} else {
		env, err = ioutil.ReadFile(c.Args()[1])
	}

	if err != nil {
		panic(err)
	}

	cr, err := buildCrypt(c)

	if err != nil {
		panic(err)
	}

	data, err := cr.Encrypt(key, env)

	if err != nil {
		panic(err)
	}

	fmt.Print(string(data))
}
Example #4
0
func buildCrypt(c *cli.Context) (*crypt.Crypt, error) {
	if role := c.GlobalString("role"); role != "" {
		return crypt.NewIam(role)
	} else {
		region := c.GlobalString("region")
		access := c.GlobalString("access")
		secret := c.GlobalString("secret")

		return crypt.New(region, access, secret), nil
	}
}