diff --git a/dgraph/cmd/cert/run.go b/dgraph/cmd/cert/run.go index 03975c4e1be..e9841cb499e 100644 --- a/dgraph/cmd/cert/run.go +++ b/dgraph/cmd/cert/run.go @@ -37,8 +37,6 @@ func init() { }, } - Cert.Cmd.AddCommand() - flag := Cert.Cmd.Flags() flag.StringP("dir", "d", defaultDir, "directory containing TLS certs and keys") flag.StringP("ca-key", "k", defaultCAKey, "path to the CA private key") diff --git a/dgraph/cmd/dgraph-converter/main.go b/dgraph/cmd/conv/conv.go similarity index 78% rename from dgraph/cmd/dgraph-converter/main.go rename to dgraph/cmd/conv/conv.go index c28eb4dbbcb..322d08fa147 100644 --- a/dgraph/cmd/dgraph-converter/main.go +++ b/dgraph/cmd/conv/conv.go @@ -5,13 +5,12 @@ * with the Commons Clause restriction. */ -package main +package conv import ( "bufio" "compress/gzip" "encoding/json" - "flag" "fmt" "io" "io/ioutil" @@ -23,13 +22,6 @@ import ( "github.com/paulmach/go.geojson" ) -var ( - // TODO - Take a directory here and convert all the files in the directory. - geoFile = flag.String("geo", "", "Location of geo file to convert") - outputFile = flag.String("out", "output.rdf.gz", "Location of output rdf.gz file") - geoPred = flag.String("geopred", "loc", "Predicate to use to store geometries") -) - // TODO: Reconsider if we need this binary. func writeToFile(fpath string, ch chan []byte) error { f, err := os.Create(fpath) @@ -106,7 +98,7 @@ func convertGeoFile(input string, output string) error { geometry := strings.Replace(string(b), `"`, "'", -1) bn := fmt.Sprintf("_:%s-%d", name, count) - rdf := fmt.Sprintf("%s <%s> \"%s\"^^ .\n", bn, *geoPred, geometry) + rdf := fmt.Sprintf("%s <%s> \"%s\"^^ .\n", bn, opt.geopred, geometry) chb <- []byte(rdf) for k, _ := range f.Properties { @@ -127,12 +119,3 @@ func convertGeoFile(input string, output string) error { fmt.Printf("%d features converted. %d rdf's generated\n", count, rdfCount) return <-che } - -func main() { - flag.Parse() - if len(*geoFile) == 0 { - fmt.Printf("The file to be loaded must be specified using the --geo flag.\n") - os.Exit(1) - } - x.Check(convertGeoFile(*geoFile, *outputFile)) -} diff --git a/dgraph/cmd/conv/run.go b/dgraph/cmd/conv/run.go new file mode 100644 index 00000000000..daf6a633332 --- /dev/null +++ b/dgraph/cmd/conv/run.go @@ -0,0 +1,48 @@ +/* + * Copyright 2018 Dgraph Labs, Inc. + * + * This file is available under the Apache License, Version 2.0, + * with the Commons Clause restriction. + */ + +package conv + +import ( + "os" + + "github.com/dgraph-io/dgraph/x" + "github.com/spf13/cobra" +) + +var Conv x.SubCommand + +var opt struct { + geo string + out string + geopred string +} + +func init() { + Conv.Cmd = &cobra.Command{ + Use: "conv", + Short: "Dgraph Geo file converter", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + defer x.StartProfile(Conv.Conf).Stop() + if err := run(); err != nil { + x.Println(err) + os.Exit(1) + } + }, + } + + flag := Conv.Cmd.Flags() + flag.StringVar(&opt.geo, "geo", "", "Location of geo file to convert") + flag.StringVar(&opt.out, "out", "output.rdf.gz", "Location of output rdf.gz file") + flag.StringVar(&opt.geopred, "geopred", "loc", "Predicate to use to store geometries") + Conv.Cmd.MarkFlagRequired("geo") +} + +func run() error { + return convertGeoFile(opt.geo, opt.out) +} diff --git a/dgraph/cmd/dgraph-converter/.gitignore b/dgraph/cmd/dgraph-converter/.gitignore deleted file mode 100644 index cf8dce1efe3..00000000000 --- a/dgraph/cmd/dgraph-converter/.gitignore +++ /dev/null @@ -1 +0,0 @@ -dgraph-converter diff --git a/dgraph/cmd/root.go b/dgraph/cmd/root.go index 385a67d2722..71f11087d09 100644 --- a/dgraph/cmd/root.go +++ b/dgraph/cmd/root.go @@ -14,6 +14,7 @@ import ( "github.com/dgraph-io/dgraph/dgraph/cmd/bulk" "github.com/dgraph-io/dgraph/dgraph/cmd/cert" + "github.com/dgraph-io/dgraph/dgraph/cmd/conv" "github.com/dgraph-io/dgraph/dgraph/cmd/debug" "github.com/dgraph-io/dgraph/dgraph/cmd/live" "github.com/dgraph-io/dgraph/dgraph/cmd/server" @@ -68,7 +69,7 @@ func init() { flag.CommandLine.AddGoFlagSet(goflag.CommandLine) var subcommands = []*x.SubCommand{ - &bulk.Bulk, &cert.Cert, &live.Live, &server.Server, &zero.Zero, + &bulk.Bulk, &cert.Cert, &conv.Conv, &live.Live, &server.Server, &zero.Zero, &version.Version, &debug.Debug, } for _, sc := range subcommands {