Skip to content

Commit

Permalink
Add support to install requred and optional tools (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Sep 2, 2021
1 parent de9d6c4 commit 322227d
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pkg/cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cmd

import (
"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/spf13/cobra"
"runtime"
)

type initOption struct {
require, optional, fetch bool

// inner fields
requireTools, optionalTools map[string]string
}

// NewInitCommand returns a command for init
func NewInitCommand(requireTools, optionalTools map[string]string) (cmd *cobra.Command) {
opt := &initOption{
requireTools: requireTools,
optionalTools: optionalTools,
}

cmd = &cobra.Command{
Use: "init",
Short: "Init your command",
RunE: opt.runE,
}

flags := cmd.Flags()
flags.BoolVarP(&opt.require, "require", "r", true,
"Indicate if you want to install required tools")
flags.BoolVarP(&opt.optional, "optional", "o", false,
"Indicate if you want to install optional tools")
flags.BoolVarP(&opt.fetch, "fetch", "", true,
"Indicate if fetch the latest config of tools")
return
}

func (o *initOption) runE(_ *cobra.Command, _ []string) (err error) {
is := installer.Installer{
Provider: "github",
OS: runtime.GOOS,
Arch: runtime.GOARCH,
Fetch: o.fetch,
}
if o.require {
err = is.CheckDepAndInstall(o.requireTools)
}
if err == nil && o.optional {
err = is.CheckDepAndInstall(o.optionalTools)
}
return
}

0 comments on commit 322227d

Please sign in to comment.