From 322227d0831d81ef1caaac4cc219c888ddc0c54e Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Thu, 2 Sep 2021 22:12:30 +0800 Subject: [PATCH] Add support to install requred and optional tools (#117) --- pkg/cmd/init.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 pkg/cmd/init.go diff --git a/pkg/cmd/init.go b/pkg/cmd/init.go new file mode 100644 index 0000000..b22805a --- /dev/null +++ b/pkg/cmd/init.go @@ -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 +}