Skip to content

Commit

Permalink
golint FTW
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Ermolenko committed May 16, 2019
1 parent 393fbb8 commit 16a9a8e
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 135 deletions.
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
language: go

go:
- 1.11.x

env:
global:
- GO111MODULE=on

go:
- 1.11.x

git:
depth: 1

branches:
only:
- master
38 changes: 19 additions & 19 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,51 @@ had links created for them.
Run: runAdd,
}

var skipFetchingTitle bool = false
var allowDuplicates bool = false
var targetList string = "default"
var providedTitle string = ""
var skipFetchingTitle = false
var allowDuplicates = false
var targetList = "default"
var providedTitle = ""

func runAdd(cmd *cobra.Command, args []string) {
if store, err := links.OpenStore(dataPath); err == nil {
for _, rawurl := range args {
saveRawUrl(store, rawurl)
saveRawURL(store, rawurl)
}
} else {
die("Unable to open links store", err)
}
}

func saveRawUrl(store links.Store, rawurl string) {
url := parseUrl(rawurl)
func saveRawURL(store links.Store, rawurl string) {
url := parseURL(rawurl)
if allowDuplicates || !urlExists(store, url) {
saveUrl(store, url)
saveURL(store, url)
} else {
fmt.Printf("URL %s already exists, skipping\n", rawurl)
}
}

func parseUrl(rawurl string) *url.URL {
if url, err := urls.ParseUrl(rawurl); err == nil {
func parseURL(rawurl string) *url.URL {
url, err := urls.ParseURL(rawurl)
if err == nil {
return url
} else {
die("Unable to add URL", err)
}

die("Unable to add URL", err)
panic("shouldn't get here")
}

func urlExists(store links.Store, url *url.URL) bool {
if exists, err := store.LinkExists(url); err == nil {
exists, err := store.LinkExists(url)
if err == nil {
return exists
} else {
die("Unexpected error", err)
}

die("Unexpected error", err)
panic("shouldn't get there")
}

func saveUrl(store links.Store, url *url.URL) {
func saveURL(store links.Store, url *url.URL) {
source := getSource(url)
title := fetchTitle(url)
link := store.NewLink(url, source, title, targetList)
Expand All @@ -86,12 +86,12 @@ func saveUrl(store links.Store, url *url.URL) {
}

func getSource(url *url.URL) string {
if source, err := urls.GetSource(url); err == nil {
source, err := urls.GetSource(url)
if err == nil {
return source
} else {
die("Mailformed URL", err)
}

die("Mailformed URL", err)
panic("shouldn't get here")
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ linkman archive id
}

func openStore(path string) links.Store {
if store, err := links.OpenStore(path); err == nil {
store, err := links.OpenStore(path)
if err == nil {
return store
} else {
die("Unable to open store", err)
}

die("Unable to open store", err)
panic("shouldn't get there")
}

func archiveLink(store links.Store, id int) {
if err := store.ArchiveById(id); err != nil {
if err := store.ArchiveByID(id); err != nil {
die("Unable to archive link", err)
}
}
Expand Down
42 changes: 21 additions & 21 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ list of "id: source" lines
Run: runList,
}

const DEFAULT_TEMPLATE = `
const defaultTemplate = `
ID: {{.ID}}
Source: {{.Source}}
Title: {{.Title}}
URL: {{.URL}}
`

var format string = DEFAULT_TEMPLATE
var source string = ""
var list string = ""
var title string = ""
var format = defaultTemplate
var source = ""
var list = ""
var title = ""

var requireTitle bool = false
var archived bool = false
var onlyArchived bool = false
var requireTitle = false
var archived = false
var onlyArchived = false

func runList(cmd *cobra.Command, args []string) {
writer := getOutputWriter()
Expand All @@ -83,12 +83,12 @@ func runList(cmd *cobra.Command, args []string) {
}

func openLinksStore(path string) links.Store {
if store, err := links.OpenStore(path); err == nil {
store, err := links.OpenStore(path)
if err == nil {
return store
} else {
die("Unable to open links store", err)
}

die("Unable to open links store", err)
panic("Shouldn't get there")
}

Expand Down Expand Up @@ -125,38 +125,38 @@ func getLinks(store links.Store) []links.Link {

filter := links.NewFilter(conds...)

if links, err := store.FindLinks(filter); err == nil {
links, err := store.FindLinks(filter)
if err == nil {
return links
} else {
die("Unable to fetch links", err)
}

die("Unable to fetch links", err)
panic("Shouldn't get there")
}

func getOutputTemplate() *template.Template {
tpl := template.New("output template")
format := unescapeOutputTemplate(format)

if tpl, err := tpl.Parse(format); err == nil {
tpl, err := tpl.Parse(format)
if err == nil {
return tpl
} else {
die("Unable to parse output template", err)
}

die("Unable to parse output template", err)
panic("Shouldn't get there")
}

func unescapeOutputTemplate(format string) string {
quoted := strconv.Quote(format)
replaced := strings.Replace(quoted, `\\`, "\\", -1)

if result, err := strconv.Unquote(replaced); err == nil {
result, err := strconv.Unquote(replaced)
if err == nil {
return result
} else {
die("Unable to parse output template", err)
}

die("Unable to parse output template", err)
panic("Shouldn't get there")
}

Expand All @@ -174,7 +174,7 @@ func init() {

listCmd.Flags().StringVarP(&format,
"format", "f",
DEFAULT_TEMPLATE,
defaultTemplate,
"Output template. Available fields are: ID, URL, Source, Title,List")

listCmd.Flags().StringVarP(&source,
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Examples on how source is calculated:

var dataPath string

//Execute is the entry point into the application.
//It configures and starts the execute of root command
//which in turn passes the execution to underying commands.
func Execute(path string, args []string) {
dataPath = path
rootCmd.SetArgs(args)
Expand Down
25 changes: 17 additions & 8 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@ import (
"github.com/adrg/xdg"
)

//EnsureDataHome make sture that directory dir exists at
//location XDG_DATA_HOME.
func EnsureDataHome(dir string) error {
path := xdg.DataHome + dir
if err := os.MkdirAll(path, 0700); err == nil {
return nil
} else {

err := os.MkdirAll(path, 0700)
if err != nil {
return fmt.Errorf("Unable to create data directory: %s", err)
}

return nil
}

//GetFilePath returns path to the file in directory dir
//that is located in XDG_DATA_HOME.
func GetFilePath(dir string, file string) (string, error) {
if path, err := xdg.DataFile(dir + "/" + file); err == nil {
return path, nil
} else {
return "", fmt.Errorf("Unable for find path to file [%s/%s]: %s",
dir, file, err)
path, err := xdg.DataFile(dir + "/" + file)
if err != nil {
return "", fmt.Errorf(
"Unable for find path to file [%s/%s]: %s",
dir, file, err,
)
}

return path, nil
}
8 changes: 5 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"github.com/asdine/storm"
)

//Open tries open database located at specified path.
func Open(path string) (*storm.DB, error) {
if db, err := storm.Open(path); err == nil {
return db, nil
} else {
db, err := storm.Open(path)
if err != nil {
return nil, fmt.Errorf("Unable to open database: %s", err)
}

return db, nil
}
Loading

0 comments on commit 16a9a8e

Please sign in to comment.