diff options
| author | Loyalsoldier <[email protected]> | 2024-07-08 13:54:29 +0800 |
|---|---|---|
| committer | Loyalsoldier <[email protected]> | 2024-07-08 14:23:41 +0800 |
| commit | 56ddd1bd048e6d42645f36cd473a211dca90b5dc (patch) | |
| tree | 1832981ab31a2c457992e053f0dd94453ac475f5 /convert.go | |
| parent | 8c55b323a91acff7cc7f839a371ec9510fe60039 (diff) | |
Refactor: CLI commands
old usage: geoip -l
new usage: geoip list
old usage: geoip -c config.json
new usage: geoip convert -c config.json
Diffstat (limited to 'convert.go')
| -rw-r--r-- | convert.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/convert.go b/convert.go new file mode 100644 index 00000000..6635137f --- /dev/null +++ b/convert.go @@ -0,0 +1,36 @@ +package main + +import ( + "log" + + "github.com/Loyalsoldier/geoip/lib" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(convertCmd) + convertCmd.PersistentFlags().StringP("config", "c", "config.json", "URI of the JSON format config file, support both local file path and remote HTTP(S) URL") +} + +var convertCmd = &cobra.Command{ + Use: "convert", + Aliases: []string{"conv"}, + Short: "Convert geoip data from one format to another by using config file", + Run: func(cmd *cobra.Command, args []string) { + configFile, _ := cmd.Flags().GetString("config") + log.Println("Use config:", configFile) + + instance, err := lib.NewInstance() + if err != nil { + log.Fatal(err) + } + + if err := instance.Init(configFile); err != nil { + log.Fatal(err) + } + + if err := instance.Run(); err != nil { + log.Fatal(err) + } + }, +} |
