func main()

in tools/mig-scaler/main.go [61:154]


func main() {
	var err error
	log.SetFlags(0)

	cfg := new(Config)

	f := pflag.NewFlagSet("mig-scaler", pflag.ContinueOnError)

	// setup flags before reading the config files , otherwise the pflag package
	// will overwrite the config with the default values
	f.StringVar(&cfg.Workflow.Project, "workflow-project", "", "")
	f.StringVar(&cfg.Workflow.Region, "workflow-region", "", "")
	f.StringVar(&cfg.Workflow.Name, "workflow-name", "mig-scaler", "")
	f.Uint32Var(&cfg.Workflow.MaxSize, "workflow-max-size", 100, "")
	f.DurationVar(&cfg.Workflow.MaxDuration, "workflow-max-duration", 8*time.Hour, "")

	f.StringVar(&cfg.MIG.Project, "project", "", "")
	f.StringVar(&cfg.MIG.Region, "region", "", "")
	f.StringVar(&cfg.MIG.Zone, "zone", "", "")

	// default rate of 10 machines per minute
	// if the clients have 32 cores this will start 9,600 cores in 30 minutes.
	f.Uint32Var(&cfg.MIG.Increment, "increment", 10, "")
	f.DurationVar(&cfg.MIG.Wait, "wait", 1*time.Minute, "")
	f.DurationVar(&cfg.MIG.Duration, "duration", 0, "")

	f.StringVar(&cfg.Format, "format", "table", "")
	f.BoolVar(&cfg.Detailed, "detailed", false, "")
	f.BoolVar(&cfg.ActiveOnly, "active", false, "")

	f.BoolVarP(&cfg.Help, "help", "h", false, "")

	// read the config file before parsing the command line arguments so
	// that the command line arguments override any config values
	err = readDefaultConfig(cfg)
	if err != nil {
		log.Printf("error: could not read config: %s", err)
		os.Exit(2)
	}

	// override values from the config file with environment variables
	readEnv(cfg)

	// command line arguments overrides all other sources
	err = f.Parse(os.Args[1:])

	// grab the positional args before checking for an error so that they can
	// be used by ShowUsage
	cfg.Args = f.Args()
	if len(cfg.Args) >= 1 {
		cfg.Command = cfg.Args[0]
		cfg.Args = cfg.Args[1:]
	}
	if err != nil {
		log.Printf("error: %+v", err)
		showUsage(cfg)
		os.Exit(2)
	}

	if f.Lookup("region").Changed && !f.Lookup("zone").Changed {
		// if the region was set, and the zone was not set, clear the zone
		// so that setting --region=value on the command line overrides zone
		// from the environment or config
		cfg.MIG.Zone = ""
	}

	// Default the workflow location to the MIG location if the workflow was
	// not set. This allows running with just --project=... --region=... without
	// an ini file.
	defaultString(&cfg.Workflow.Project, cfg.MIG.Project)
	defaultString(&cfg.Workflow.Region, cfg.MIG.Region)

	// Likewise, default the job's max duration to the workflow's max duration
	// if the job's max duration was not set.
	defaultDuration(&cfg.MIG.Duration, cfg.Workflow.MaxDuration)

	// Truncate durations to the nearest second
	truncateDuration(&cfg.Workflow.MaxDuration)
	truncateDuration(&cfg.MIG.Wait)
	truncateDuration(&cfg.MIG.Duration)

	cmd, err := CreateCommand(cfg)
	if err != nil {
		printConfigError(err)
		showUsage(cfg)
		os.Exit(2)
	}

	ctx := context.Background()
	err = cmd.Execute(ctx)
	if err != nil {
		log.Fatalf("error: %+v", err)
	}
}