Skip to content

Commit fc0c58f

Browse files
committed
implement all subcommands
1 parent 5291909 commit fc0c58f

File tree

8 files changed

+168
-12
lines changed

8 files changed

+168
-12
lines changed

cmd/communes.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
3131
package cmd
3232

3333
import (
34-
"fmt"
35-
3634
"github.com/spf13/cobra"
35+
"github.com/tsirysndr/mada/mada"
3736
)
3837

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

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

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

cmd/districts.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
3131
package cmd
3232

3333
import (
34-
"fmt"
35-
3634
"github.com/spf13/cobra"
35+
"github.com/tsirysndr/mada/mada"
3736
)
3837

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

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

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

cmd/fokontany.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
3131
package cmd
3232

3333
import (
34-
"fmt"
35-
3634
"github.com/spf13/cobra"
35+
"github.com/tsirysndr/mada/mada"
3736
)
3837

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

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

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

cmd/regions.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
3131
package cmd
3232

3333
import (
34-
"fmt"
35-
3634
"github.com/spf13/cobra"
35+
"github.com/tsirysndr/mada/mada"
3736
)
3837

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

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

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

mada/commune.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
11
package mada
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/blevesearch/bleve/v2"
8+
)
9+
10+
type Commune struct{}
11+
12+
func (c *Commune) List(outputInJSON bool, limit int) {
13+
index, err := InitializeBleve()
14+
if err != nil {
15+
panic(err)
16+
}
17+
query := bleve.NewMatchQuery("commune")
18+
search := bleve.NewSearchRequest(query)
19+
search.Fields = []string{"*"}
20+
search.Size = limit
21+
22+
searchResults, err := index.Search(search)
23+
if err != nil {
24+
fmt.Println(err)
25+
return
26+
}
27+
28+
if !outputInJSON {
29+
fmt.Println(searchResults)
30+
return
31+
}
32+
33+
b, _ := json.MarshalIndent(searchResults.Hits, "", " ")
34+
35+
fmt.Println(string(b))
36+
}

mada/district.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
11
package mada
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/blevesearch/bleve/v2"
8+
)
9+
10+
type District struct{}
11+
12+
func (d *District) List(outputInJSON bool, limit int) {
13+
index, err := InitializeBleve()
14+
if err != nil {
15+
panic(err)
16+
}
17+
query := bleve.NewMatchQuery("district")
18+
search := bleve.NewSearchRequest(query)
19+
search.Fields = []string{"*"}
20+
search.Size = limit
21+
22+
searchResults, err := index.Search(search)
23+
if err != nil {
24+
fmt.Println(err)
25+
return
26+
}
27+
28+
if !outputInJSON {
29+
fmt.Println(searchResults)
30+
return
31+
}
32+
33+
b, _ := json.MarshalIndent(searchResults.Hits, "", " ")
34+
35+
fmt.Println(string(b))
36+
}

mada/fokontany.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
11
package mada
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/blevesearch/bleve/v2"
8+
)
9+
10+
type Fokontany struct{}
11+
12+
func (f *Fokontany) List(outputInJSON bool, limit int) {
13+
index, err := InitializeBleve()
14+
if err != nil {
15+
panic(err)
16+
}
17+
query := bleve.NewMatchQuery("fokontany")
18+
search := bleve.NewSearchRequest(query)
19+
search.Fields = []string{"*"}
20+
search.Size = limit
21+
22+
searchResults, err := index.Search(search)
23+
if err != nil {
24+
fmt.Println(err)
25+
return
26+
}
27+
28+
if !outputInJSON {
29+
fmt.Println(searchResults)
30+
return
31+
}
32+
33+
b, _ := json.MarshalIndent(searchResults.Hits, "", " ")
34+
35+
fmt.Println(string(b))
36+
}

mada/region.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
11
package mada
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/blevesearch/bleve/v2"
8+
)
9+
10+
type Region struct{}
11+
12+
func (r *Region) List(outputInJSON bool, limit int) {
13+
index, err := InitializeBleve()
14+
if err != nil {
15+
panic(err)
16+
}
17+
query := bleve.NewMatchQuery("region")
18+
search := bleve.NewSearchRequest(query)
19+
search.Fields = []string{"*"}
20+
search.Size = limit
21+
22+
searchResults, err := index.Search(search)
23+
if err != nil {
24+
fmt.Println(err)
25+
return
26+
}
27+
28+
if !outputInJSON {
29+
fmt.Println(searchResults)
30+
return
31+
}
32+
33+
b, _ := json.MarshalIndent(searchResults.Hits, "", " ")
34+
35+
fmt.Println(string(b))
36+
}

0 commit comments

Comments
 (0)