Skip to content

Commit

Permalink
Split logging into two verbosity levels
Browse files Browse the repository at this point in the history
  • Loading branch information
barnardb committed Jan 24, 2021
1 parent 5c41264 commit 9343e24
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ usage: cookies [options…] <URL> [<cookie-name>]
The following options are available:
-a, --accept-missing don't fail with exit status 1 when cookies aren't found
-b, --browser stringArray browser to try extracting a cookie from, can be repeated to try multiple browsers (default [chrome,chromium,firefox,safari])
-v, --verbose enables logging to stderr
-v, --verbose[=level] enables logging to stderr; specify it twice or provide level 2 to get per-cookie details (`-vv` or `--verbose=2`)
```

So you get all cookies for a URL, so e.g. this:
Expand Down
13 changes: 6 additions & 7 deletions find.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"log"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -35,21 +34,21 @@ STORES:
return stores[:n]
}

func findCookies(url *url.URL, name string, browsers []string, logger *log.Logger) (cookies []*kooky.Cookie) {
func findCookies(url *url.URL, name string, browsers []string, logger *Logger) (cookies []*kooky.Cookie) {
logger.Printf("Looking for cookies for URL %s", url)

stores := storesForBrowsers(browsers)
logger.Printf("Found %v cookie stores", len(stores))
logger.Printf("Found %v cookie store(s)", len(stores))

filter := currentlyAppliesToURLAndName(url, name, logger)
filter := currentlyAppliesToURLAndName(url, name, logger.RequireVerbosity(2))
for _, store := range stores {
logger.Printf("Loading cookies from %v", store)
cookies, err := store.ReadCookies(filter)
if err != nil {
logger.Printf("Error loading cookies from %v: %s", store, err)
continue
}
logger.Printf("Found %d matching cookies", len(cookies))
logger.Printf("Found %d matching cookie(s)", len(cookies))

if len(cookies) > 0 {
return cookies
Expand All @@ -59,13 +58,13 @@ func findCookies(url *url.URL, name string, browsers []string, logger *log.Logge
return []*kooky.Cookie{}
}

func currentlyAppliesToURLAndName(url *url.URL, name string, logger *log.Logger) kooky.Filter {
func currentlyAppliesToURLAndName(url *url.URL, name string, logger *Logger) kooky.Filter {
currentTime := time.Now()
logger.Printf("Current time is %v", currentTime)
return appliesToURLAndNameAtTime(url, name, currentTime, logger)
}

func appliesToURLAndNameAtTime(url *url.URL, name string, time time.Time, logger *log.Logger) kooky.Filter {
func appliesToURLAndNameAtTime(url *url.URL, name string, time time.Time, logger *Logger) kooky.Filter {
urlIsNotSecure := url.Scheme != "https"
return func(cookie *kooky.Cookie) bool {
if !hostMatchesDomain(url.Host, cookie.Domain) {
Expand Down
34 changes: 34 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"io"
"os"
)

type Logger struct {
writer io.Writer
verbosity int
}

func LoggerWithVerbosity(verbosity int) *Logger {
if verbosity > 0 {
return &Logger{os.Stderr, verbosity}
}
return nil
}

func (l *Logger) RequireVerbosity(verbosity int) *Logger {
if l != nil && l.verbosity >= verbosity {
return l
}
return nil
}

func (l *Logger) Printf(format string, a ...interface{}) {
if l == nil {
return
}
fmt.Fprintf(l.writer, format, a...)
l.writer.Write([]byte("\n"))
}
19 changes: 5 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"regexp"

flag "github.com/spf13/pflag"
"github.com/zellyn/kooky"
Expand All @@ -17,12 +16,12 @@ type options struct {
url *url.URL
name string
acceptMissing bool
verbose bool
verbosity int
}

func main() {
options := parseCommandLine()
logger := buildLogger(options.verbose)
logger := LoggerWithVerbosity(options.verbosity)

cookies := findCookies(options.url, options.name, options.browsers, logger)
if len(cookies) == 0 {
Expand All @@ -45,7 +44,7 @@ func parseCommandLine() (options options) {

usage := func(output io.Writer) {
fmt.Fprintf(output, "usage: %s [options…] <URL> [<cookie-name>]\n\nThe following options are available:\n", os.Args[0])
fmt.Fprint(output, flagSet.FlagUsages())
fmt.Fprint(output, regexp.MustCompile(`--verbose level `).ReplaceAllString(flagSet.FlagUsages(), `--verbose[=level]`))
}

fatalError := func(error ...interface{}) {
Expand All @@ -58,7 +57,7 @@ func parseCommandLine() (options options) {

flagSet.BoolVarP(&options.acceptMissing, "accept-missing", "a", false, "don't fail with exit status 1 when cookies aren't found")
flagSet.StringArrayVarP(&options.browsers, "browser", "b", []string{"chrome", "chromium", "firefox", "safari"}, "browser to try extracting a cookie from, can be repeated to try multiple browsers")
flagSet.BoolVarP(&options.verbose, "verbose", "v", false, "enables logging to stderr")
flagSet.CountVarP(&options.verbosity, "verbose", "v", "enables logging to stderr; specify it twice or provide `level` 2 to get per-cookie details (`-vv` or `--verbose=2`)")

err := flagSet.Parse(os.Args[1:])
if err != nil {
Expand Down Expand Up @@ -88,14 +87,6 @@ func parseCommandLine() (options options) {
return
}

func buildLogger(verbose bool) *log.Logger {
w := ioutil.Discard
if verbose {
w = os.Stderr
}
return log.New(w, "", 0)
}

func writeStrongestValue(w io.Writer, cookies []*kooky.Cookie) {
strongest := cookies[0]
for _, cookie := range cookies[1:] {
Expand Down

0 comments on commit 9343e24

Please sign in to comment.