Code Monkey home page Code Monkey logo

gopkgs's Introduction

gopkgs - List Go packages FAST by using the same implementation as goimports

Travis Build Status Appveyor Build status Releases LICENSE GoDoc

gopkgs outputs list of importable Go packages.

By using the same implementation as goimports, it's faster than go list ... and it also has -f option.

gopkgs cares .goimportsignore which was introduced by golang/go#16386 since it uses the same implementation as goimports.

gopkgs_usage.gif (890ร—542) Sample usage of gopkgs with other tools like godoc and filtering tools (peco).

Installation

Install Binary from GitHub Releases

https://github.com/haya14busa/gopkgs/releases

go get

go get -u github.com/haya14busa/gopkgs/cmd/gopkgs

SYNOPSIS

$ gopkgs -h
Usage of gopkgs:
  -f string
        alternate format for the output using the syntax of template package. e.g. {{.Name}};{{ImportPathShort}}
  -fullpath
        output absolute file path to package directory. ("/usr/lib/go/src/net/http")
  -include-name
        fill Pkg.Name which can be used with -f flag
  -short
        output vendorless import path ("net/http", "foo/bar/vendor/a/b") (default true)


Use -f to custom the output using template syntax. The struct being passed to template is:
    type Pkg struct {
        Dir             string // absolute file path to Pkg directory ("/usr/lib/go/src/net/http")
        ImportPath      string // full Pkg import path ("net/http", "foo/bar/vendor/a/b")
        ImportPathShort string // vendorless import path ("net/http", "a/b")

        // It can be empty. It's filled only when -include-name flag is true.
        Name string // package name ("http")
    }

Vim

gopkgs_vim.gif (890ร—542)

Sample usage of gopkgs in Vim: open godoc and import with vim-go and fzf

augroup gopkgs
  autocmd!
  autocmd FileType go command! -buffer Import exe 'GoImport' fzf#run({'source': 'gopkgs'})[0]
  autocmd FileType go command! -buffer Doc exe 'GoDoc' fzf#run({'source': 'gopkgs'})[0]
augroup END

Above Vim script is just a sample and isn't robust. I'm planning to create or contribute Vim plugins to include the same feature.

LICENSE

LICENSE

under x/tools/imports/ directory

Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
   * Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

gopkgs copied and modified https://godoc.org/golang.org/x/tools/imports to export some interface in accordance with LICENSE.

If The Go Authors provide goimports's directory scanning code as a library, I plan to use it. ref: golang/go#16427

๐Ÿฆ Author

haya14busa (https://github.com/haya14busa)

gopkgs's People

Contributors

haya14busa avatar mattn avatar rhysd avatar uudashr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gopkgs's Issues

Output lines are not unique

In my environment, some packages are duplicate.
For example:

> gopkgs | grep isatty
github.com/mattn/go-isatty
github.com/junegunn/go-isatty
github.com/mattn/go-isatty
github.com/mattn/go-isatty

It seems that vendored packages are shown multiple times even if -short or no option.

> cd ~/.go/src
> fd go-isatty
github.com/mattn/go-isatty
github.com/junegunn/go-isatty
github.com/derekparker/delve/vendor/github.com/mattn/go-isatty
github.com/fatih/color/vendor/github.com/mattn/go-isatty
  • OS: macOS 10.12
  • Go: 1.10

Gopkgs stops working if Go gets upgraded

Every couple of months I notice that gopkgs stops listing Go's packages such as "time", "os", "net/http" and others.

I think that I was able to pinpoint why that thing happens. Inside the codebase, there are usages of the build.Default.GOROOT (from go/build package):

isGoroot := srcDir == filepath.Join(build.Default.GOROOT, "src")

If the host machine does not have the GOROOT env. variable set (it's not a requirement to be set), then it uses values that are written to the binary file itself. Those values may be different if compiled on different host machines or if you use homebrew and upgrade the Go to a different version.

To test this theory I've written a small program that displays the build's default context.

package main

import "fmt"
import "go/build"

func main() {
	fmt.Printf("%+v\n", build.Default)
}

I've built the same program on two different hosts with two different Go versions installed. One is the Linux machine (inside of a docker container) and the other one is my host machine which is the macOS 10.13.3.

I've built the from_linux binary inside of a Docker container for the macOS, then I run it from the macOS. I've also built the same binary on the host machine itself.

Running both binaries locally, it's visible that GOROOT is different.

Screen Shot 2019-07-01 at 14 42 50

The same thing happens when you are using homebrew to upgrade go. If you built the gopkgs on version Go version 1.11.11 and then upgrade Go to version 1.12.6, it will not list any of those Go related packages because the folder does not exist anymore. It was deleted by the homebrew. This is what happens to me every few months.


And of course, setting the GOROOT env variable fixes the problem:

Screen Shot 2019-07-01 at 14 45 24

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.