// ./jsonpath "$.store" a.json
// cat a.json | ./jsonpath "$.store"
763642
func main() {
	if len(os.Args) < 2 {
		// Only have the command name. Need to print out help text.
		log.Printf("Usage: %v '$['json']['path']['string']' a.json...\n", os.Args[0])
		os.Exit(1)
	}
	filter, err := jsonpath.Parse(os.Args[1])
	if err != nil {
		log.Printf("Usage: %v '$['json']['path']['string']' a.json...\n", os.Args[0])
		log.Printf("Got the following error Parsing %v : %v\n", os.Args[1], err)
		os.Exit(1)
	}
	if len(os.Args) == 2 {
		// use stdinput
		data, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			log.Printf("Usage: %v '$['json']['path']['string']' a.json...\n", os.Args[0])
			log.Printf("Got the following error Reading from STDIN  %v\n", err)
			os.Exit(1)
		}
		d, err := applyFilter(filter, data)
		if err != nil {
			log.Printf("Usage: %v '$['json']['path']['string']' a.json...\n", os.Args[0])
			log.Printf("Got the following error Applying filter  %v\n", err)
			os.Exit(1)
		}
		data, err = json.MarshalIndent(d, "", "   ")
		if err != nil {
			log.Printf("Usage: %v '$['json']['path']['string']' a.json...\n", os.Args[0])
			log.Printf("Got the following error Printing out json %v\n", err)
			os.Exit(1)
		}
		fmt.Println(string(data))
	}
	for _, filename := range os.Args[2:] {
		data, err := ioutil.ReadFile(filename)
		if err != nil {
			log.Printf("Got the following error Reading from %v  %v\n", filename, err)
			continue
		}
		d, err := applyFilter(filter, data)
		if err != nil {
			log.Printf("Got the following error Applying filter for %v  %v\n", filename, err)
			continue
		}
		data, err = json.MarshalIndent(d, "", "   ")
		if err != nil {
			log.Printf("Got the following error Trying to print result for file %v  %v\n", filename, err)
			continue
		}
		fmt.Println(string(data))
	}

}
422807
func Path(browser, libpath string, globalInstall bool) (string, error) {
	location, err := getLocation(browser, libpath, globalInstall)
	if err != nil {
		return "", err
	}

	expanded, err := homedir.Expand(location)
	if err != nil {
		return "", err
	}

	return filepath.Join(expanded, Name+".json"), nil
}
653134
func NewReferenceStore(jsonPath string) (Store, error) {
	abspath, err := filepath.Abs(jsonPath)
	if err != nil {
		return nil, err
	}

	store := &store{
		jsonPath:            abspath,
		Repositories:        make(map[string]repository),
		referencesByIDCache: make(map[digest.Digest]map[string]reference.Named),
	}
	// Load the json file if it exists, otherwise create it.
	if err := store.reload(); os.IsNotExist(err) {
		if err := store.save(); err != nil {
			return nil, err
		}
	} else if err != nil {
		return nil, err
	}
	return store, nil
}
945857
func GetJSONPathString(obj interface{}, path string) (string, error) {
	j := jsonpath.New("GetJSONPathString")
	// If the key is missing, return an empty string without errors
	j.AllowMissingKeys(true)
	err := j.Parse(path)
	if err != nil {
		return "", errors.Wrapf(err, "JsonPath parse %s error", path)
	}
	var buf bytes.Buffer
	err = j.Execute(&buf, obj)
	if err != nil {
		return "", errors.Wrap(err, "JsonPath execute error")
	}
	return buf.String(), nil
}
1036820
func (m *Meta) loadPluginPath() ([]string, error) {
	js, err := ioutil.ReadFile(filepath.Join(m.DataDir(), PluginPathFile))
	if os.IsNotExist(err) {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}

	var pluginPath []string
	if err := json.Unmarshal(js, &pluginPath); err != nil {
		return nil, err
	}

	return pluginPath, nil
}
692417
func msgUnPath(s string) string {
	ks := strings.Split(strings.Trim(s, etcdPathSeparator), etcdPathSeparator)
	for i, j := 0, len(ks)-1; i < j; i, j = i+1, j-1 {
		ks[i], ks[j] = ks[j], ks[i]
	}
	return strings.Join(ks, ".")
}
455747
func (l *fsLoader) JsonReference() (gojsonreference.JsonReference, error) { // nolint: golint
	return gojsonreference.NewJsonReference(l.JsonSource().(string))
}
806966
func (m *Manager) SnapshotPath() string {
	if m.DataDir == "" {
		return ""
	}

	return filepath.Join(m.DataDir, "snapshot.json")
}
578155
func JoinPath(parent string, children ...string) string {
	path := new(bytes.Buffer)

	if len(parent) > 0 {
		if !strings.HasPrefix(parent, PATH_SEPARATOR) {
			path.WriteString(PATH_SEPARATOR)
		}

		if strings.HasSuffix(parent, PATH_SEPARATOR) {
			path.WriteString(parent[:len(parent)-1])
		} else {
			path.WriteString(parent)
		}
	}

	for _, child := range children {
		if len(child) == 0 || child == PATH_SEPARATOR {
			if path.Len() == 0 {
				path.WriteString(PATH_SEPARATOR)
			}
		} else {
			path.WriteString(PATH_SEPARATOR)

			if strings.HasPrefix(child, PATH_SEPARATOR) {
				child = child[1:]
			}

			if strings.HasSuffix(child, PATH_SEPARATOR) {
				child = child[:len(child)-1]
			}

			path.WriteString(child)
		}
	}

	return path.String()
}
930039
func setPath(url *url.URL, resource string) {

	// ensure that path is "/" terminated before concatting resource
	if url.Path != "" && !strings.HasSuffix(url.Path, "/") {
		url.Path = url.Path + "/"
	}

	// don't pluralize resource automagically, JSON API spec is agnostic
	url.Path = fmt.Sprintf("%s%s", url.Path, resource)
}
625581
func New(name string) *JSONPath {
	return &JSONPath{
		name:       name,
		beginRange: 0,
		inRange:    0,
		endRange:   0,
	}
}
763368
func (j *JSONPath) Parse(text string) error {
	var err error
	j.parser, err = Parse(j.name, text)
	return err
}
863405
func (j *Json) GetPath(branch ...string) *Json {
	jin := j
	for _, p := range branch {
		jin = jin.Get(p)
	}
	return jin
}
263531
func AppPath(root string, appName types.ACName) string {
	return filepath.Join(AppsPath(root), appName.String())
}
568348
func (j *Json) SetPath(branch []string, val interface{}) {
	if len(branch) == 0 {
		j.data = val
		return
	}

	// in order to insert our branch, we need map[string]interface{}
	if _, ok := (j.data).(map[string]interface{}); !ok {
		// have to replace with something suitable
		j.data = make(map[string]interface{})
	}
	curr := j.data.(map[string]interface{})

	for i := 0; i < len(branch)-1; i++ {
		b := branch[i]
		// key exists?
		if _, ok := curr[b]; !ok {
			n := make(map[string]interface{})
			curr[b] = n
			curr = n
			continue
		}

		// make sure the value is the right sort of thing
		if _, ok := curr[b].(map[string]interface{}); !ok {
			// have to replace with something suitable
			n := make(map[string]interface{})
			curr[b] = n
		}

		curr = curr[b].(map[string]interface{})
	}

	// add remaining k/v
	curr[branch[len(branch)-1]] = val
}
1304051
func (ctx *Context) JSONError(v interface{}) error {
	ctx.Err500()
	return ctx.JSON(v)
}
219554
func (fs FS) Path(p ...string) string {
	return path.Join(append([]string{string(fs)}, p...)...)
}