| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package main
- import (
- "bytes"
- "database/sql"
- "encoding/csv"
- "encoding/xml"
- "fmt"
- "io"
- "log"
- "os"
- "github.com/northbright/pathhelper"
- "github.com/northbright/xls2csv-go/xls2csv"
- "golang.org/x/net/html/charset"
- _ "github.com/lib/pq"
- )
- func importCreditCards(path string) {
- f, err := os.Open(path + "/dataimport/cc2.csv")
- if err != nil {
- log.Fatal(err)
- }
- // Get absolute path of XLS file.
- xls, _ := pathhelper.GetAbsPath(path + "/dataimport/cc2.csv")
- // Call XLS2CSV() to convert XLS and get all records.
- if records, err = xls2csv.XLS2CSV(f, 0); err != nil {
- log.Printf("XLS2CSV() error: %v\n", err)
- goto end
- }
- for i, row := range records {
- fmt.Printf("%v", row)
- if i != len(records)-1 {
- fmt.Printf("\n")
- }
- }
- // remember to close the file at the end of the program
- defer f.Close()
- // read csv values using csv.Reader
- csvReader := csv.NewReader(f)
- for {
- rec, err := csvReader.Read()
- if err == io.EOF {
- break
- }
- if err != nil {
- log.Fatal(err)
- }
- // do something with read line
- fmt.Printf("%+v\n", rec[0])
- }
- }
- func importAccount(path string) {
- transactions := &AccountTransactions{}
- data, err := os.ReadFile(path + "/dataimport/2016-2022.xml")
- reader := bytes.NewReader(data)
- decoder := xml.NewDecoder(reader)
- decoder.CharsetReader = charset.NewReaderLabel
- err = decoder.Decode(&transactions)
- // Open our xmlFile
- // if we os.Open returns an error then handle it
- if err != nil {
- fmt.Println(err)
- }
- // defer the closing of our xmlFile so that we can parse it later on
- //defer xmlFile.Close()
- // read our opened xmlFile as a byte array.
- //byteValue, _ := ioutil.ReadAll(xmlFile)
- // we initialize our Users array
- // we unmarshal our byteArray which contains our
- // xmlFiles content into 'users' which we defined above
- //xml.Unmarshal([]byte(data), &transactions)
- fmt.Println(transactions)
- // we iterate through every user within our users array and
- // print out the user Type, their name, and their facebook url
- // as just an example
- for i := 0; i < len(transactions.Transactions); i++ {
- fmt.Println("User Type: " + transactions.Transactions[i].ID)
- fmt.Println("User Name: " + transactions.Transactions[i].Date)
- fmt.Println("Facebook Url: " + transactions.Transactions[i].Amount)
- }
- }
- func main() {
- argsWithoutProg := os.Args[1:]
- path, err := os.Getwd()
- if err != nil {
- log.Println(err)
- }
- fmt.Println("Starting importer on path " + path)
- fmt.Println("Program " + argsWithoutProg[0])
- switch argsWithoutProg[0] {
- case "creditcards":
- importCreditCards(path)
- break
- default:
- importAccount(path)
- }
- fmt.Println("Connecting to database")
- psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
- "password=%s dbname=%s sslmode=disable",
- host, port, user, password, dbname)
- db, err := sql.Open("postgres", psqlInfo)
- if err != nil {
- panic(err)
- }
- sqlStatement := `
- INSERT INTO creditcards.transactions (date, description, exchange_rate, fx_amount,local_amount)
- VALUES ('2022-02-02','desc', '1', '1', '1')`
- _, err = db.Exec(sqlStatement)
- if err != nil {
- panic(err)
- }
- defer db.Close()
- }
- const (
- host = "172.23.0.2"
- port = 5432
- user = "postgres"
- password = "example"
- dbname = "books"
- )
- // our struct which contains the complete
- // array of all Users in the file
- type AccountTransactions struct {
- XMLName xml.Name `xml:"main"`
- Transactions []Transaction `xml:"Faerslur"`
- }
- // the user struct, this contains our
- // Type attribute, our user's name and
- // a social struct which will contain all
- // our social links
- type Transaction struct {
- XMLName xml.Name `xml:"Faerslur"`
- ID string `xml:"Radnumer"`
- Date string `xml:"Dagsetning"`
- Amount string `xml:"Upphaed"`
- }
- // a simple struct which contains all our
- // social links
- type Social struct {
- XMLName xml.Name `xml:"social"`
- Facebook string `xml:"facebook"`
- Twitter string `xml:"twitter"`
- Youtube string `xml:"youtube"`
- }
|