Example #1
0
func main() {
	xsrc := flag.String("src", "./content", "Source directory to read markdown from")
	xdst := flag.String("dst", "", "Destination to write translated content")
	flag.Parse()

	srcD, srcE := os.Stat(*xsrc)
	if os.IsNotExist(srcE) || !srcD.IsDir() {
		clog.Fatalf("Source directory does not exist: %s", *xsrc)
	}

	if len(*xdst) > 0 {
		dstD, dstE := os.Stat(*xdst)
		if os.IsNotExist(dstE) || !dstD.IsDir() {
			clog.Fatalf("Target directory does not exist: %s", *xdst)
		}
	}

	tld := strings.Trim(*xsrc, "/")
	dirpos := strings.LastIndex(tld, "/")
	if dirpos < 0 {
		tld = tld[dirpos+1:]
	}
	if len(tld) < 1 {
		clog.Fatalf("Source directory path must be at least one level deep, example: ./content")
	}

	if len(*xdst) > 0 {
		translate(*xsrc, *xdst, tld)
	} else {
		serve(*xsrc, tld)
	}
}
Example #2
0
func translate(src, dst, tld string) {

	if !isEmpty(dst) {
		clog.Fatalf("Target directory %s is not empty", dst)
	}

	var idx Index = make(map[string]int)
	walker := func(fsrc string, f os.FileInfo, err error) error {
		return setup(dst, fsrc, f, idx, err)
	}
	if err := filepath.Walk(src, walker); err != nil {
		clog.Fatalf("Filewalk %v", err)
	}

	json, err := json.Marshal(idx)
	if err != nil {
		clog.Fatalf("During Index JSON Marshal %v", err)
	}

	jfile := strings.TrimRight(dst, "/")
	jfile += "/" + tld + "/index.json"
	err = ioutil.WriteFile(jfile, json, 0666)
	if err != nil {
		clog.Fatalf("Error writing json file: %s", jfile)
	}
}
Example #3
0
func serve(cdir string, tld string) {
	tempDir, _ := ioutil.TempDir("", "tut")
	tempDir += string(os.PathSeparator)
	defer os.RemoveAll(tempDir)
	clog.Log("Workdir %s", tempDir)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		<-c
		os.RemoveAll(tempDir)
		clog.Fatal("Stopped")
	}()

	var idx Index
	idx = make(map[string]int)

	walker := func(src string, f os.FileInfo, err error) error {
		return setup(tempDir, src, f, idx, err)
	}

	if err := filepath.Walk(cdir, walker); err != nil {
		clog.Fatalf("Filewalk %v", err)
	}

	getindex := func(w http.ResponseWriter, r *http.Request) {
		json, err := json.Marshal(idx)
		if err != nil {
			clog.Fatalf("During Index JSON Marshal %v", err)
		}
		w.Header().Set("Content-Type", "application/json")
		w.Write(json)
	}
	http.HandleFunc("/tutorial/index.json", getindex)

	url, _ := url.Parse("http://localhost:8093")
	rp := httputil.NewSingleHostReverseProxy(url)
	http.Handle("/query", rp)

	fs := http.FileServer(http.Dir(tempDir + "/" + tld + "/"))
	http.Handle("/tutorial/", http.StripPrefix("/tutorial/", fs))

	http.Handle("/", http.RedirectHandler("/tutorial/index.html#1", 302))

	clog.Log("Running at http://localhost:8000/")
	go func() {
		for {
			filepath.Walk(cdir, walker)
			time.Sleep(2 * time.Second)
		}
	}()

	// last step
	if err := http.ListenAndServe(":8000", nil); err != nil {
		clog.Fatalf("ListenAndServe %v", err)
	}
}
Example #4
0
func main() {
	flag.Parse()

	if *devMode {
		ast.EnableDeveloperFunctions()
		if *logKeys == "" {
			clog.ParseLogFlags(devModeDefaultLogKeys)
			clog.Log("Developer Mode Enabled (default developer logging)")
		} else {
			clog.ParseLogFlag(*logKeys)
			clog.Log("Developer Mode Enabled (custom command-line logging)")
		}
	}

	if *profileMode {
		clog.Log("Enabling HTTP Profiling on :6060")
		go func() {
			realhttp.ListenAndServe(":6060", nil)
		}()
	}

	go dumpOnSignalForPlatform()

	// create a QueryChannel
	queryChannel := make(network.QueryChannel)

	// create one or more network endpoints
	httpEndpoint := http.NewHttpEndpoint(*addr, *staticPath)
	httpEndpoint.SendQueriesTo(queryChannel)

	err := server.Server(VERSION, *couchbaseSite, *defaultPoolName, queryChannel, queryTimeout)
	if err != nil {
		clog.Fatalf("Unable to run server, err: %v", err)
	}
}
Example #5
0
func isEmpty(dir string) bool {
	empty := true
	walker := func(dir string, f os.FileInfo, err error) error {
		if f.IsDir() {
			return nil
		}
		empty = false
		return nil
	}
	if err := filepath.Walk(dir, walker); err != nil {
		clog.Fatalf("Filewalk %v", err)
	}
	return empty
}
Example #6
0
func (this *NotOperator) Evaluate(item *dparval.Value) (*dparval.Value, error) {
	ov, err := this.Operand.Evaluate(item)
	if err != nil {
		return nil, err
	}

	oval := ov.Value()
	operandBoolVal := ValueInBooleanContext(oval)
	switch operandBoolVal := operandBoolVal.(type) {
	case bool:
		return dparval.NewValue(!operandBoolVal), nil
	case nil:
		return dparval.NewValue(nil), nil
	default:
		clog.Fatalf("Unexpected type %T in NOT", operandBoolVal)
		return dparval.NewValue(nil), nil
	}
}
Example #7
0
func (this *SimpleExecutablePipelineBuilder) Build(p *plan.Plan, q network.Query) (*xpipeline.ExecutablePipeline, error) {
	rv := &xpipeline.ExecutablePipeline{}

	var lastOperator xpipeline.Operator = nil
	currentElement := p.Root

	for currentElement != nil {
		var currentOperator xpipeline.Operator = nil
		switch currentElement := currentElement.(type) {
		case *plan.FastCount:
			pool, err := this.site.PoolByName(currentElement.Pool)
			if err != nil {
				return nil, err
			}
			bucket, err := pool.BucketByName(currentElement.Bucket)
			if err != nil {
				return nil, err
			}

			var countIndex catalog.CountIndex
			if currentElement.CountIndex != "" {
				index, err := bucket.IndexByName(currentElement.CountIndex)
				if err != nil {
					return nil, err
				}
				countIndex = index.(catalog.CountIndex) // FIXME: need static type safety
			}
			currentOperator = xpipeline.NewFastCount(bucket, countIndex, currentElement.Expr, currentElement.Ranges)
		case *plan.Scan:
			pool, err := this.site.PoolByName(currentElement.Pool)
			if err != nil {
				return nil, err
			}
			bucket, err := pool.BucketByName(currentElement.Bucket)
			if err != nil {
				return nil, err
			}
			index, err := bucket.IndexByName(currentElement.ScanIndex)
			if err != nil {
				return nil, err
			}
			scanIndex := index.(catalog.ScanIndex) // FIXME: need static type safety
			currentOperator = xpipeline.NewScan(bucket, scanIndex, currentElement.Ranges, currentElement.As)
		case *plan.KeyScan:
			currentOperator = xpipeline.NewKeyScan(currentElement.KeyList)
		case *plan.KeyJoin:
			pool, err := this.site.PoolByName(currentElement.Pool)
			if err != nil {
				return nil, err
			}
			bucket, err := pool.BucketByName(currentElement.Bucket)
			if err != nil {
				return nil, err
			}
			if currentElement.Oper == "NEST" {
				currentOperator = xpipeline.NewKeyNest(bucket, currentElement.Projection, currentElement.JoinType, currentElement.Keys, currentElement.As)
			} else {
				currentOperator = xpipeline.NewKeyJoin(bucket, currentElement.Projection, currentElement.JoinType, currentElement.Keys, currentElement.As)
			}
		case *plan.Fetch:
			pool, err := this.site.PoolByName(currentElement.Pool)
			if err != nil {
				return nil, err
			}
			bucket, err := pool.BucketByName(currentElement.Bucket)
			if err != nil {
				return nil, err
			}
			currentOperator = xpipeline.NewFetch(bucket, currentElement.Projection, currentElement.As)
			if currentElement.Ids != nil {
				fetchOperator := currentOperator.(*xpipeline.Fetch)
				fetchOperator.SetIds(currentElement.Ids)
			}
		case *plan.Filter:
			currentOperator = xpipeline.NewFilter(currentElement.Expr)
		case *plan.Order:
			currentOperator = xpipeline.NewOrder(currentElement.Sort, currentElement.ExplicitAliases)
		case *plan.Limit:
			currentOperator = xpipeline.NewLimit(currentElement.Val)
		case *plan.Offset:
			currentOperator = xpipeline.NewOffset(currentElement.Val)
		case *plan.Projector:
			currentOperator = xpipeline.NewProject(currentElement.Result, currentElement.ProjectEmpty)
		case *plan.Unnest:
			currentOperator = xpipeline.NewUnnest(currentElement.Over, currentElement.JoinType, currentElement.As)
		case *plan.EliminateDuplicates:
			currentOperator = xpipeline.NewEliminateDuplicates()
		case *plan.Grouper:
			currentOperator = xpipeline.NewGrouper(currentElement.Group, currentElement.Aggregates)
		case *plan.Explain:
			currentOperator = xpipeline.NewExplain(currentElement.Input)
		case *plan.CreateIndex:
			pool, err := this.site.PoolByName(currentElement.Pool)
			if err != nil {
				return nil, err
			}
			bucket, err := pool.BucketByName(currentElement.Bucket)
			if err != nil {
				return nil, err
			}
			currentOperator = xpipeline.NewCreateIndex(bucket, currentElement.Name, currentElement.IndexType, currentElement.Primary, currentElement.On)
		case *plan.DropIndex:
			pool, err := this.site.PoolByName(currentElement.Pool)
			if err != nil {
				return nil, err
			}
			bucket, err := pool.BucketByName(currentElement.Bucket)
			if err != nil {
				return nil, err
			}
			index, err := bucket.IndexByName(currentElement.Name)
			if err != nil {
				return nil, err
			}
			currentOperator = xpipeline.NewDropIndex(index)
		}

		// pass reference to query
		currentOperator.SetQuery(q)

		//link root of xpipeline
		if rv.Root == nil {
			rv.Root = currentOperator
		}

		// link previous operator to this one
		if lastOperator != nil {
			lastOperator.SetSource(currentOperator)
		}

		// advance to next
		lastOperator = currentOperator

		sources := currentElement.Sources()
		if len(sources) > 1 {
			// FIXME future operators like JOIN will have more than one source
			clog.Fatalf("multiple sources not yet supported")
		} else if len(sources) == 1 {
			currentElement = sources[0]
		} else {
			currentElement = nil
		}
	}

	return rv, nil
}