Skip to content

Commit

Permalink
switch to new cli pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Jess Frazelle <[email protected]>
  • Loading branch information
jessfraz committed Jul 15, 2018
1 parent 8ccd86c commit f6b3e5f
Show file tree
Hide file tree
Showing 1,169 changed files with 419 additions and 266,966 deletions.
35 changes: 22 additions & 13 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 72 additions & 75 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"context"
"errors"
"flag"
"fmt"
"io"
Expand All @@ -13,22 +15,11 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/genuinetools/apk-file/version"
"github.com/genuinetools/pkg/cli"
"github.com/sirupsen/logrus"
)

const (
// BANNER is what is printed for help/info output
BANNER = ` _ __ _ _
__ _ _ __ | | __ / _(_) | ___
/ _` + "`" + ` | '_ \| |/ /____| |_| | |/ _ \
| (_| | |_) | <_____| _| | | __/
\__,_| .__/|_|\_\ |_| |_|_|\___|
|_|
Search apk package contents via the command line.
Version: %s
`
alpineContentsSearchURI = "https://pkgs.alpinelinux.org/contents"
)

Expand All @@ -41,89 +32,95 @@ var (
repo string

debug bool
vrsn bool

validArches = []string{"x86", "x86_64", "armhf"}
validRepos = []string{"main", "community", "testing"}
)

func init() {
// Parse flags
flag.StringVar(&arch, "arch", "", "arch to search for ("+strings.Join(validArches, ", ")+")")
flag.StringVar(&repo, "repo", "", "repository to search in ("+strings.Join(validRepos, ", ")+")")

flag.BoolVar(&vrsn, "version", false, "print version and exit")
flag.BoolVar(&vrsn, "v", false, "print version and exit (shorthand)")
flag.BoolVar(&debug, "d", false, "run in debug mode")
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "apk-file"
p.Description = "Search apk package contents via the command line"

// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION

// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.StringVar(&arch, "arch", "", "arch to search for ("+strings.Join(validArches, ", ")+")")
p.FlagSet.StringVar(&repo, "repo", "", "repository to search in ("+strings.Join(validRepos, ", ")+")")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")

// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}

flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(BANNER, version.VERSION))
flag.PrintDefaults()
}
if arch != "" && !stringInSlice(arch, validArches) {
return fmt.Errorf("%s is not a valid arch", arch)
}

flag.Parse()
if repo != "" && !stringInSlice(repo, validRepos) {
return fmt.Errorf("%s is not a valid repo", repo)
}

if vrsn {
fmt.Printf("apk-file version %s, build %s", version.VERSION, version.GITCOMMIT)
os.Exit(0)
return nil
}

// Set log level
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
// Set the main program action.
p.Action = func(ctx context.Context) error {
if p.FlagSet.NArg() < 1 {
return errors.New("must pass a file to search for")
}

if arch != "" && !stringInSlice(arch, validArches) {
logrus.Fatalf("%s is not a valid arch", arch)
}
f, p := getFileAndPath(p.FlagSet.Arg(0))

if repo != "" && !stringInSlice(repo, validRepos) {
logrus.Fatalf("%s is not a valid repo", repo)
}
}

func main() {
if flag.NArg() < 1 {
logrus.Fatal("must pass a file to search for.")
}
query := url.Values{
"file": {f},
"path": {p},
"branch": {""},
"repo": {repo},
"arch": {arch},
}

f, p := getFileAndPath(flag.Arg(0))
uri := fmt.Sprintf("%s?%s", alpineContentsSearchURI, query.Encode())
logrus.Debugf("requesting from %s", uri)
resp, err := http.Get(uri)
if err != nil {
logrus.Fatalf("requesting %s failed: %v", uri, err)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
logrus.Fatalf("creating document failed: %v", err)
}

query := url.Values{
"file": {f},
"path": {p},
"branch": {""},
"repo": {repo},
"arch": {arch},
}
// create the writer
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
io.WriteString(w, "FILE\tPACKAGE\tBRANCH\tREPOSITORY\tARCHITECTURE\n")

uri := fmt.Sprintf("%s?%s", alpineContentsSearchURI, query.Encode())
resp, err := http.Get(uri)
if err != nil {
logrus.Fatalf("requesting %s failed: %v", uri, err)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
logrus.Fatalf("creating document failed: %v", err)
}
files := getFilesInfo(doc)

// create the writer
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
io.WriteString(w, "FILE\tPACKAGE\tBRANCH\tREPOSITORY\tARCHITECTURE\n")
for _, f := range files {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
f.path,
f.pkg,
f.branch,
f.repo,
f.arch)
}

files := getFilesInfo(doc)
w.Flush()

for _, f := range files {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
f.path,
f.pkg,
f.branch,
f.repo,
f.arch)
return nil
}

w.Flush()
// Run our program.
p.Run()
}

func getFilesInfo(d *goquery.Document) []fileInfo {
Expand Down
1 change: 0 additions & 1 deletion vendor/github.com/PuerkitoBio/goquery/.gitattributes

This file was deleted.

16 changes: 0 additions & 16 deletions vendor/github.com/PuerkitoBio/goquery/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions vendor/github.com/PuerkitoBio/goquery/.travis.yml

This file was deleted.

Loading

0 comments on commit f6b3e5f

Please sign in to comment.