-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
97 lines (85 loc) · 2.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// untrack-url
// Copyright (C) 2016-2017 Vladimir Bauer
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"os"
"runtime"
"github.com/pkg/errors"
"github.com/skratchdot/open-golang/open"
"github.com/vbauerster/untrack-url/ranger"
)
const (
projectHome = "https://github.com/vbauerster/untrack-url"
cmdName = "untrack-url"
)
var (
version = "devel"
// Command line flags.
printOnly bool
debug bool
showVersion bool
// FlagSet
cmd *flag.FlagSet
)
func init() {
// NewFlagSet for the sake of cmd.SetOutput
cmd = flag.NewFlagSet(cmdName, flag.ExitOnError)
cmd.SetOutput(os.Stdout)
cmd.BoolVar(&printOnly, "p", false, "print only: don't open URL in browser")
cmd.BoolVar(&debug, "d", false, "debug: print debug info, implies -p")
cmd.BoolVar(&showVersion, "v", false, "print version number")
cmd.Usage = func() {
fmt.Printf("Usage: %s [OPTIONS] URL\n\n", cmdName)
fmt.Println("OPTIONS:")
cmd.PrintDefaults()
fmt.Println()
fmt.Println("Known trackers:")
fmt.Println()
for _, host := range ranger.KnownTrackers() {
fmt.Printf("\t%s\n", host)
}
fmt.Println()
fmt.Println("Known shops:")
fmt.Println()
for _, host := range ranger.KnownShops() {
fmt.Printf("\t%s\n", host)
}
fmt.Println()
fmt.Printf("Project home: %s\n", projectHome)
}
}
func main() {
cmd.Parse(os.Args[1:])
if showVersion {
fmt.Printf("%s: %s (runtime: %s)\n", cmdName, version, runtime.Version())
os.Exit(0)
}
if cmd.NArg() != 1 {
cmd.Usage()
os.Exit(2)
}
ranger.Debug = debug
cleanURL, err := ranger.Untrack(cmd.Arg(0))
if err != nil {
if _, ok := errors.Cause(err).(ranger.UntrackErr); ok {
if debug {
fmt.Fprintf(os.Stderr, "%+v\n", err)
} else {
fmt.Fprintln(os.Stderr, err)
}
} else {
fmt.Fprintf(os.Stderr, "%+v\n", err)
fmt.Println("There was an unexpected error; please report this as a bug.")
}
os.Exit(1)
}
if printOnly || debug {
fmt.Println(cleanURL)
} else if err := open.Start(cleanURL); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}