Browse Source

Initial import: "update" command

Thomas Jost 3 years ago
commit
52e5a379d4
Signed by: Thomas Jost <schnouki@schnouki.net> GPG Key ID: 3F3B990246141762
8 changed files with 221 additions and 0 deletions
  1. 3
    0
      .gitignore
  2. 21
    0
      LICENSE
  3. 23
    0
      Makefile
  4. 30
    0
      cmd/root.go
  5. 106
    0
      cmd/update.go
  6. 24
    0
      glide.lock
  7. 7
    0
      glide.yaml
  8. 7
    0
      main.go

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
1
+/.gopath
2
+/islas
3
+/vendor

+ 21
- 0
LICENSE View File

@@ -0,0 +1,21 @@
1
+The MIT License (MIT)
2
+
3
+Copyright © 2018 Thomas Jost
4
+
5
+Permission is hereby granted, free of charge, to any person obtaining a copy
6
+of this software and associated documentation files (the "Software"), to deal
7
+in the Software without restriction, including without limitation the rights
8
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+copies of the Software, and to permit persons to whom the Software is
10
+furnished to do so, subject to the following conditions:
11
+
12
+The above copyright notice and this permission notice shall be included in
13
+all copies or substantial portions of the Software.
14
+
15
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+THE SOFTWARE.

+ 23
- 0
Makefile View File

@@ -0,0 +1,23 @@
1
+PACKAGE = islas
2
+GOPATH  = $(CURDIR)/.gopath
3
+BASE    = $(GOPATH)/src/$(PACKAGE)
4
+
5
+SOURCE  = main.go $(wildcard cmd/*.go)
6
+
7
+.PHONY: all clean
8
+all: islas
9
+
10
+clean:
11
+	rm -f islas
12
+
13
+islas: $(BASE) $(SOURCE) vendor
14
+	cd $(BASE) && CGO_ENABLED=1 go build .
15
+
16
+$(BASE):
17
+	@mkdir -p $(dir $@)
18
+	@ln -sf $(CURDIR) $@
19
+
20
+vendor: $(BASE) vendor/.ok
21
+vendor/.ok:
22
+	glide install
23
+	@touch vendor/.ok

+ 30
- 0
cmd/root.go View File

@@ -0,0 +1,30 @@
1
+package cmd
2
+
3
+import (
4
+	"fmt"
5
+	"os"
6
+
7
+	"github.com/casimir/xdg-go"
8
+	"github.com/spf13/cobra"
9
+)
10
+
11
+// rootCmd represents the base command when called without any subcommands
12
+var rootCmd = &cobra.Command{
13
+	Use:   "islas",
14
+	Short: "Quickly get timetables for STAN bus and tramway lines",
15
+	Long: `A tool that allows to quickly get infos about STAN bus and tramway lines,
16
+and get real-time timetables for any stop in the STAN network.`,
17
+	Run: func(cmd *cobra.Command, args []string) {},
18
+}
19
+
20
+func init() {
21
+	xdg.SetName("islas")
22
+}
23
+
24
+// Execute runs the root command.
25
+func Execute() {
26
+	if err := rootCmd.Execute(); err != nil {
27
+		fmt.Println(err)
28
+		os.Exit(1)
29
+	}
30
+}

+ 106
- 0
cmd/update.go View File

@@ -0,0 +1,106 @@
1
+package cmd
2
+
3
+import (
4
+	"compress/gzip"
5
+	"io"
6
+	"io/ioutil"
7
+	"net/http"
8
+	"os"
9
+	"path"
10
+
11
+	"github.com/DHowett/go-plist"
12
+	"github.com/casimir/xdg-go"
13
+	log "github.com/sirupsen/logrus"
14
+	"github.com/spf13/cobra"
15
+)
16
+
17
+const (
18
+	DatabaseXML = "http://www.reseau-stan.com/android/db/stan/db-2.2.xml"
19
+)
20
+
21
+func init() {
22
+	rootCmd.AddCommand(updateCmd)
23
+}
24
+
25
+// GetDBPath returns the path to the database file.
26
+func GetDBPath() string {
27
+	return xdg.CachePath("stan.db")
28
+}
29
+
30
+// updateCommand is the command that downloads an updated database.
31
+var updateCmd = &cobra.Command{
32
+	Use:   "update",
33
+	Short: "Update the STAN database",
34
+	Long:  "Update the STAN database that contains the list of all the lines and stops",
35
+	Run:   UpdateDB,
36
+}
37
+
38
+type stanDbDescriptor struct {
39
+	NetworkID      string
40
+	NetworkFormat  string
41
+	NetworkVersion string
42
+	URL            []string `plist:"url"`
43
+	Size           int
44
+}
45
+
46
+type stanDbsDescriptor struct {
47
+	Databases []stanDbDescriptor `plist:"databases"`
48
+}
49
+
50
+// UpdateDB downloads an updated database.
51
+func UpdateDB(cmd *cobra.Command, args []string) {
52
+	log.Info("Starting the DB update")
53
+
54
+	// Get the XML descriptor
55
+	resp, err := http.Get(DatabaseXML)
56
+	if err != nil {
57
+		log.WithError(err).Fatal("Cannot download the database XML")
58
+	}
59
+	defer resp.Body.Close()
60
+	xmlBody, err := ioutil.ReadAll(resp.Body)
61
+	if err != nil {
62
+		log.WithError(err).Fatal("Cannot read the database XML")
63
+	}
64
+
65
+	var descriptor stanDbsDescriptor
66
+	if _, err := plist.Unmarshal(xmlBody, &descriptor); err != nil {
67
+		log.WithError(err).Fatal("Cannot parse the database XML")
68
+	}
69
+
70
+	dbPath := GetDBPath()
71
+	url := descriptor.Databases[0].URL[0]
72
+	log.WithFields(log.Fields{
73
+		"url":  url,
74
+		"path": dbPath,
75
+	}).Info("Downloading the actual DB")
76
+
77
+	resp, err = http.Get(url)
78
+	if err != nil {
79
+		log.WithError(err).Fatal("Cannot download the database")
80
+	}
81
+	defer resp.Body.Close()
82
+
83
+	gzReader, err := gzip.NewReader(resp.Body)
84
+	if err != nil {
85
+		log.WithError(err).Fatal("Cannot decompress the database")
86
+	}
87
+	gzReader.Multistream(false)
88
+	defer gzReader.Close()
89
+
90
+	if err := os.MkdirAll(path.Dir(dbPath), 0700); err != nil {
91
+		log.WithError(err).Fatal("Cannot create the database parent directory")
92
+	}
93
+
94
+	dbFile, err := os.Create(dbPath)
95
+	if err != nil {
96
+		log.WithError(err).Fatal("Cannot create the database file")
97
+	}
98
+	defer dbFile.Close()
99
+
100
+	dbSize, err := io.Copy(dbFile, gzReader)
101
+	if err != nil {
102
+		log.WithError(err).Fatal("Cannot write the database file")
103
+	}
104
+
105
+	log.WithField("size", dbSize).Info("Database downloaded")
106
+}

+ 24
- 0
glide.lock View File

@@ -0,0 +1,24 @@
1
+hash: faf707a540ffdc0044bbf8bf1ed8f3371d851ebfafef87f53c45302db86091e7
2
+updated: 2018-06-20T11:36:42.243348956+02:00
3
+imports:
4
+- name: github.com/casimir/xdg-go
5
+  version: 372ccc2180dab73316615641d5617c5ed3e35529
6
+- name: github.com/DHowett/go-plist
7
+  version: 500bd5b9081b5957ac10389f86e069869f00c348
8
+- name: github.com/inconshreveable/mousetrap
9
+  version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75
10
+- name: github.com/sirupsen/logrus
11
+  version: c155da19408a8799da419ed3eeb0cb5db0ad5dbc
12
+- name: github.com/spf13/cobra
13
+  version: 1e58aa3361fd650121dceeedc399e7189c05674a
14
+- name: github.com/spf13/pflag
15
+  version: 3ebe029320b2676d667ae88da602a5f854788a8a
16
+- name: golang.org/x/crypto
17
+  version: 41d678d1df78cd0410143162dff954e6dc09300f
18
+  subpackages:
19
+  - ssh/terminal
20
+- name: golang.org/x/sys
21
+  version: d75a52659825e75fff6158388dddc6a5b04f9ba5
22
+  subpackages:
23
+  - unix
24
+testImports: []

+ 7
- 0
glide.yaml View File

@@ -0,0 +1,7 @@
1
+package: .
2
+import:
3
+- package: github.com/spf13/cobra
4
+- package: github.com/casimir/xdg-go
5
+- package: github.com/sirupsen/logrus
6
+  version: ^1.0.5
7
+- package: github.com/DHowett/go-plist

+ 7
- 0
main.go View File

@@ -0,0 +1,7 @@
1
+package main
2
+
3
+import "islas/cmd"
4
+
5
+func main() {
6
+	cmd.Execute()
7
+}

Loading…
Cancel
Save