Skip to content

Commit

Permalink
implement all subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed May 26, 2022
1 parent 5291909 commit fc0c58f
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 12 deletions.
10 changes: 7 additions & 3 deletions cmd/communes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/tsirysndr/mada/mada"
)

// communesCmd represents the communes command
Expand All @@ -47,12 +46,17 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("communes called")
outputInJSON, _ := cmd.Flags().GetBool("json")
limit, _ := cmd.Flags().GetInt("limit")
c := mada.Commune{}
c.List(outputInJSON, limit)
},
}

func init() {
rootCmd.AddCommand(communesCmd)
communesCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
communesCmd.Flags().Int("limit", 100, "Limit the number of communes")

// Here you will define your flags and configuration settings.

Expand Down
10 changes: 7 additions & 3 deletions cmd/districts.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/tsirysndr/mada/mada"
)

// districtsCmd represents the districts command
Expand All @@ -47,12 +46,17 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("districts called")
outputInJSON, _ := cmd.Flags().GetBool("json")
limit, _ := cmd.Flags().GetInt("limit")
d := mada.District{}
d.List(outputInJSON, limit)
},
}

func init() {
rootCmd.AddCommand(districtsCmd)
districtsCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
districtsCmd.Flags().Int("limit", 100, "Limit the number of communes")

// Here you will define your flags and configuration settings.

Expand Down
10 changes: 7 additions & 3 deletions cmd/fokontany.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/tsirysndr/mada/mada"
)

// fokontanyCmd represents the fokontany command
Expand All @@ -47,12 +46,17 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("fokontany called")
outputInJSON, _ := cmd.Flags().GetBool("json")
limit, _ := cmd.Flags().GetInt("limit")
f := mada.Fokontany{}
f.List(outputInJSON, limit)
},
}

func init() {
rootCmd.AddCommand(fokontanyCmd)
fokontanyCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
fokontanyCmd.Flags().Int("limit", 100, "Limit the number of communes")

// Here you will define your flags and configuration settings.

Expand Down
10 changes: 7 additions & 3 deletions cmd/regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/tsirysndr/mada/mada"
)

// regionsCmd represents the regions command
Expand All @@ -47,12 +46,17 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("regions called")
outputInJSON, _ := cmd.Flags().GetBool("json")
limit, _ := cmd.Flags().GetInt("limit")
r := mada.Region{}
r.List(outputInJSON, limit)
},
}

func init() {
rootCmd.AddCommand(regionsCmd)
regionsCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
regionsCmd.Flags().Int("limit", 100, "Limit the number of communes")

// Here you will define your flags and configuration settings.

Expand Down
35 changes: 35 additions & 0 deletions mada/commune.go
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
package mada

import (
"encoding/json"
"fmt"

"github.com/blevesearch/bleve/v2"
)

type Commune struct{}

func (c *Commune) List(outputInJSON bool, limit int) {
index, err := InitializeBleve()
if err != nil {
panic(err)
}
query := bleve.NewMatchQuery("commune")
search := bleve.NewSearchRequest(query)
search.Fields = []string{"*"}
search.Size = limit

searchResults, err := index.Search(search)
if err != nil {
fmt.Println(err)
return
}

if !outputInJSON {
fmt.Println(searchResults)
return
}

b, _ := json.MarshalIndent(searchResults.Hits, "", " ")

fmt.Println(string(b))
}
35 changes: 35 additions & 0 deletions mada/district.go
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
package mada

import (
"encoding/json"
"fmt"

"github.com/blevesearch/bleve/v2"
)

type District struct{}

func (d *District) List(outputInJSON bool, limit int) {
index, err := InitializeBleve()
if err != nil {
panic(err)
}
query := bleve.NewMatchQuery("district")
search := bleve.NewSearchRequest(query)
search.Fields = []string{"*"}
search.Size = limit

searchResults, err := index.Search(search)
if err != nil {
fmt.Println(err)
return
}

if !outputInJSON {
fmt.Println(searchResults)
return
}

b, _ := json.MarshalIndent(searchResults.Hits, "", " ")

fmt.Println(string(b))
}
35 changes: 35 additions & 0 deletions mada/fokontany.go
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
package mada

import (
"encoding/json"
"fmt"

"github.com/blevesearch/bleve/v2"
)

type Fokontany struct{}

func (f *Fokontany) List(outputInJSON bool, limit int) {
index, err := InitializeBleve()
if err != nil {
panic(err)
}
query := bleve.NewMatchQuery("fokontany")
search := bleve.NewSearchRequest(query)
search.Fields = []string{"*"}
search.Size = limit

searchResults, err := index.Search(search)
if err != nil {
fmt.Println(err)
return
}

if !outputInJSON {
fmt.Println(searchResults)
return
}

b, _ := json.MarshalIndent(searchResults.Hits, "", " ")

fmt.Println(string(b))
}
35 changes: 35 additions & 0 deletions mada/region.go
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
package mada

import (
"encoding/json"
"fmt"

"github.com/blevesearch/bleve/v2"
)

type Region struct{}

func (r *Region) List(outputInJSON bool, limit int) {
index, err := InitializeBleve()
if err != nil {
panic(err)
}
query := bleve.NewMatchQuery("region")
search := bleve.NewSearchRequest(query)
search.Fields = []string{"*"}
search.Size = limit

searchResults, err := index.Search(search)
if err != nil {
fmt.Println(err)
return
}

if !outputInJSON {
fmt.Println(searchResults)
return
}

b, _ := json.MarshalIndent(searchResults.Hits, "", " ")

fmt.Println(string(b))
}

0 comments on commit fc0c58f

Please sign in to comment.