Ejemplo n.º 1
0
func (yat *YandexTranslator) GetCli() *cli.Command {
	c := cli.Command{
		Name:    "yandex",
		Aliases: []string{"ya"},
		Usage:   "Yandex translator",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:   "api-key-file, f",
				Value:  ".yandex_api_key",
				Usage:  "yandex api key file",
				EnvVar: "YANDEX_API_KEY_FILE",
			},
		},
		Subcommands: []cli.Command{
			{
				Name:    "translate",
				Aliases: []string{"tr"},
				Usage:   "Translate with yandex translator",
				Action: func(c *cli.Context) {
					key, err := utilits.ReadFile(c.Parent().String("api-key-file"))
					if err != nil {
						fmt.Println(err)
						return
					}
					yat.Key = key

					langs := utilits.ParseLang(c.Args()[0])
					tr, err := yat.Translate(c.Args()[1], langs[0], langs[1])
					if err != nil {
						fmt.Println(err)
						return
					}
					fmt.Println(tr)
				},
			},
			{
				Name:    "langs",
				Usage:   "Get supported languages",
				Aliases: []string{"l"},
				Action: func(c *cli.Context) {
					key, err := utilits.ReadFile(c.Parent().String("api-key-file"))
					if err != nil {
						fmt.Println(err)
					}
					yat.Key = key
					langs, err := yat.GetLangs()
					if err != nil {
						fmt.Println(err)
					}
					fmt.Println(langs)
				},
			},
		},
	}

	return &c
}