Beispiel #1
0
// WarnWordSepNormalizeFunc changes and warns for flags that contain "_" separators
func WarnWordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
	if strings.Contains(name, "_") {
		nname := strings.Replace(name, "_", "-", -1)
		glog.Warningf("%s is DEPRECATED and will be removed in a future version. Use %s instead.", name, nname)

		return pflag.NormalizedName(nname)
	}
	return pflag.NormalizedName(name)
}
Beispiel #2
0
func main() {
	// configuration
	cfg := pkg.NewConfig()

	// flags
	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	fs.StringVar(&cfg.IngressesData, "ingresses-data", cfg.IngressesData, "Ingresses data.")
	fs.StringVar(&cfg.KubeMasterURL, "kube-master-url", cfg.KubeMasterURL, "URL to reach kubernetes master.")
	fs.StringVar(&cfg.Namespace, "namespace", cfg.Namespace, "If present, the namespace scope.")
	fs.StringVar(&cfg.Selector, "selector", cfg.Selector, "Filter resources by a user-provided selector.")
	fs.DurationVar(&cfg.ResyncInterval, "resync-interval", cfg.ResyncInterval, "Resync with kubernetes master every user-defined interval.")
	fs.StringVar(&cfg.NginxSrc, "nginx-src", cfg.NginxSrc, "nginx.conf template file path.")
	fs.StringVar(&cfg.NginxDest, "nginx-dst", cfg.NginxDest, "nginx.conf destination file path.")
	fs.IntVar(&cfg.NginxDestUid, "nginx-dst-uid", cfg.NginxDestUid, "nginx.conf destination file uid.")
	fs.IntVar(&cfg.NginxDestGid, "nginx-dst-gid", cfg.NginxDestGid, "nginx.conf destination file gid.")
	fs.StringVar(&cfg.NginxDestMode, "nginx-dst-mode", cfg.NginxDestMode, "nginx.conf destination file mode.")
	fs.StringVar(&cfg.NginxCheckCmd, "nginx-check-cmd", cfg.NginxCheckCmd, "nginx check command.")
	fs.StringVar(&cfg.NginxReloadCmd, "nginx-reload-cmd", cfg.NginxReloadCmd, "nginx reload command.")
	fs.SetNormalizeFunc(
		func(f *flag.FlagSet, name string) flag.NormalizedName {
			if strings.Contains(name, "_") {
				return flag.NormalizedName(strings.Replace(name, "_", "-", -1))
			}
			return flag.NormalizedName(name)
		},
	)

	// parse
	fs.Parse(os.Args[1:])

	// set from env (if present)
	fs.VisitAll(func(f *flag.Flag) {
		if !f.Changed {
			key := strings.ToUpper(strings.Join(
				[]string{
					cliName,
					strings.Replace(f.Name, "-", "_", -1),
				},
				"_",
			))
			val := os.Getenv(key)
			if val != "" {
				fs.Set(f.Name, val)
			}
		}
	})

	// and then, run!
	k2n := pkg.NewKubeToNginx(cfg)
	k2n.Run()
}
Beispiel #3
0
func main() {
	// configuration
	cfg := pkg.NewConfig()

	// flags
	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	fs.StringVar(&cfg.Me, "me", cfg.Me, "me.")
	fs.StringVar(&cfg.Members, "members", cfg.Members, "members.")
	fs.StringVar(&cfg.TmplListenUrl, "tmpl-listen-url", cfg.TmplListenUrl, "tmpl-listen-url.")
	fs.StringVar(&cfg.TmplPeerUrl, "tmpl-peer-url", cfg.TmplPeerUrl, "tmpl-peer-url.")
	fs.StringVar(&cfg.CertFile, "cert-file", cfg.CertFile, "Identify HTTPS client using this SSL certificate file.")
	fs.StringVar(&cfg.KeyFile, "key-file", cfg.KeyFile, "Identify HTTPS client using this SSL key file.")
	fs.StringVar(&cfg.CAFile, "ca-file", cfg.CAFile, "Verify certificates of HTTPS-enabled servers using this CA bundle.")
	fs.BoolVar(&cfg.Force, "force", cfg.Force, "force.")
	fs.StringVar(&cfg.Out, "out", cfg.Out, "etcd2 peers environment file destination")
	fs.SetNormalizeFunc(
		func(f *flag.FlagSet, name string) flag.NormalizedName {
			if strings.Contains(name, "_") {
				return flag.NormalizedName(strings.Replace(name, "_", "-", -1))
			}
			return flag.NormalizedName(name)
		},
	)

	// parse
	fs.Parse(os.Args[1:])

	// set from env (if present)
	fs.VisitAll(func(f *flag.Flag) {
		if !f.Changed {
			key := strings.ToUpper(strings.Join(
				[]string{
					cliName,
					strings.Replace(f.Name, "-", "_", -1),
				},
				"_",
			))
			val := os.Getenv(key)
			if val != "" {
				fs.Set(f.Name, val)
			}
		}
	})

	// and then, run!
	eb := pkg.NewEtcd2Bootstrapper(cfg)
	eb.Run()
}
Beispiel #4
0
func main() {
	// configuration
	cfg := pkg.NewConfig()

	// flags
	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	fs.StringVar(&cfg.KubeMasterURL, "kube-master-url", cfg.KubeMasterURL, "URL to reach kubernetes master.")
	fs.StringVar(&cfg.Namespace, "namespace", cfg.Namespace, "If present, the namespace scope.")
	fs.StringVar(&cfg.Resource, "resource", cfg.Resource, "Which resource to watch.")
	fs.StringVar(&cfg.Selector, "selector", cfg.Selector, "Filter resources by a user-provided selector.")
	fs.DurationVar(&cfg.ResyncInterval, "resync-interval", cfg.ResyncInterval, "Resync with kubernetes master every user-defined interval.")
	fs.StringVar(&cfg.AddEventsFile, "add-events-file", cfg.AddEventsFile, "File in which the events of type 'add' are printed.")
	fs.StringVar(&cfg.UpdateEventsFile, "update-events-file", cfg.UpdateEventsFile, "File in which the events of type 'update' are printed.")
	fs.StringVar(&cfg.DeleteEventsFile, "delete-events-file", cfg.DeleteEventsFile, "File in which the events of type 'delete' are printed.")
	fs.SetNormalizeFunc(
		func(f *flag.FlagSet, name string) flag.NormalizedName {
			if strings.Contains(name, "_") {
				return flag.NormalizedName(strings.Replace(name, "_", "-", -1))
			}
			return flag.NormalizedName(name)
		},
	)

	// parse
	fs.Parse(os.Args[1:])

	// set from env (if present)
	fs.VisitAll(func(f *flag.Flag) {
		if !f.Changed {
			key := strings.ToUpper(strings.Join(
				[]string{
					cliName,
					strings.Replace(f.Name, "-", "_", -1),
				},
				"_",
			))
			val := os.Getenv(key)
			if val != "" {
				fs.Set(f.Name, val)
			}
		}
	})

	// and then, run!
	kl := pkg.NewKubeListener(cfg)
	kl.Run()
}
Beispiel #5
0
func WordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
	from := []string{"-", "_"}
	to := "."
	for _, sep := range from {
		name = strings.Replace(name, sep, to, -1)
	}
	// Type convert to indicate normalization has been done.
	return pflag.NormalizedName(name)
}
Beispiel #6
0
// NormalizeHugoFlags facilitates transitions of Hugo command-line flags,
// e.g. --baseUrl to --baseURL, --uglyUrls to --uglyURLs
func NormalizeHugoFlags(f *pflag.FlagSet, name string) pflag.NormalizedName {
	switch name {
	case "baseUrl":
		name = "baseURL"
		break
	case "uglyUrls":
		name = "uglyURLs"
		break
	}
	return pflag.NormalizedName(name)
}
Beispiel #7
0
func TestGlobalNormFuncPropagation(t *testing.T) {
	normFunc := func(f *pflag.FlagSet, name string) pflag.NormalizedName {
		return pflag.NormalizedName(name)
	}

	rootCmd := initialize()
	rootCmd.SetGlobalNormalizationFunc(normFunc)
	if reflect.ValueOf(normFunc) != reflect.ValueOf(rootCmd.GlobalNormalizationFunc()) {
		t.Error("rootCmd seems to have a wrong normalization function")
	}

	// First add the cmdEchoSub to cmdPrint
	cmdPrint.AddCommand(cmdEchoSub)
	if cmdPrint.GlobalNormalizationFunc() != nil && cmdEchoSub.GlobalNormalizationFunc() != nil {
		t.Error("cmdPrint and cmdEchoSub should had no normalization functions")
	}

	// Now add cmdPrint to rootCmd
	rootCmd.AddCommand(cmdPrint)
	if reflect.ValueOf(cmdPrint.GlobalNormalizationFunc()).Pointer() != reflect.ValueOf(rootCmd.GlobalNormalizationFunc()).Pointer() ||
		reflect.ValueOf(cmdEchoSub.GlobalNormalizationFunc()).Pointer() != reflect.ValueOf(rootCmd.GlobalNormalizationFunc()).Pointer() {
		t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd")
	}
}
Beispiel #8
0
// WordSepNormalizeFunc changes all flags that contain "_" separators
func WordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
	if strings.Contains(name, "_") {
		return pflag.NormalizedName(strings.Replace(name, "_", "-", -1))
	}
	return pflag.NormalizedName(name)
}
Beispiel #9
0
func main() {
	trc := newTmplRendererConfig()

	// flags
	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	fs.StringVar(&trc.outputPath, "out", trc.outputPath, "")

	fs.SetNormalizeFunc(
		func(f *flag.FlagSet, name string) flag.NormalizedName {
			if strings.Contains(name, "_") {
				return flag.NormalizedName(strings.Replace(name, "_", "-", -1))
			}
			return flag.NormalizedName(name)
		},
	)

	// define usage
	fs.Usage = func() {
		goVersion := strings.TrimPrefix(runtime.Version(), "go")
		fmt.Fprintf(os.Stderr, "%s (version=%s, gitrev=%s, go=%s)\n", cliName, Version, GitRev, goVersion)
		/*
			fmt.Fprintf(os.Stderr, "Usage:\n")
			fs.PrintDefaults()
		*/
	}

	// parse
	fs.Parse(os.Args[1:])

	// set from env (if present)
	fs.VisitAll(func(f *flag.Flag) {
		if !f.Changed {
			key := strings.ToUpper(strings.Join(
				[]string{
					cliName,
					strings.Replace(f.Name, "-", "_", -1),
				},
				"_",
			))
			val := os.Getenv(key)
			if val != "" {
				fs.Set(f.Name, val)
			}
		}
	})

	// check if template was provided
	args := fs.Args()
	if len(args) < 1 {
		log.Fatal("Please provide a template")
	}

	// template file
	tmplFile := args[0]

	// check if file exists
	if _, err := os.Stat(tmplFile); os.IsNotExist(err) {
		log.Fatalf("%s", err)
	}

	// template funcs
	funcMap := make(map[string]interface{})
	funcMap["getenv"] = os.Getenv
	funcMap["getEnv"] = os.Getenv
	funcMap["encodeJSON"] = func(i interface{}) string {
		d, err := json.Marshal(i)
		if err != nil {
			log.Fatalf("%v", err)
		}
		return string(d)
	}
	funcMap["getFileContent"] = func(i string) string {
		d, err := ioutil.ReadFile(i)
		if err != nil {
			log.Fatalf("%v", err)
		}
		return string(d)
	}
	funcMap["getFileContentBytes"] = func(i string) []byte {
		d, err := ioutil.ReadFile(i)
		if err != nil {
			log.Fatalf("%v", err)
		}
		return d
	}
	funcMap["gzip"] = func(i []byte) []byte {
		var b bytes.Buffer
		w := gzip.NewWriter(&b)
		if _, err := w.Write(i); err != nil {
			log.Fatalf("%v", err)
		}

		if err := w.Close(); err != nil {
			log.Fatalf("%v", err)
		}

		return b.Bytes()
	}
	funcMap["encodeBase64"] = func(i []byte) string {
		return base64.StdEncoding.EncodeToString(i)
	}
	funcMap["toInt"] = func(s string) int {
		i, err := strconv.Atoi(s)
		if err != nil {
			log.Fatalf("%v", err)
		}

		return i
	}
	funcMap["add"] = func(a, b int) int {
		return a + b
	}
	funcMap["sub"] = func(a, b int) int {
		return a - b
	}

	// create template
	tmpl, err := template.New(path.Base(tmplFile)).Funcs(funcMap).ParseFiles(tmplFile)
	if err != nil {
		log.Fatalf("Unable to process template %s, %s", tmplFile, err)
	}

	// choose writer between file and stdout
	var writer io.Writer
	if trc.outputPath != "" {
		f, err := os.Create(trc.outputPath)
		if err != nil {
			log.Fatalf("Unable to create %s: %v", trc.outputPath, err)
		}
		defer f.Close()

		writer = f
	} else {
		writer = os.Stdout
	}

	// and then execute ;)
	if err = tmpl.Execute(writer, nil); err != nil {
		log.Fatalf("%s", err)
	}
}