Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit f0a09af

Browse files
committed
various: initial minor tweaks for usability
1 parent 772efd3 commit f0a09af

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

cmd/list.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"time"
56

67
"github.com/spf13/cobra"
78
)
@@ -65,11 +66,21 @@ func list(args []string) error {
6566
return err
6667
}
6768
}
68-
_, err = fmt.Fprintf(fOut, " File last modified: %s\n", j.LastModified)
69+
// The server gives us the last modified and repo modified dates in pre-formatted UTC timezone. For now, lets
70+
// convert these back to the users local time
71+
z, err := time.Parse(time.RFC822, j.LastModified)
6972
if err != nil {
7073
return err
7174
}
72-
_, err = fmt.Fprintf(fOut, " Repository last updated: %s\n\n", j.RepoModified)
75+
_, err = fmt.Fprintf(fOut, " File last modified: %s\n", z.Local().Format(time.RFC1123))
76+
if err != nil {
77+
return err
78+
}
79+
z, err = time.Parse(time.RFC822, j.RepoModified)
80+
if err != nil {
81+
return err
82+
}
83+
_, err = fmt.Fprintf(fOut, " Repository last updated: %s\n\n", z.Local().Format(time.RFC1123))
7384
if err != nil {
7485
return err
7586
}

cmd/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func branchLog(args []string) error {
8888
func createCommitText(c commitEntry, licList map[string]string) string {
8989
s := fmt.Sprintf(" * Commit: %s\n", c.ID)
9090
s += fmt.Sprintf(" Author: %s <%s>\n", c.AuthorName, c.AuthorEmail)
91-
s += fmt.Sprintf(" Date: %v\n", c.Timestamp.Format(time.UnixDate))
91+
s += fmt.Sprintf(" Date: %v\n", c.Timestamp.Local().Format(time.RFC1123))
9292
if c.Tree.Entries[0].LicenceSHA != "" {
9393
s += fmt.Sprintf(" Licence: %s\n\n", licList[c.Tree.Entries[0].LicenceSHA])
9494
} else {

cmd/pull.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ func pull(args []string) error {
187187
}
188188

189189
// Download the database file
190+
// TODO: Use a streaming download approach, so download progress can be shown. Something like this should help:
191+
// https://stackoverflow.com/questions/22108519/how-do-i-read-a-streaming-response-body-using-golangs-net-http-package
192+
_, err = fmt.Fprintf(fOut, "Downloading '%s' from %s...\n", db, cloud)
193+
if err != nil {
194+
return err
195+
}
190196
resp, body, err := retrieveDatabase(db, pullCmdBranch, pullCmdCommit)
191197
if err != nil {
192198
return err
@@ -201,17 +207,17 @@ func pull(args []string) error {
201207
}
202208

203209
// Calculate the sha256 of the database file
204-
s := sha256.Sum256([]byte(body))
210+
s := sha256.Sum256(body)
205211
shaSum := hex.EncodeToString(s[:])
206212

207213
// Write the database file to disk in the cache directory
208-
err = ioutil.WriteFile(filepath.Join(".dio", db, "db", shaSum), []byte(body), 0644)
214+
err = ioutil.WriteFile(filepath.Join(".dio", db, "db", shaSum), body, 0644)
209215
if err != nil {
210216
return err
211217
}
212218

213219
// Write the database file to disk again, this time in the working directory
214-
err = ioutil.WriteFile(db, []byte(body), 0644)
220+
err = ioutil.WriteFile(db, body, 0644)
215221
if err != nil {
216222
return err
217223
}
@@ -250,7 +256,7 @@ func pull(args []string) error {
250256

251257
// Display success message to the user
252258
comID := resp.Header.Get("Commit-Id")
253-
_, err = fmt.Fprintf(fOut, "Database '%s' downloaded from %s\n", db, cloud)
259+
_, err = fmt.Fprintln(fOut, "Downloaded complete")
254260
if err != nil {
255261
return err
256262
}
@@ -267,8 +273,5 @@ func pull(args []string) error {
267273
}
268274
}
269275
_, err = numFormat.Fprintf(fOut, " * Size: %d bytes\n", len(body))
270-
if err != nil {
271-
return err
272-
}
273-
return nil
276+
return err
274277
}

cmd/shared.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323
// Check if the database with the given SHA256 checksum is in local cache. If it's not then download and cache it
2424
func checkDBCache(db, shaSum string) (err error) {
2525
if _, err = os.Stat(filepath.Join(".dio", db, "db", shaSum)); os.IsNotExist(err) {
26-
var body string
26+
var body []byte
2727
_, body, err = retrieveDatabase(db, pullCmdBranch, pullCmdCommit)
2828
if err != nil {
2929
return
3030
}
3131

3232
// Verify the SHA256 checksum of the new download
33-
s := sha256.Sum256([]byte(body))
33+
s := sha256.Sum256(body)
3434
thisSum := hex.EncodeToString(s[:])
3535
if thisSum != shaSum {
3636
// The newly downloaded database file doesn't have the expected checksum. Abort.
@@ -39,7 +39,7 @@ func checkDBCache(db, shaSum string) (err error) {
3939
}
4040

4141
// Write the database file to disk in the cache directory
42-
err = ioutil.WriteFile(filepath.Join(".dio", db, "db", shaSum), []byte(body), 0644)
42+
err = ioutil.WriteFile(filepath.Join(".dio", db, "db", shaSum), body, 0644)
4343
}
4444
return
4545
}
@@ -525,7 +525,7 @@ func mergeMetadata(origMeta metaData, newMeta metaData) (mergedMeta metaData, er
525525
}
526526

527527
// Retrieves a database from DBHub.io
528-
func retrieveDatabase(db string, branch string, commit string) (resp rq.Response, body string, err error) {
528+
func retrieveDatabase(db string, branch string, commit string) (resp rq.Response, body []byte, err error) {
529529
dbURL := fmt.Sprintf("%s/%s/%s", cloud, certUser, db)
530530
req := rq.New().TLSClientConfig(&TLSConfig).Get(dbURL)
531531
if branch != "" {
@@ -534,7 +534,7 @@ func retrieveDatabase(db string, branch string, commit string) (resp rq.Response
534534
req.Query(fmt.Sprintf("commit=%s", url.QueryEscape(commit)))
535535
}
536536
var errs []error
537-
resp, body, errs = req.End()
537+
resp, body, errs = req.EndBytes()
538538
if errs != nil {
539539
log.Print("Errors when downloading database:")
540540
for _, err := range errs {

0 commit comments

Comments
 (0)