7
7
"context"
8
8
"flag"
9
9
"fmt"
10
+ "io"
11
+ "log"
10
12
"os"
11
13
"runtime"
12
14
"strings"
@@ -37,8 +39,10 @@ Usage: hc-install install [options] -version <version> <product>
37
39
This command installs a HashiCorp product.
38
40
Options:
39
41
-version [REQUIRED] Version of product to install.
40
- -path Path to directory where the product will be installed. Defaults
41
- to current working directory.
42
+ -path Path to directory where the product will be installed.
43
+ Defaults to current working directory.
44
+ -log-file Path to file where logs will be written. /dev/stdout
45
+ or /dev/stderr can be used to log to STDOUT/STDERR.
42
46
`
43
47
return strings .TrimSpace (helpText )
44
48
}
@@ -47,12 +51,14 @@ func (c *InstallCommand) Run(args []string) int {
47
51
var (
48
52
version string
49
53
installDirPath string
54
+ logFilePath string
50
55
)
51
56
52
57
fs := flag .NewFlagSet ("install" , flag .ExitOnError )
53
58
fs .Usage = func () { c .Ui .Output (c .Help ()) }
54
59
fs .StringVar (& version , "version" , "" , "version of product to install" )
55
60
fs .StringVar (& installDirPath , "path" , "" , "path to directory where production will be installed" )
61
+ fs .StringVar (& logFilePath , "log-file" , "" , "path to file where logs will be written" )
56
62
57
63
if err := fs .Parse (args ); err != nil {
58
64
return 1
@@ -83,7 +89,17 @@ Option flags must be provided before the positional argument`)
83
89
installDirPath = cwd
84
90
}
85
91
86
- installedPath , err := c .install (product , version , installDirPath )
92
+ logger := log .New (io .Discard , "" , 0 )
93
+ if logFilePath != "" {
94
+ f , err := os .OpenFile (logFilePath , os .O_WRONLY | os .O_APPEND | os .O_CREATE , 0644 )
95
+ if err != nil {
96
+ c .Ui .Error (fmt .Sprintf ("unable to log into %q: %s" , logFilePath , err ))
97
+ return 1
98
+ }
99
+ logger = log .New (f , "[DEBUG] " , log .LstdFlags | log .Lshortfile | log .Lmicroseconds )
100
+ }
101
+
102
+ installedPath , err := c .install (product , version , installDirPath , logger )
87
103
if err != nil {
88
104
msg := fmt .Sprintf ("failed to install %s@%s: %v" , product , version , err )
89
105
c .Ui .Error (msg )
@@ -94,7 +110,7 @@ Option flags must be provided before the positional argument`)
94
110
return 0
95
111
}
96
112
97
- func (c * InstallCommand ) install (project , tag , installDirPath string ) (string , error ) {
113
+ func (c * InstallCommand ) install (project , tag , installDirPath string , logger * log. Logger ) (string , error ) {
98
114
msg := fmt .Sprintf ("hc-install: will install %s@%s" , project , tag )
99
115
c .Ui .Info (msg )
100
116
@@ -103,6 +119,7 @@ func (c *InstallCommand) install(project, tag, installDirPath string) (string, e
103
119
return "" , fmt .Errorf ("invalid version: %w" , err )
104
120
}
105
121
i := hci .NewInstaller ()
122
+ i .SetLogger (logger )
106
123
107
124
source := & releases.ExactVersion {
108
125
Product : product.Product {
0 commit comments