/
install.go
74 lines (61 loc) · 1.94 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cmd
import (
"fmt"
"github.com/AthulMuralidhar/protobuff-installer/pkg/cmd/downloader"
"github.com/AthulMuralidhar/protobuff-installer/pkg/cmd/semvar"
"github.com/AthulMuralidhar/protobuff-installer/pkg/cmd/unzip"
"github.com/spf13/cobra"
"go.uber.org/zap"
"log"
"os"
)
// installCmd represents the new command
var installCmd = &cobra.Command{
Use: "install",
Short: "main command the the install functionality",
Long: `main command the the install functionality`,
Run: func(cmd *cobra.Command, args []string) {
protocInstall()
},
}
func init() {
rootCmd.AddCommand(installCmd)
}
func protocInstall() {
log.Println("protocInstall called")
installProtoc := promptGetProtocInput(protocPrompt)
if !installProtoc {
log.Println("protocInstall: user does not want to install protoc, exiting...")
return
}
sm := promptGetProtocVersion(versionPrompt)
installPath := promptGetInstallPath(installPathPrompt)
if err := protocInstaller(sm, installPath); err != nil {
log.Fatalln(err)
}
}
func protocInstaller(sm semvar.SemVar, installPath string) error {
// https://github.com/protocolbuffers/protobuf/releases/download/v24.2/protoc-24.2-linux-aarch_64.zip
// https://github.com/protocolbuffers/protobuf/releases/download/v24.2/protoc-24.2-linux-x86_64.zip
logger, _ := zap.NewProduction()
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
url := sm.ToProtocURL()
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("err during getcwd call: %s", err)
}
f, err := downloader.DownloadAndCreateFile(sugar, url, cwd)
defer os.Remove(f.Name())
// basically this: unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local
err = os.Mkdir("protoc", 0777)
if err != nil {
return fmt.Errorf("error during making protoc directory: %w", err)
}
err = unzip.UnzipToDir(f.Name(), cwd+"protoc")
if err != nil {
return fmt.Errorf("error during unzipping: %w", err)
}
sugar.Info("protocInstall: done")
return nil
}