github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/update/internal/installer/main.go (about) 1 // installer with reexecute functionality: 2 // after downloading and installing newest version, it reexecutes the command 3 package main 4 5 import ( 6 "fmt" 7 "os" 8 "os/exec" 9 "runtime" 10 "syscall" 11 12 "github.com/equinox-io/equinox" 13 "github.com/henvic/uilive" 14 "github.com/henvic/wedeploycli/update/keys" 15 ) 16 17 // EnvSkipReexec environment variable option for skipping to re-execute command 18 const EnvSkipReexec = "WEDEPLOY_INSTALLER_SKIP_REEXEC" 19 20 // EnvVerbose option to print verbose info about the program 21 const EnvVerbose = "VERBOSE" 22 23 var ( 24 // Version of the Liferay Cloud CLI tool 25 Version = "installer" 26 27 // Build commit 28 Build = "" 29 30 // BuildTime is the time when the build was generated 31 BuildTime = "" 32 ) 33 34 type installation struct { 35 ws *uilive.Writer 36 } 37 38 func check(channel string) (*equinox.Response, error) { 39 var opts equinox.Options 40 opts.Channel = channel 41 42 if err := opts.SetPublicKeyPEM(keys.PublicKey); err != nil { 43 return nil, err 44 } 45 46 resp, err := equinox.Check(keys.AppID, opts) 47 48 return &resp, err 49 } 50 51 func (i *installation) install() error { 52 var resp, err = check("stable") 53 54 if err != nil { 55 return err 56 } 57 58 _, _ = fmt.Fprintln(i.ws, "Downloading newest Liferay Cloud Platform CLI. Please wait.") 59 _ = i.ws.Flush() 60 return resp.Apply() 61 } 62 63 func (i *installation) run() error { 64 i.ws = uilive.New() 65 _, _ = fmt.Fprintln(i.ws, "Installing Liferay Cloud Platform CLI for the first time. Please wait.") 66 _ = i.ws.Flush() 67 68 if err := i.install(); err != nil { 69 return err 70 } 71 72 _, _ = fmt.Fprint(i.ws, "Liferay Cloud Platform CLI tool installed. Thank you.\n\n") 73 _ = i.ws.Flush() 74 return nil 75 } 76 77 func reexecute() { 78 var cmd = exec.Command(os.Args[0], os.Args[1:]...) // #nosec 79 cmd.Env = os.Environ() 80 cmd.Dir, _ = os.Getwd() 81 cmd.Stdin = os.Stdin 82 cmd.Stderr = os.Stderr 83 cmd.Stdout = os.Stdout 84 err := cmd.Run() 85 86 if err == nil { 87 return 88 } 89 90 if exit, ok := err.(*exec.ExitError); ok { 91 if process, ok := exit.Sys().(syscall.WaitStatus); ok { 92 os.Exit(process.ExitStatus()) 93 } 94 } 95 96 _, _ = fmt.Fprintln(os.Stderr, "An unexpected error happened, please report it to https://help.liferay.com/hc/en-us/categories/360000813091-Liferay-DXP-Cloud-Documentation") 97 panic(err) 98 } 99 100 func main() { 101 if _, ok := os.LookupEnv(EnvVerbose); ok { 102 printInstallerVersion() 103 } 104 105 var i = installation{} 106 107 if err := i.run(); err != nil { 108 _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) 109 os.Exit(1) 110 } 111 112 if _, ok := os.LookupEnv(EnvSkipReexec); !ok { 113 reexecute() 114 } 115 } 116 117 func printInstallerVersion() { 118 var os = runtime.GOOS 119 var arch = runtime.GOARCH 120 fmt.Printf("Liferay Cloud Platform CLI Installer version %s %s/%s\n", 121 Version, 122 os, 123 arch) 124 125 if Build != "" { 126 fmt.Printf("Liferay Cloud Platform CLI Installer Build commit: %v\n", Build) 127 } 128 129 if BuildTime != "" { 130 fmt.Printf("Liferay Cloud Platform CLI Installer Build time: %v\n", BuildTime) 131 } 132 133 fmt.Printf("Liferay Cloud Platform CLI Installer Go version: %s\n", runtime.Version()) 134 }