Skip to content

Commit

Permalink
removes New and using Init
Browse files Browse the repository at this point in the history
this removes the factory for an init function which is clearer, since
the app structure is filled in several places.
  • Loading branch information
leucos committed Apr 24, 2022
1 parent 5c8589e commit ba31849
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
10 changes: 5 additions & 5 deletions SYSTEM.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ wget -q https://github.com/devops-works/binenv/releases/latest/download/checksum
sha256sum --check --ignore-missing checksums.txt
chmod +x ./binenv_linux_amd64
sudo ./binenv_linux_amd64 -g update
sudo ./binenv_linux_amd64 -g install binenv
sudo ./binenv_linux_amd64 -g install binenv 0.19.0-rc4
rm ./binenv_linux_amd64
if [[ -n $BASH ]]; then ZESHELL=bash; fi
if [[ -n $ZSH_NAME ]]; then ZESHELL=zsh; fi
Expand All @@ -60,7 +60,7 @@ wget -q https://github.com/devops-works/binenv/releases/latest/download/checksum
sha256sum --check --ignore-missing checksums.txt
chmod +x binenv_darwin_amd64
sudo ./binenv_darwin_amd64 -g update
sudo ./binenv_darwin_amd64 -g install binenv
sudo ./binenv_darwin_amd64 -g install binenv 0.19.0-rc4
rm ./binenv_darwin_amd64
echo 'source <(binenv completion bash)' >> ~/.bashrc
exec $SHELL
Expand All @@ -78,7 +78,7 @@ fetch https://github.com/devops-works/binenv/releases/latest/download/checksums.
shasum --ignore-missing -a 512 -c checksums.txt
chmod +x binenv_freebsd_amd64
sudo ./binenv_freebsd_amd64 -g update
sudo ./binenv_freebsd_amd64 -g install binenv
sudo ./binenv_freebsd_amd64 -g install binenv 0.19.0-rc4
rm ./binenv_freebsd_amd64
if [[ -n $BASH ]]; then ZESHELL=bash; fi
if [[ -n $ZSH_NAME ]]; then ZESHELL=zsh; fi
Expand All @@ -99,7 +99,7 @@ ftp https://github.com/devops-works/binenv/releases/latest/download/checksums.tx
cksum -a sha256 -C checksums.txt binenv_openbsd_amd64
chmod +x binenv_openbsd_amd64
sudo ./binenv_openbsd_amd64 -g update
sudo ./binenv_openbsd_amd64 -g install binenv
sudo ./binenv_openbsd_amd64 -g install binenv 0.19.0-rc4
rm ./binenv_openbsd_amd64
if [[ -n $BASH ]]; then ZESHELL=bash; fi
if [[ -n $ZSH_NAME ]]; then ZESHELL=zsh; fi
Expand All @@ -121,7 +121,7 @@ binenv version
- install `binenv` system-wide

```bash
binenv -g install binenv
binenv -g install binenv 0.19.0-rc4
```

- adjust path in your rcfiles according to your preferences
Expand Down
15 changes: 8 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@ func RootCmd() *cobra.Command {
var (
bindir, linkdir, cachedir, confdir string
global, verbose bool
a *app.App
)

a, err := app.New()
if err != nil {
fmt.Printf("got error %v\n", err)
panic(err)
}

rootCmd := &cobra.Command{
Use: "binenv",
Short: "Install binary distributions easily",
Expand Down Expand Up @@ -65,8 +60,12 @@ selected.`,
a.SetCacheDir(cachedir)
}

a.DumpConfig()
a.Init()

if err != nil {
fmt.Printf("got error %v\n", err)
panic(err)
}
return err
},
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -76,6 +75,8 @@ selected.`,
},
}

a = &app.App{}

rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose operation [BINENV_VERBOSE]")
rootCmd.PersistentFlags().BoolVarP(&global, "global", "g", false, "global mode [BINENV_GLOBAL]")

Expand Down
34 changes: 13 additions & 21 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,17 @@ var (
ErrAlreadyInstalled = errors.New("version already installed")
)

// New create a new app instance
func New(o ...func(*App) error) (*App, error) {
bindir := GetDefaultBinDir()
cachedir := GetDefaultCacheDir()
configdir := GetDefaultConfDir()

a := &App{
mappers: make(map[string]mapping.Remapper),
installers: make(map[string]install.Installer),
listers: make(map[string]list.Lister),
fetchers: make(map[string]fetch.Fetcher),
cache: make(map[string][]string),
bindir: bindir,
linkdir: bindir,
cachedir: cachedir,
configdir: configdir,
logger: zerolog.New(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
}).With().Timestamp().Logger(),
}
// Init prepares App for use
func (a *App) Init(o ...func(*App) error) (*App, error) {
a.mappers = make(map[string]mapping.Remapper)
a.installers = make(map[string]install.Installer)
a.listers = make(map[string]list.Lister)
a.fetchers = make(map[string]fetch.Fetcher)
a.cache = make(map[string][]string)
a.logger = zerolog.New(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
}).With().Timestamp().Logger()

// Default to warn log level
a.logger = a.logger.Level(zerolog.InfoLevel)
Expand All @@ -110,6 +100,8 @@ func New(o ...func(*App) error) (*App, error) {
}
}

a.DumpConfig()

err := a.readDistributions()
if err != nil {
a.logger.Error().Err(err).Msgf("unable to read distributions")
Expand Down

0 comments on commit ba31849

Please sign in to comment.