// Import used to import data into a exchange Resource
//     product.Import(csv.New("products.csv"), context)
337292
func (res *Resource) Import(container Container, context *qor.Context, callbacks ...func(Progress) error) error {
	rows, err := container.NewReader(res, context)
	if err == nil {
		var hasError bool
		var current uint
		var total = rows.Total()

		if db := context.GetDB(); db != nil && !res.Config.DisableTransaction {
			tx := db.Begin()
			context.SetDB(tx)
			defer func() {
				if hasError {
					tx.Rollback()
				} else {
					tx.Commit()
				}
			}()
		}

		for rows.Next() {
			current++
			progress := Progress{Total: total, Current: current}

			var metaValues *resource.MetaValues
			var handleError func(err error)

			if metaValues, err = rows.ReadRow(); err == nil {
				for _, metaValue := range metaValues.Values {
					progress.Cells = append(progress.Cells, Cell{
						Header: metaValue.Name,
						Value:  metaValue.Value,
					})
				}

				handleError = func(err error) {
					hasError = true
					progress.Errors.AddError(err)

					if errors, ok := err.(errorsInterface); ok {
						for _, err := range errors.GetErrors() {
							handleError(err)
						}
					} else if err, ok := err.(*validations.Error); ok {
						for idx, cell := range progress.Cells {
							if cell.Header == err.Column {
								cell.Error = err
								progress.Cells[idx] = cell
								break
							}
						}
					} else if len(progress.Cells) > 0 {
						var err error = err
						cell := progress.Cells[0]
						if cell.Error != nil {
							var errors qor.Errors
							errors.AddError(cell.Error)
							errors.AddError(err)
							err = errors
						}
						cell.Error = err
					}
				}

				result := res.NewStruct()
				progress.Value = result

				if err = res.FindOneHandler(result, metaValues, context); err == nil || err == gorm.ErrRecordNotFound {
					if err = resource.DecodeToResource(res, result, metaValues, context).Start(); err == nil {
						if err = res.CallSave(result, context); err != nil {
							handleError(err)
						}
					} else {
						handleError(err)
					}
				} else {
					handleError(err)
				}
			}

			for _, callback := range callbacks {
				if err := callback(progress); err != nil {
					return err
				}
			}
		}
	}
	return err
}
792017
func (cmd *ImportCommand) importValues(ctx context.Context, useColumnKeys bool, vals []pilosa.FieldValue) error {
	logger := log.New(cmd.Stderr, "", log.LstdFlags)

	// If keys are used, all values are sent to the primary translate store (i.e. coordinator).
	if useColumnKeys {
		logger.Printf("importing keyed values: n=%d", len(vals))
		if err := cmd.client.ImportValueK(ctx, cmd.Index, cmd.Field, vals); err != nil {
			return errors.Wrap(err, "importing keys")
		}
		return nil
	}

	// Group vals by shard.
	logger.Printf("grouping %d vals", len(vals))
	valsByShard := http.FieldValues(vals).GroupByShard()

	// Parse path into FieldValues.
	for shard, vals := range valsByShard {
		if cmd.Sort {
			sort.Sort(http.FieldValues(vals))
		}

		logger.Printf("importing shard: %d, n=%d", shard, len(vals))
		if err := cmd.client.ImportValue(ctx, cmd.Index, cmd.Field, shard, vals, pilosa.OptImportOptionsClear(cmd.Clear)); err != nil {
			return errors.Wrap(err, "importing values")
		}
	}

	return nil
}
452820
func (d *Diskv) Import(srcFilename, dstKey string, move bool) (err error) {
	if dstKey == "" {
		return errEmptyKey
	}

	if fi, err := os.Stat(srcFilename); err != nil {
		return err
	} else if fi.IsDir() {
		return errImportDirectory
	}

	dstPathKey := d.transform(dstKey)

	d.mu.Lock()
	defer d.mu.Unlock()

	if err := d.ensurePathWithLock(dstPathKey); err != nil {
		return fmt.Errorf("ensure path: %s", err)
	}

	if move {
		if err := syscall.Rename(srcFilename, d.completeFilename(dstPathKey)); err == nil {
			d.bustCacheWithLock(dstPathKey.originalKey)
			return nil
		} else if err != syscall.EXDEV {
			// If it failed due to being on a different device, fall back to copying
			return err
		}
	}

	f, err := os.Open(srcFilename)
	if err != nil {
		return err
	}
	defer f.Close()
	err = d.writeStreamWithLock(dstPathKey, f, false)
	if err == nil && move {
		err = os.Remove(srcFilename)
	}
	return err
}
1271734
func (s *NFSBackup) Import(lfile io.Reader) (err error) {
	lo.G.Debug("uploading file for backup")
	if err = s.RemoteOps.UploadFile(lfile); err == nil {
		lo.G.Debug("starting backup from %s", s.RemoteOps.Path())
		err = s.Caller.Execute(ioutil.Discard, s.getRestoreCommand())
	}
	if err == nil {
		lo.G.Debug("backup from %s completed", s.RemoteOps.Path())
	} else {
		lo.G.Debug("backup from %s completed with error %s", s.RemoteOps.Path(), err)
	}
	s.RemoteOps.RemoveRemoteFile()
	return
}
550963
func (g *Generator) Import(pkg interface{}) (path, selector string) {
	path, name := g.PackagePathAndName(pkg)

	imp, ok := g.imports[path]
	if !ok {
		imp = &importInfo{Name: name}

		if g.isSelectorTaken(path, name) {
			imp.Alias = g.makeAliasForName(name)
		}

		g.imports[path] = imp
	}

	return path, imp.String()
}
1035945
func ImportConfigs(c context.Context) (string, error) {
	cfg, rev, err := fetchConfigs(c)
	if err != nil {
		return "", errors.Annotate(err, "failed to fetch project configs").Err()
	}
	if err := importIdentities(c, cfg); err != nil {
		return "", errors.Annotate(err, "failed to import project configs").Err()
	}
	return rev, nil
}
1093311
func (t *UniversalTranslator) Import(format ImportExportFormat, dirnameOrFilename string) error {

	fi, err := os.Stat(dirnameOrFilename)
	if err != nil {
		return err
	}

	processFn := func(filename string) error {

		f, err := os.Open(filename)
		if err != nil {
			return err
		}
		defer f.Close()

		return t.ImportByReader(format, f)
	}

	if !fi.IsDir() {
		return processFn(dirnameOrFilename)
	}

	// recursively go through directory
	walker := func(path string, info os.FileInfo, err error) error {

		if info.IsDir() {
			return nil
		}

		switch format {
		case FormatJSON:
			// skip non JSON files
			if filepath.Ext(info.Name()) != ".json" {
				return nil
			}
		}

		return processFn(path)
	}

	return filepath.Walk(dirnameOrFilename, walker)
}
250700
func (csp *CSP) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
	// Validate arguments
	if raw == nil {
		return nil, errors.New("Invalid raw. It must not be nil.")
	}
	if opts == nil {
		return nil, errors.New("Invalid opts. It must not be nil.")
	}

	keyImporter, found := csp.KeyImporters[reflect.TypeOf(opts)]
	if !found {
		return nil, errors.Errorf("Unsupported 'KeyImportOpts' provided [%v]", opts)
	}

	k, err = keyImporter.KeyImport(raw, opts)
	if err != nil {
		return nil, errors.Wrapf(err, "Failed importing key with opts [%v]", opts)
	}

	// If the key is not Ephemeral, store it.
	if !opts.Ephemeral() {
		// Store the key
		err = csp.ks.StoreKey(k)
		if err != nil {
			return nil, errors.Wrapf(err, "Failed storing imported key with opts [%v]", opts)
		}
	}

	return
}
524787
func (si *shareImporter) importAll(ctx context.Context) error {
	src, shared, err := client.NewFromShareRoot(ctx, si.shareURL, client.OptionNoExternalConfig())
	if err != nil {
		return err
	}
	si.src = src
	si.br = shared
	si.workc = make(chan work, 2*numWorkers)
	defer close(si.workc)

	// fan out over a pool of numWorkers workers overall
	for i := 0; i < numWorkers; i++ {
		go func() {
			for wi := range si.workc {
				wi.errc <- si.imprt(ctx, wi.br)
			}
		}()
	}
	// work assignment is done asynchronously, so imprt returns before all the work is finished.
	err = si.imprt(ctx, shared)
	si.wg.Wait()
	if err == nil {
		si.mu.RLock()
		err = si.err
		si.mu.RUnlock()
	}
	log.Print("share importer: all workers done")
	if err != nil {
		return err
	}
	return nil
}
1036962
func (w *Wrapper) importDirsUsed() []string {
	used := []string{}
	for _, imp := range w.ImportDirs {
		for _, proto := range w.ProtoFiles {
			if strings.HasPrefix(proto, imp) {
				used = append(used, imp)
				break
			}
		}
	}
	return used
}
45609
func createImport(name, path string) *ast.ImportSpec {
	return &ast.ImportSpec{
		Name: &ast.Ident{Name: name},
		Path: &ast.BasicLit{Kind: 9, Value: path},
	}
}
1066735
func addImports(imports map[string]string, decl ast.Decl, srcDir string) {
	genDecl, ok := decl.(*ast.GenDecl)
	if !ok {
		return
	}

	if genDecl.Tok != token.IMPORT {
		return
	}

	for _, spec := range genDecl.Specs {
		importSpec := spec.(*ast.ImportSpec)
		var pkgAlias string
		if importSpec.Name != nil {
			pkgAlias = importSpec.Name.Name
			if pkgAlias == "_" {
				continue
			}
		}
		quotedPath := importSpec.Path.Value           // e.g. "\"sample/app/models\""
		fullPath := quotedPath[1 : len(quotedPath)-1] // Remove the quotes

		// If the package was not aliased (common case), we have to import it
		// to see what the package name is.
		// TODO: Can improve performance here a lot:
		// 1. Do not import everything over and over again.  Keep a cache.
		// 2. Exempt the standard library; their directories always match the package name.
		// 3. Can use build.FindOnly and then use parser.ParseDir with mode PackageClauseOnly
		if pkgAlias == "" {
			utils.Logger.Debug("Reading from build", "path", fullPath, "srcPath", srcDir, "gopath", build.Default.GOPATH)
			pkg, err := build.Import(fullPath, srcDir, 0)
			if err != nil {
				// We expect this to happen for apps using reverse routing (since we
				// have not yet generated the routes).  Don't log that.
				if !strings.HasSuffix(fullPath, "/app/routes") {
					utils.Logger.Error("Could not find import:", "path", fullPath, "srcPath", srcDir, "error", err)
				}
				continue
			} else {
				utils.Logger.Debug("Found package in dir", "dir", pkg.Dir, "name", pkg.ImportPath)
			}
			pkgAlias = pkg.Name
		}

		imports[pkgAlias] = fullPath
	}
}
1137878
func Import(name string, s Writer, reader io.Reader) error {
	tr := tar.NewReader(reader)
	tlsData := ContextTLSData{
		Endpoints: map[string]EndpointTLSData{},
	}
	for {
		hdr, err := tr.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		if hdr.Typeflag == tar.TypeDir {
			// skip this entry, only taking files into account
			continue
		}
		if hdr.Name == metaFile {
			data, err := ioutil.ReadAll(tr)
			if err != nil {
				return err
			}
			var meta Metadata
			if err := json.Unmarshal(data, &meta); err != nil {
				return err
			}
			meta.Name = name
			if err := s.CreateOrUpdate(meta); err != nil {
				return err
			}
		} else if strings.HasPrefix(hdr.Name, "tls/") {
			relative := strings.TrimPrefix(hdr.Name, "tls/")
			parts := strings.SplitN(relative, "/", 2)
			if len(parts) != 2 {
				return errors.New("archive format is invalid")
			}
			endpointName := parts[0]
			fileName := parts[1]
			data, err := ioutil.ReadAll(tr)
			if err != nil {
				return err
			}
			if _, ok := tlsData.Endpoints[endpointName]; !ok {
				tlsData.Endpoints[endpointName] = EndpointTLSData{
					Files: map[string][]byte{},
				}
			}
			tlsData.Endpoints[endpointName].Files[fileName] = data
		}
	}
	return s.ResetTLSMaterial(name, &tlsData)
}
1076070
func (a *StorageAPI) Import(args params.BulkImportStorageParams) (params.ImportStorageResults, error) {
	if err := a.checkCanWrite(); err != nil {
		return params.ImportStorageResults{}, errors.Trace(err)
	}

	blockChecker := common.NewBlockChecker(a.backend)
	if err := blockChecker.ChangeAllowed(); err != nil {
		return params.ImportStorageResults{}, errors.Trace(err)
	}

	results := make([]params.ImportStorageResult, len(args.Storage))
	for i, arg := range args.Storage {
		details, err := a.importStorage(arg)
		if err != nil {
			results[i].Error = common.ServerError(err)
			continue
		}
		results[i].Result = details
	}
	return params.ImportStorageResults{Results: results}, nil
}
691769
func (api *API) Import(serialized params.SerializedModel) error {
	controller := state.NewController(api.pool)
	_, st, err := migration.ImportModel(controller, api.getClaimer, serialized.Bytes)
	if err != nil {
		return err
	}
	defer st.Close()
	// TODO(mjs) - post import checks
	// NOTE(fwereade) - checks here would be sensible, but we will
	// also need to check after the binaries are imported too.
	return err
}
40181
func (imp *Import) Copy() *Import {
	cpy := *imp
	cpy.Path = imp.Path.Copy()
	return &cpy
}
811786
func Import(env *vm.Env) *vm.Env {
	env.Define("keys", func(v interface{}) []interface{} {
		rv := reflect.ValueOf(v)
		if rv.Kind() == reflect.Interface {
			rv = rv.Elem()
		}
		mapKeysValue := rv.MapKeys()
		mapKeys := make([]interface{}, len(mapKeysValue))
		for i := 0; i < len(mapKeysValue); i++ {
			mapKeys[i] = mapKeysValue[i].Interface()
		}
		return mapKeys
	})

	env.Define("range", func(args ...int64) []int64 {
		var start, stop int64
		var step int64 = 1

		switch len(args) {
		case 0:
			panic("range expected at least 1 argument, got 0")
		case 1:
			stop = args[0]
		case 2:
			start = args[0]
			stop = args[1]
		case 3:
			start = args[0]
			stop = args[1]
			step = args[2]
			if step == 0 {
				panic("range argument 3 must not be zero")
			}
		default:
			panic(fmt.Sprintf("range expected at most 3 arguments, got %d", len(args)))
		}

		arr := []int64{}
		for i := start; (step > 0 && i < stop) || (step < 0 && i > stop); i += step {
			arr = append(arr, i)
		}
		return arr
	})

	env.Define("typeOf", func(v interface{}) string {
		return reflect.TypeOf(v).String()
	})

	env.Define("kindOf", func(v interface{}) string {
		typeOf := reflect.TypeOf(v)
		if typeOf == nil {
			return "nil"
		}
		return typeOf.Kind().String()
	})

	env.Define("chanOf", func(t reflect.Type) reflect.Value {
		return reflect.MakeChan(t, 1)
	})

	env.Define("defined", func(s string) bool {
		_, err := env.Get(s)
		return err == nil
	})

	env.Define("load", func(s string) interface{} {
		body, err := ioutil.ReadFile(s)
		if err != nil {
			panic(err)
		}
		scanner := new(parser.Scanner)
		scanner.Init(string(body))
		stmts, err := parser.Parse(scanner)
		if err != nil {
			if pe, ok := err.(*parser.Error); ok {
				pe.Filename = s
				panic(pe)
			}
			panic(err)
		}
		rv, err := vm.Run(stmts, env)
		if err != nil {
			panic(err)
		}
		return rv
	})

	env.Define("panic", func(e interface{}) {
		os.Setenv("ANKO_DEBUG", "1")
		panic(e)
	})

	env.Define("print", fmt.Print)
	env.Define("println", fmt.Println)
	env.Define("printf", fmt.Printf)

	ImportToX(env)

	return env
}