cmd/root.go
func JsonnetVM(cmd *cobra.Command) (*jsonnet.VM, error) {
	vm := jsonnet.MakeVM()
	flags := cmd.Flags()

	var searchUrls []*url.URL

	jpath := filepath.SplitList(os.Getenv("KUBECFG_JPATH"))

	jpathArgs, err := flags.GetStringArray(flagJpath)
	if err != nil {
		return nil, err
	}
	jpath = append(jpath, jpathArgs...)

	for _, p := range jpath {
		p, err := filepath.Abs(p)
		if err != nil {
			return nil, err
		}
		searchUrls = append(searchUrls, dirURL(p))
	}

	sURLs, err := flags.GetStringArray(flagJUrl)
	if err != nil {
		return nil, err
	}

	// Special URL scheme used to find embedded content
	sURLs = append(sURLs, "internal:///")

	for _, ustr := range sURLs {
		u, err := url.Parse(ustr)
		if err != nil {
			return nil, err
		}
		if u.Path[len(u.Path)-1] != '/' {
			u.Path = u.Path + "/"
		}
		searchUrls = append(searchUrls, u)
	}

	for _, u := range searchUrls {
		log.Debugln("Jsonnet search path:", u)
	}

	vm.Importer(utils.MakeUniversalImporter(searchUrls))

	extvars, err := flags.GetStringSlice(flagExtVar)
	if err != nil {
		return nil, err
	}
	for _, extvar := range extvars {
		kv := strings.SplitN(extvar, "=", 2)
		switch len(kv) {
		case 1:
			v, present := os.LookupEnv(kv[0])
			if present {
				vm.ExtVar(kv[0], v)
			} else {
				return nil, fmt.Errorf("Missing environment variable: %s", kv[0])
			}
		case 2:
			vm.ExtVar(kv[0], kv[1])
		}
	}

	extvarfiles, err := flags.GetStringSlice(flagExtVarFile)
	if err != nil {
		return nil, err
	}
	for _, extvar := range extvarfiles {
		kv := strings.SplitN(extvar, "=", 2)
		if len(kv) != 2 {
			return nil, fmt.Errorf("Failed to parse %s: missing '=' in %s", flagExtVarFile, extvar)
		}
		v, err := ioutil.ReadFile(kv[1])
		if err != nil {
			return nil, err
		}
		vm.ExtVar(kv[0], string(v))
	}

	tlavars, err := flags.GetStringSlice(flagTlaVar)
	if err != nil {
		return nil, err
	}
	for _, tlavar := range tlavars {
		kv := strings.SplitN(tlavar, "=", 2)
		switch len(kv) {
		case 1:
			v, present := os.LookupEnv(kv[0])
			if present {
				vm.TLAVar(kv[0], v)
			} else {
				return nil, fmt.Errorf("Missing environment variable: %s", kv[0])
			}
		case 2:
			vm.TLAVar(kv[0], kv[1])
		}
	}

	tlavarfiles, err := flags.GetStringSlice(flagTlaVarFile)
	if err != nil {
		return nil, err
	}
	for _, tlavar := range tlavarfiles {
		kv := strings.SplitN(tlavar, "=", 2)
		if len(kv) != 2 {
			return nil, fmt.Errorf("Failed to parse %s: missing '=' in %s", flagTlaVarFile, tlavar)
		}
		v, err := ioutil.ReadFile(kv[1])
		if err != nil {
			return nil, err
		}
		vm.TLAVar(kv[0], string(v))
	}

	resolver, err := buildResolver(cmd)
	if err != nil {
		return nil, err
	}
	utils.RegisterNativeFuncs(vm, resolver)

	return vm, nil
}
func JsonnetVM(cmd *cobra.Command) (*jsonnet.VM, error) {
	vm := jsonnet.MakeVM()
	flags := cmd.Flags()

	var searchUrls []*url.URL

	jpath := filepath.SplitList(os.Getenv("KUBECFG_JPATH"))

	jpathArgs, err := flags.GetStringArray(flagJpath)
	if err != nil {
		return nil, err
	}
	jpath = append(jpath, jpathArgs...)

	for _, p := range jpath {
		p, err := filepath.Abs(p)
		if err != nil {
			return nil, err
		}
		searchUrls = append(searchUrls, dirURL(p))
	}

	sURLs, err := flags.GetStringArray(flagJUrl)
	if err != nil {
		return nil, err
	}

	// Special URL scheme used to find embedded content
	sURLs = append(sURLs, "internal:///")

	for _, ustr := range sURLs {
		u, err := url.Parse(ustr)
		if err != nil {
			return nil, err
		}
		if u.Path[len(u.Path)-1] != '/' {
			u.Path = u.Path + "/"
		}
		searchUrls = append(searchUrls, u)
	}

	for _, u := range searchUrls {
		log.Debugln("Jsonnet search path:", u)
	}

	vm.Importer(utils.MakeUniversalImporter(searchUrls))

	extvars, err := flags.GetStringSlice(flagExtVar)
	if err != nil {
		return nil, err
	}
	for _, extvar := range extvars {
		kv := strings.SplitN(extvar, "=", 2)
		switch len(kv) {
		case 1:
			v, present := os.LookupEnv(kv[0])
			if present {
				vm.ExtVar(kv[0], v)
			} else {
				return nil, fmt.Errorf("Missing environment variable: %s", kv[0])
			}
		case 2:
			vm.ExtVar(kv[0], kv[1])
		}
	}

	extvarfiles, err := flags.GetStringSlice(flagExtVarFile)
	if err != nil {
		return nil, err
	}
	for _, extvar := range extvarfiles {
		kv := strings.SplitN(extvar, "=", 2)
		if len(kv) != 2 {
			return nil, fmt.Errorf("Failed to parse %s: missing '=' in %s", flagExtVarFile, extvar)
		}
		v, err := ioutil.ReadFile(kv[1])
		if err != nil {
			return nil, err
		}
		vm.ExtVar(kv[0], string(v))
	}

	tlavars, err := flags.GetStringSlice(flagTlaVar)
	if err != nil {
		return nil, err
	}
	for _, tlavar := range tlavars {
		kv := strings.SplitN(tlavar, "=", 2)
		switch len(kv) {
		case 1:
			v, present := os.LookupEnv(kv[0])
			if present {
				vm.TLAVar(kv[0], v)
			} else {
				return nil, fmt.Errorf("Missing environment variable: %s", kv[0])
			}
		case 2:
			vm.TLAVar(kv[0], kv[1])
		}
	}

	tlavarfiles, err := flags.GetStringSlice(flagTlaVarFile)
	if err != nil {
		return nil, err
	}
	for _, tlavar := range tlavarfiles {
		kv := strings.SplitN(tlavar, "=", 2)
		if len(kv) != 2 {
			return nil, fmt.Errorf("Failed to parse %s: missing '=' in %s", flagTlaVarFile, tlavar)
		}
		v, err := ioutil.ReadFile(kv[1])
		if err != nil {
			return nil, err
		}
		vm.TLAVar(kv[0], string(v))
	}

	resolver, err := buildResolver(cmd)
	if err != nil {
		return nil, err
	}
	utils.RegisterNativeFuncs(vm, resolver)

	return vm, nil
}
func JsonnetVM ( cmd * cobra . Command ) ( * jsonnet . VM , error ) { vm := jsonnet . MakeVM ( ) flags := cmd . Flags ( ) var searchUrls [ ] * url . URL jpath := filepath . SplitList ( os . Getenv ( " " ) ) jpathArgs , err := flags . GetStringArray ( flagJpath ) if err != nil { return nil , err } jpath = append ( jpath , jpathArgs ... ) for _ , p := range jpath { p , err := filepath . Abs ( p ) if err != nil { return nil , err } searchUrls = append ( searchUrls , dirURL ( p ) ) } sURLs , err := flags . GetStringArray ( flagJUrl ) if err != nil { return nil , err } // Special URL scheme used to find embedded content sURLs = append ( sURLs , " " ) for _ , ustr := range sURLs { u , err := url . Parse ( ustr ) if err != nil { return nil , err } if u . Path [ len ( u . Path ) - 1 ] != '/' { u . Path = u . Path + " " } searchUrls = append ( searchUrls , u ) } for _ , u := range searchUrls { log . Debugln ( " " , u ) } vm . Importer ( utils . MakeUniversalImporter ( searchUrls ) ) extvars , err := flags . GetStringSlice ( flagExtVar ) if err != nil { return nil , err } for _ , extvar := range extvars { kv := strings . SplitN ( extvar , " " , 2 ) switch len ( kv ) { case 1 : v , present := os . LookupEnv ( kv [ 0 ] ) if present { vm . ExtVar ( kv [ 0 ] , v ) } else { return nil , fmt . Errorf ( " " , kv [ 0 ] ) } case 2 : vm . ExtVar ( kv [ 0 ] , kv [ 1 ] ) } } extvarfiles , err := flags . GetStringSlice ( flagExtVarFile ) if err != nil { return nil , err } for _ , extvar := range extvarfiles { kv := strings . SplitN ( extvar , " " , 2 ) if len ( kv ) != 2 { return nil , fmt . Errorf ( " " , flagExtVarFile , extvar ) } v , err := ioutil . ReadFile ( kv [ 1 ] ) if err != nil { return nil , err } vm . ExtVar ( kv [ 0 ] , string ( v ) ) } tlavars , err := flags . GetStringSlice ( flagTlaVar ) if err != nil { return nil , err } for _ , tlavar := range tlavars { kv := strings . SplitN ( tlavar , " " , 2 ) switch len ( kv ) { case 1 : v , present := os . LookupEnv ( kv [ 0 ] ) if present { vm . TLAVar ( kv [ 0 ] , v ) } else { return nil , fmt . Errorf ( " " , kv [ 0 ] ) } case 2 : vm . TLAVar ( kv [ 0 ] , kv [ 1 ] ) } } tlavarfiles , err := flags . GetStringSlice ( flagTlaVarFile ) if err != nil { return nil , err } for _ , tlavar := range tlavarfiles { kv := strings . SplitN ( tlavar , " " , 2 ) if len ( kv ) != 2 { return nil , fmt . Errorf ( " " , flagTlaVarFile , tlavar ) } v , err := ioutil . ReadFile ( kv [ 1 ] ) if err != nil { return nil , err } vm . TLAVar ( kv [ 0 ] , string ( v ) ) } resolver , err := buildResolver ( cmd ) if err != nil { return nil , err } utils . RegisterNativeFuncs ( vm , resolver ) return vm , nil }
// JsonnetVM constructs a new jsonnet.VM, according to command line
// flags
JsonnetVM constructs a new jsonnet . VM according to command line flags