func main()

in cmd/config-downloader/downloader.go [102:235]


func main() {

	defer func() {
		if r := recover(); r != nil {
			if val, ok := r.(string); ok {
				fmt.Println(val)
			}
			fmt.Println(exitErrorMessage)
			os.Exit(1)
		}
	}()

	var region, mode, downloadLocation, outputDir, inputConfig, multiConfig string

	flag.StringVar(&mode, "mode", "ec2", "The mode value, i.e. ec2 or onPrem")
	flag.StringVar(&downloadLocation, "download-source", "",
		"Download source. Example: \"ssm:my-parameter-store-name\" for an EC2 SSM Parameter Store Name holding your CloudWatch Agent configuration.")
	flag.StringVar(&outputDir, "output-dir", "", "Path of output json config directory.")
	flag.StringVar(&inputConfig, "config", "", "Please provide the common-config file")
	flag.StringVar(&multiConfig, "multi-config", "default", "valid values: default, append, remove")
	flag.Parse()

	cc := commonconfig.New()
	if inputConfig != "" {
		f, err := os.Open(inputConfig)
		if err != nil {
			fmt.Printf("E! Failed to open Common Config: %v\n", err)
			panic(err)
		}

		if err := cc.Parse(f); err != nil {
			fmt.Printf("E! Failed to parse Common Config: %v\n", err)
			panic(err)
		}
	}
	util.SetProxyEnv(cc.ProxyMap())
	util.SetSSLEnv(cc.SSLMap())
	var errorMessage string
	if downloadLocation == "" || outputDir == "" {
		executable, e := os.Executable()
		if e == nil {
			errorMessage = fmt.Sprintf("usage: " + filepath.Base(executable) + " --output-dir <path> --download-source ssm:<parameter-store-name> ")
		} else {
			errorMessage = fmt.Sprintf("usage: --output-dir <path> --download-source ssm:<parameter-store-name> ")
		}
		panic(errorMessage)
	}

	mode = sdkutil.DetectAgentMode(mode)

	region = util.DetectRegion(mode, cc.CredentialsMap())

	if region == "" && downloadLocation != locationDefault {
		fmt.Println("Unable to determine aws-region.")
		if mode == config.ModeEC2 {
			errorMessage = fmt.Sprintf("Please check if you can access the metadata service. For exampe, on linux, run 'wget -q -O - http://169.254.169.254/latest/meta-data/instance-id && echo' ")
		} else {
			errorMessage = fmt.Sprintf("Please make sure the credentials and region set correctly on your hosts.\n" +
				"Refer to http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html")
		}
		panic(errorMessage)
	}

	// clean up output dir for tmp files before writing out new tmp file.
	// this step cannot be in translator because it is too late at that time.
	filepath.Walk(
		outputDir,
		func(path string, info os.FileInfo, err error) error {
			if err != nil {
				fmt.Printf("Cannot access %v: %v", path, err)
				return err
			}
			if info.IsDir() {
				if strings.EqualFold(path, outputDir) {
					return nil
				} else {
					fmt.Printf("Sub dir %v will be ignored.", path)
					return filepath.SkipDir
				}
			}
			if filepath.Ext(path) == context.TmpFileSuffix {
				return os.Remove(path)
			}
			return nil
		})

	locationArray := strings.SplitN(downloadLocation, locationSeparator, 2)
	if locationArray == nil || len(locationArray) < 2 && downloadLocation != locationDefault {
		panic(fmt.Sprintf("downloadLocation %s is malformated.\n", downloadLocation))
	}

	var config, outputFilePath string
	var err error
	switch locationArray[0] {
	case locationDefault:
		outputFilePath = locationDefault
		if multiConfig != "remove" {
			config, err = defaultJsonConfig(mode)
		}
	case locationSSM:
		outputFilePath = locationSSM + "_" + EscapeFilePath(locationArray[1])
		if multiConfig != "remove" {
			config, err = downloadFromSSM(region, locationArray[1], mode, cc.CredentialsMap())
		}
	case locationFile:
		outputFilePath = locationFile + "_" + EscapeFilePath(filepath.Base(locationArray[1]))
		if multiConfig != "remove" {
			config, err = readFromFile(locationArray[1])
		}
	default:
		panic(fmt.Sprintf("location type %s is not supported.", locationArray[0]))
	}

	if err != nil {
		panic(fmt.Sprintf("Fail to fetch/remove json config: %v\n", err))
	}

	if multiConfig != "remove" {
		outputFilePath = filepath.Join(outputDir, outputFilePath+context.TmpFileSuffix)
		err = ioutil.WriteFile(outputFilePath, []byte(config), 0644)
		if err != nil {
			panic(fmt.Sprintf("Failed to write the json file %v: %v\n", outputFilePath, err))
		} else {
			fmt.Printf("Successfully fetched the config and saved in %s\n", outputFilePath)
		}
	} else {
		outputFilePath = filepath.Join(outputDir, outputFilePath)
		if err := os.Remove(outputFilePath); err != nil {
			panic(fmt.Sprintf("Failed to remove the json file %v: %v", outputFilePath, err))
		} else {
			fmt.Printf("Successfully removed the config file %s\n", outputFilePath)
		}
	}
}