Skip to content

Commit

Permalink
Merge pull request #23 from WillAbides/breakupmain
Browse files Browse the repository at this point in the history
break up main into separate files
  • Loading branch information
WillAbides authored Nov 25, 2019
2 parents 9a1dfff + fbafba7 commit 28de755
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 73 deletions.
32 changes: 32 additions & 0 deletions cmd/bindownloader/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"encoding/json"
"io/ioutil"

"github.com/alecthomas/kong"
"github.com/willabides/bindownloader"
)

var configKongVars = kong.Vars{
"config_format_help": `formats the config file`,
"config_checksums_help": `update checksums in the config file`,
}

type configCmd struct {
Format configFmtCmd `kong:"cmd,help=${config_format_help}"`
}

type configFmtCmd struct{}

func (c configFmtCmd) Run() error {
config, err := bindownloader.LoadConfigFile(cli.Configfile)
if err != nil {
return err
}
b, err := json.MarshalIndent(&config, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(cli.Configfile, b, 0600)
}
52 changes: 52 additions & 0 deletions cmd/bindownloader/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"path"
"runtime"

"github.com/alecthomas/kong"
"github.com/willabides/bindownloader"
)

var downloadKongVars = kong.Vars{
"download_arch_help": `download for this architecture`,
"download_arch_default": runtime.GOARCH,
"download_os_help": `download for this operating system`,
"download_os_default": runtime.GOOS,
"download_force_help": `force download even if it already exists`,
"download_target_file_help": `file to download`,
}

type downloadCmd struct {
Arch string `kong:"help=${download_arch_help},default=${download_arch_default}"`
OS string `kong:"help=${download_os_help},default=${download_os_default}"`
Force bool `kong:"help=${download_force_help}"`
TargetFile string `kong:"required=true,arg,help=${download_target_file_help}"`
}

func (d *downloadCmd) Run(*kong.Context) error {
config, err := bindownloader.LoadConfigFile(cli.Configfile)
if err != nil {
return fmt.Errorf("error loading config from %q", cli.Configfile)
}
binary := path.Base(d.TargetFile)
binDir := path.Dir(d.TargetFile)

downloader := config.Downloader(binary, d.OS, d.Arch)
if downloader == nil {
return fmt.Errorf(`no downloader configured for:
bin: %s
os: %s
arch: %s`, binary, d.OS, d.Arch)
}

installOpts := bindownloader.InstallOpts{
DownloaderName: binary,
TargetDir: binDir,
Force: d.Force,
CellarDir: cli.CellarDir,
}

return downloader.Install(installOpts)
}
80 changes: 7 additions & 73 deletions cmd/bindownloader/main.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"

"github.com/alecthomas/kong"
"github.com/willabides/bindownloader"
)

var kongVars = kong.Vars{
"arch_help": `download for this architecture`,
"arch_default": runtime.GOARCH,
"os_help": `download for this operating system`,
"os_default": runtime.GOOS,
"configfile_help": `file with tool definitions`,
"configfile_default": `buildtools.json`,
"force_help": `force download even if it already exists`,
"cellar_dir_help": `directory where downloads will be cached`,
"config_format_help": `formats the config file`,
"download_help": `download a bin`,
}

var version = "unknown"

var cli struct {
Version versionCmd `kong:"cmd"`
Download downloadCmd `kong:"cmd,help=${download_help}"`
Expand All @@ -35,66 +21,14 @@ var cli struct {
CellarDir string `kong:"type=path,help=${cellar_dir_help}"`
}

type versionCmd struct{}

func (*versionCmd) Run(k *kong.Context) error {
k.Printf("version %s", version)
return nil
}

type downloadCmd struct {
Arch string `kong:"help=${arch_help},default=${arch_default}"`
OS string `kong:"help=${os_help},default=${os_default}"`
Force bool `kong:"help=${force_help}"`
TargetFile string `kong:"required=true,arg,help='file to download'"`
}

func (d *downloadCmd) Run(*kong.Context) error {
config, err := bindownloader.LoadConfigFile(cli.Configfile)
if err != nil {
return fmt.Errorf("error loading config from %q", cli.Configfile)
}
binary := path.Base(d.TargetFile)
binDir := path.Dir(d.TargetFile)

downloader := config.Downloader(binary, d.OS, d.Arch)
if downloader == nil {
return fmt.Errorf(`no downloader configured for:
bin: %s
os: %s
arch: %s`, binary, d.OS, d.Arch)
}

installOpts := bindownloader.InstallOpts{
DownloaderName: binary,
TargetDir: binDir,
Force: d.Force,
CellarDir: cli.CellarDir,
}

return downloader.Install(installOpts)
}

type configCmd struct {
Format configFmtCmd `kong:"cmd,help=${config_format_help}"`
}

type configFmtCmd struct{}

func (c configFmtCmd) Run() error {
config, err := bindownloader.LoadConfigFile(cli.Configfile)
if err != nil {
return err
}
b, err := json.MarshalIndent(&config, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(cli.Configfile, b, 0600)
}

func main() {
parser := kong.Must(&cli, kongVars, kong.UsageOnError())
parser := kong.Must(
&cli,
kongVars,
downloadKongVars,
configKongVars,
kong.UsageOnError(),
)

kongCtx, err := parser.Parse(os.Args[1:])
parser.FatalIfErrorf(err)
Expand Down
14 changes: 14 additions & 0 deletions cmd/bindownloader/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"github.com/alecthomas/kong"
)

var version = "unknown"

type versionCmd struct{}

func (*versionCmd) Run(k *kong.Context) error {
k.Printf("version %s", version)
return nil
}

0 comments on commit 28de755

Please sign in to comment.