func actionImport(s *Session, arg string) error { if arg == "" { return fmt.Errorf("arg required") } path := strings.Trim(arg, `"`) // check if the package specified by path is importable _, err := types.DefaultImport(s.Types.Packages, path) if err != nil { return err } astutil.AddImport(s.Fset, s.File, path) return nil }
// NewSession initiates a new REPL func NewSession() (*Session, error) { var err error s := &Session{ Fset: token.NewFileSet(), Types: &types.Config{ Packages: make(map[string]*types.Package), }, } s.FilePath, err = tempFile() if err != nil { return nil, err } var initialSource string for _, pp := range printerPkgs { _, err := types.DefaultImport(s.Types.Packages, pp.path) if err == nil { initialSource = fmt.Sprintf(initialSourceTemplate, pp.path, pp.code) break } debugf("could not import %q: %s", pp.path, err) } if initialSource == "" { return nil, fmt.Errorf(`Could not load pretty printing package (even "fmt"; something is wrong)`) } s.File, err = parser.ParseFile(s.Fset, "gore_session.go", initialSource, parser.Mode(0)) if err != nil { return nil, err } s.mainBody = s.mainFunc().Body return s, nil }