Skip to content

Commit

Permalink
Fix: Add JSON export
Browse files Browse the repository at this point in the history
  • Loading branch information
pitw committed Oct 19, 2019
1 parent 13355d7 commit fb232ff
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
57 changes: 49 additions & 8 deletions cmd/subscription/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,42 @@ func main() {
fmt.Println("Username is mandatory")
} else {
if clientid > 0 {
WriteSubscriptionClientCSV(username, password, filename, clientid)
switch format {
case "json":
WriteSubscriptionClientJSON(username, password, filename, clientid)
case "csv":
WriteSubscriptionClientCSV(username, password, filename, clientid)
default:
WriteSubscriptionClientCSV(username, password, filename, clientid)
}
} else {
err := WriteSubscriptionsCSV(username, password, filename)
if err != nil {
fmt.Println(err)

switch format {
case "json":
WriteSubscriptionsJSON(username, password, filename)
case "csv":
WriteSubscriptionsCSV(username, password, filename)
default:
WriteSubscriptionsCSV(username, password, filename)
}

}
}

}

// WriteSubscriptionsCSV parses a all Boll Kaspersky Subscription to CSV
func WriteSubscriptionsCSV(username string, password string, filename string) (err error) {
func WriteSubscriptionsCSV(username string, password string, filename string) {
subsc, err := subscriptionparser.ParseSubscriptions(username, password)
if err != nil {
return err
fmt.Println(err)
}
err = subscriptionparser.WriteCSV(subsc, filename)
if err != nil {
return err
fmt.Println(err)
} else {
fmt.Printf("CSV written to %v", filename)
}
return err
}

// WriteSubscriptionCSV parses a all Boll Kaspersky Subscription to CSV
Expand All @@ -70,3 +82,32 @@ func WriteSubscriptionClientCSV(username string, password string, filename strin
fmt.Printf("CSV written to %v", filename)
}
}

// WriteSubscriptionsJSON parses a all Boll Kaspersky Subscription to CSV
func WriteSubscriptionsJSON(username string, password string, filename string) (err error) {
subsc, err := subscriptionparser.ParseSubscriptions(username, password)
if err != nil {
return err
}
err = subscriptionparser.WriteJSON(subsc, filename)
if err != nil {
return err
} else {
fmt.Printf("JSON written to %v", filename)
}
return err
}

// WriteSubscriptionClientJSON parses a all Boll Kaspersky Subscription to CSV
func WriteSubscriptionClientJSON(username string, password string, filename string, clientid int) {
subsc, err := subscriptionparser.ParseSubscriptionClient(username, password, clientid)
if err != nil {
fmt.Println(err)
}
err = subscriptionparser.WriteJSON(subsc, filename)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("JSON written to %v", filename)
}
}
23 changes: 23 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package subscriptionparser

import (
"encoding/json"
"log"
"os"
)

func WriteJSON(subscriptions []Subscriptions, filename string) (err error) {
file, err := os.Create(filename + ".json")
if err != nil {
return err
}
defer file.Close()

w := json.NewEncoder(file)

if err := w.Encode(subscriptions); err != nil {
log.Fatalln("error writing record to json:", err)
}

return err
}

0 comments on commit fb232ff

Please sign in to comment.