Browse Source

cmd/fdroidcl: don't use log for error messages

Also, don't print stuff if we're using the default config.
pull/21/head
Daniel Martí 9 years ago
parent
commit
bbf402016e
  1. 39
      cmd/fdroidcl/main.go

39
cmd/fdroidcl/main.go

@ -7,7 +7,6 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -17,10 +16,15 @@ import (
const cmdName = "fdroidcl" const cmdName = "fdroidcl"
func errExit(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(1)
}
func subdir(dir, name string) string { func subdir(dir, name string) string {
p := filepath.Join(dir, name) p := filepath.Join(dir, name)
if err := os.MkdirAll(p, 0755); err != nil { if err := os.MkdirAll(p, 0755); err != nil {
log.Fatalf("Could not create dir '%s': %v", p, err) errExit("Could not create dir '%s': %v\n", p, err)
} }
return p return p
} }
@ -28,7 +32,7 @@ func subdir(dir, name string) string {
func mustCache() string { func mustCache() string {
dir := basedir.Cache() dir := basedir.Cache()
if dir == "" { if dir == "" {
log.Fatalf("Could not determine cache dir") errExit("Could not determine cache dir\n")
} }
return subdir(dir, cmdName) return subdir(dir, cmdName)
} }
@ -36,7 +40,7 @@ func mustCache() string {
func mustData() string { func mustData() string {
dir := basedir.Data() dir := basedir.Data()
if dir == "" { if dir == "" {
log.Fatalf("Could not determine data dir") errExit("Could not determine data dir\n")
} }
return subdir(dir, cmdName) return subdir(dir, cmdName)
} }
@ -70,19 +74,16 @@ var config = userConfig{
}, },
} }
func readConfig() error { func readConfig() {
f, err := os.Open(configPath()) f, err := os.Open(configPath())
if err != nil { if err != nil {
if os.IsNotExist(err) { return
return nil
}
return fmt.Errorf("could not open config file: %v", err)
} }
defer f.Close() defer f.Close()
if err := json.NewDecoder(f).Decode(&config); err != nil { fileConfig := userConfig{}
return fmt.Errorf("could not decode config file: %v", err) if err := json.NewDecoder(f).Decode(&fileConfig); err == nil {
config = fileConfig
} }
return nil
} }
// A Command is an implementation of a go command // A Command is an implementation of a go command
@ -166,26 +167,24 @@ func main() {
os.Exit(2) os.Exit(2)
} }
cmdName := args[0]
for _, cmd := range commands { for _, cmd := range commands {
if cmd.Name() != args[0] { if cmd.Name() != cmdName {
continue continue
} }
if err := readConfig(); err != nil { readConfig()
log.Printf("Could not load config: %v", err)
log.Printf("Using default config")
}
cmd.Flag.Usage = func() { cmd.Usage() } cmd.Flag.Usage = func() { cmd.Usage() }
cmd.Flag.Parse(args[1:]) cmd.Flag.Parse(args[1:])
args = cmd.Flag.Args() args = cmd.Flag.Args()
if err := cmd.Run(args); err != nil { if err := cmd.Run(args); err != nil {
log.Fatal(err) errExit("%s: %v\n", cmdName, err)
} }
return return
} }
switch args[0] { switch cmdName {
default: default:
log.Printf("Unrecognised command '%s'\n\n", args[0]) fmt.Fprintf(os.Stderr, "Unrecognised command '%s'\n\n", cmdName)
flag.Usage() flag.Usage()
os.Exit(2) os.Exit(2)
} }

Loading…
Cancel
Save