Exemplo n.º 1
0
Arquivo: main.go Projeto: ches/ironcli
func main() {
	if runtime.GOOS == "windows" {
		red = fmt.Sprint
		yellow = fmt.Sprint
		green = fmt.Sprint
	} else {
		red = color.New(color.FgRed).SprintFunc()
		yellow = color.New(color.FgYellow).SprintFunc()
		green = color.New(color.FgGreen).SprintFunc()
	}

	flag.Parse()

	if *helpFlag || *hFlag {
		usage()
	} else if *versionFlag {
		fmt.Println(Version)
		os.Exit(0)
	}

	if flag.NArg() < 1 {
		usage()
	}

	product := flag.Arg(0)
	cmds, ok := commands[product]
	if !ok || flag.NArg() < 2 {
		pusage(product)
	}

	cmdName := flag.Arg(1)
	cmd, err := cmds.Command(flag.Args()[1:]...)

	if err != nil {
		if err == flag.ErrHelp && cmd != nil {
			cmd.Usage()
		}
		switch strings.TrimSpace(cmdName) {
		case "-h", "help", "--help", "-help":
			pusage(product)
		default:
			fmt.Fprintln(os.Stderr, red(err))
		}
		os.Exit(1)
	}

	err = cmd.Config()
	if err != nil {
		fmt.Fprintln(os.Stderr, red(err))
		os.Exit(2)
	}

	cmd.Run()
}
Exemplo n.º 2
0
func main() {
	if runtime.GOOS == "windows" {
		red = fmt.Sprint
		yellow = fmt.Sprint
		green = fmt.Sprint
	} else {
		red = color.New(color.FgRed).SprintFunc()
		yellow = color.New(color.FgYellow).SprintFunc()
		green = color.New(color.FgGreen).SprintFunc()
	}

	flag.Parse()

	if *helpFlag || *hFlag {
		usage()
	} else if *versionFlag {
		fmt.Fprintln(os.Stderr, Version)
		os.Exit(0)
	}

	if flag.NArg() < 1 {
		usage()
	}

	product := flag.Arg(0)
	cmds, ok := commands[product]
	if !ok {
		pusage(product)
	}

	if flag.NArg() < 2 {
		pusage(product)
	}

	cmdName := flag.Arg(1)
	cmd, ok := cmds[cmdName]

	if !ok {
		switch strings.TrimSpace(cmdName) {
		case "-h", "help", "--help", "-help":
			pusage(product)
		default:
			fmt.Fprintln(os.Stderr, red(cmdName, " not a command, see -h"))
		}
		os.Exit(1)
	}

	// each command defines its flags, err is either ErrHelp or bad flag value
	if err := cmd.Flags(flag.Args()[2:]...); err != nil {
		if err != flag.ErrHelp {
			fmt.Println(red(err))
		}
		os.Exit(2)
	}

	if err := cmd.Args(); err != nil {
		fmt.Fprintln(os.Stderr, red(err))
		os.Exit(2)
	}

	err := cmd.Config()
	if err != nil {
		fmt.Println(red(err))
		os.Exit(2)
	}

	cmd.Run()
}