Browse Source

Merge http.go into update.go and repo.go into main.go

pull/8/head
Daniel Martí 11 years ago
parent
commit
00a3fcee88
  1. 56
      cmd/fdroidcl/http.go
  2. 1
      cmd/fdroidcl/list.go
  3. 31
      cmd/fdroidcl/main.go
  4. 10
      cmd/fdroidcl/repo.go
  5. 47
      cmd/fdroidcl/update.go

56
cmd/fdroidcl/http.go

@ -1,56 +0,0 @@
/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
func respEtag(resp *http.Response) string {
etags, e := resp.Header["Etag"]
if !e || len(etags) == 0 {
return ""
}
return etags[0]
}
func downloadEtag(url, path string) error {
fmt.Printf("Downloading %s...", url)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
etagPath := path + "-etag"
if _, err := os.Stat(path); err == nil {
etag, _ := ioutil.ReadFile(etagPath)
req.Header.Add("If-None-Match", string(etag))
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotModified {
fmt.Println(" not modified")
return nil
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
_, err = io.Copy(f, resp.Body)
err2 := ioutil.WriteFile(etagPath, []byte(respEtag(resp)), 0644)
if err != nil {
return err
}
if err2 != nil {
return err2
}
fmt.Println(" done")
return nil
}

1
cmd/fdroidcl/list.go

@ -28,6 +28,7 @@ func runList(args []string) {
}
func mustLoadIndex() *fdroidcl.Index {
repoPubkey := ""
p := indexPath(repoName)
f, err := os.Open(p)
if err != nil {

31
cmd/fdroidcl/main.go

@ -10,19 +10,10 @@ import (
"os"
)
func init() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: fdroidcl [-h] <command> [<args>]")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Available commands:")
fmt.Fprintln(os.Stderr, " update Update the index")
fmt.Fprintln(os.Stderr, " list List all available apps")
fmt.Fprintln(os.Stderr, " search <term...> Search available apps")
fmt.Fprintln(os.Stderr, " show <appid...> Show detailed info of an app")
fmt.Fprintln(os.Stderr, " devices List connected devices")
fmt.Fprintln(os.Stderr, " installed List installed apps")
}
}
const (
repoName = "repo"
repoURL = "https://f-droid.org/repo"
)
// A Command is an implementation of a go command
// like go build or go fix.
@ -41,6 +32,20 @@ type Command struct {
Flag flag.FlagSet
}
func init() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: fdroidcl [-h] <command> [<args>]")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Available commands:")
fmt.Fprintln(os.Stderr, " update Update the index")
fmt.Fprintln(os.Stderr, " list List all available apps")
fmt.Fprintln(os.Stderr, " search <term...> Search available apps")
fmt.Fprintln(os.Stderr, " show <appid...> Show detailed info of an app")
fmt.Fprintln(os.Stderr, " devices List connected devices")
fmt.Fprintln(os.Stderr, " installed List installed apps")
}
}
// Commands lists the available commands.
var commands = []*Command{
cmdUpdate,

10
cmd/fdroidcl/repo.go

@ -1,10 +0,0 @@
/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */
package main
const (
repoName = "repo"
repoURL = "https://f-droid.org/repo"
repoPubkey = "3082035e30820246a00302010202044c49cd00300d06092a864886f70d01010505003071310b300906035504061302554b3110300e06035504081307556e6b6e6f776e3111300f0603550407130857657468657262793110300e060355040a1307556e6b6e6f776e3110300e060355040b1307556e6b6e6f776e311930170603550403131043696172616e2047756c746e69656b73301e170d3130303732333137313032345a170d3337313230383137313032345a3071310b300906035504061302554b3110300e06035504081307556e6b6e6f776e3111300f0603550407130857657468657262793110300e060355040a1307556e6b6e6f776e3110300e060355040b1307556e6b6e6f776e311930170603550403131043696172616e2047756c746e69656b7330820122300d06092a864886f70d01010105000382010f003082010a028201010096d075e47c014e7822c89fd67f795d23203e2a8843f53ba4e6b1bf5f2fd0e225938267cfcae7fbf4fe596346afbaf4070fdb91f66fbcdf2348a3d92430502824f80517b156fab00809bdc8e631bfa9afd42d9045ab5fd6d28d9e140afc1300917b19b7c6c4df4a494cf1f7cb4a63c80d734265d735af9e4f09455f427aa65a53563f87b336ca2c19d244fcbba617ba0b19e56ed34afe0b253ab91e2fdb1271f1b9e3c3232027ed8862a112f0706e234cf236914b939bcf959821ecb2a6c18057e070de3428046d94b175e1d89bd795e535499a091f5bc65a79d539a8d43891ec504058acb28c08393b5718b57600a211e803f4a634e5c57f25b9b8c4422c6fd90203010001300d06092a864886f70d0101050500038201010008e4ef699e9807677ff56753da73efb2390d5ae2c17e4db691d5df7a7b60fc071ae509c5414be7d5da74df2811e83d3668c4a0b1abc84b9fa7d96b4cdf30bba68517ad2a93e233b042972ac0553a4801c9ebe07bf57ebe9a3b3d6d663965260e50f3b8f46db0531761e60340a2bddc3426098397fda54044a17e5244549f9869b460ca5e6e216b6f6a2db0580b480ca2afe6ec6b46eedacfa4aa45038809ece0c5978653d6c85f678e7f5a2156d1bedd8117751e64a4b0dcd140f3040b021821a8d93aed8d01ba36db6c82372211fed714d9a32607038cdfd565bd529ffc637212aaa2c224ef22b603eccefb5bf1e085c191d4b24fe742b17ab3f55d4e6f05ef"
)

47
cmd/fdroidcl/update.go

@ -5,7 +5,10 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
@ -35,6 +38,50 @@ func updateIndex() error {
return nil
}
func respEtag(resp *http.Response) string {
etags, e := resp.Header["Etag"]
if !e || len(etags) == 0 {
return ""
}
return etags[0]
}
func downloadEtag(url, path string) error {
fmt.Printf("Downloading %s...", url)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
etagPath := path + "-etag"
if _, err := os.Stat(path); err == nil {
etag, _ := ioutil.ReadFile(etagPath)
req.Header.Add("If-None-Match", string(etag))
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotModified {
fmt.Println(" not modified")
return nil
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
_, err = io.Copy(f, resp.Body)
err2 := ioutil.WriteFile(etagPath, []byte(respEtag(resp)), 0644)
if err != nil {
return err
}
if err2 != nil {
return err2
}
fmt.Println(" done")
return nil
}
func indexPath(name string) string {
cache, err := appdir.Cache()
if err != nil {

Loading…
Cancel
Save