示例#1
0
文件: watch.go 项目: thrawn01/args
// Simple example always updates the config when file changes are detected.
func simple(parser *args.ArgParser) args.WatchCancelFunc {
	// Get our current config
	appConf := parser.GetOpts()
	configFile := appConf.String("config-file")

	// Watch the file every time.Second and call func(err error){} when the file is modified
	cancelWatch, err := args.WatchFile(configFile, time.Second, func(err error) {
		if err != nil {
			fmt.Printf("Watch Error %s\n", err.Error())
			return
		}

		// You can safely ignore the returned Options{} object here.
		// the next call to GetOpts() from within the handler will
		// pick up the newly parsed config
		appConf, err = parser.FromINIFile(configFile)
		if err != nil {
			fmt.Printf("Failed to load updated config - %s\n", err.Error())
			return
		}
	})

	if err != nil {
		fmt.Printf("Unable to start watch '%s' -  %s", configFile, err.Error())
	}
	return cancelWatch
}
示例#2
0
func createChickens(subParser *args.ArgParser, data interface{}) int {
	subParser.AddArgument("name").Required().Help("The name of the chicken to create")
	opts, err := subParser.ParseArgs(nil)
	if err != nil {
		fmt.Println(err.Error())
		return 1
	}

	shared := data.(*SharedStruct)

	// Create the payload
	payload, err := json.Marshal(map[string]string{
		"name":     opts.String("name"),
		"metadata": shared.Metadata,
	})
	if err != nil {
		fmt.Fprintln(os.Stderr, "JSON Marshalling Error -", err)
		return 1
	}

	// Create the new Request
	req, err := http.NewRequest("POST", joinUrl(shared, "chickens"), bytes.NewBuffer(payload))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	resp, err := sendRequest(opts, req, &payload)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	fmt.Println(resp)
	return 0
}
示例#3
0
func listChickens(subParser *args.ArgParser, data interface{}) int {
	opts := subParser.GetOpts()

	shared := data.(*SharedStruct)
	req, err := http.NewRequest("GET", joinUrl(shared, "chickens"), nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	resp, err := sendRequest(opts, req, nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	fmt.Println(resp)
	return 0
}
示例#4
0
func deleteChickens(subParser *args.ArgParser, data interface{}) int {
	subParser.AddArgument("name").Required().Help("The name of the chicken to delete")
	opts, err := subParser.ParseArgs(nil)
	if err != nil {
		fmt.Println(err.Error())
		return 1
	}

	shared := data.(*SharedStruct)
	req, err := http.NewRequest("DELETE", joinUrl(shared, "chickens", opts.String("name")), nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	resp, err := sendRequest(opts, req, nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}
	fmt.Println(resp)
	return 0
}
示例#5
0
文件: watch.go 项目: thrawn01/args
// The complex example allows a user to write the config file multiple times, possibly applying edits incrementally.
// When the user is ready for the application to apply the config changes, modify the 'version' value and the
// new config is applied.
func complex(parser *args.ArgParser) args.WatchCancelFunc {
	// Get our current config
	appConf := parser.GetOpts()
	configFile := appConf.String("config-file")

	// Watch the file every time.Second and call func(err error){} when the file is modified
	cancelWatch, err := args.WatchFile(configFile, time.Second, func(err error) {
		if err != nil {
			fmt.Printf("Watch Error %s\n", err.Error())
			return
		}

		// load the file from disk
		content, err := args.LoadFile(configFile)
		if err != nil {
			fmt.Printf("Failed to load config - %s\n", err.Error())
		}

		// Parse the file contents
		newConfig, err := parser.ParseINI(content)
		if err != nil {
			fmt.Printf("Failed to update config - %s\n", err.Error())
			return
		}

		// Only "Apply" the newConfig when the version changes
		if appConf.Int("version") != newConfig.Int("version") {
			// Apply the newConfig values to the parser rules
			appConf, err = parser.Apply(newConfig)
			if err != nil {
				fmt.Printf("Probably a type cast error - %s\n", err.Error())
				return
			}
		}
	})
	if err != nil {
		log.Fatal(fmt.Sprintf("Failed to watch '%s' -  %s", configFile, err.Error()))
	}
	return cancelWatch
}