Skip to content

Commit 8093f09

Browse files
authored
cmd/hc-install: Enable logging into a file (#193)
* cmd/hc-install: Enable logging into a file * improve documentation around stdout/stderr logging
1 parent 8f20715 commit 8093f09

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ Usage: hc-install install [options] -version <version> <product>
108108
This command installs a HashiCorp product.
109109
Options:
110110
-version [REQUIRED] Version of product to install.
111-
-path Path to directory where the product will be installed. Defaults
112-
to current working directory.
111+
-path Path to directory where the product will be installed.
112+
Defaults to current working directory.
113+
-log-file Path to file where logs will be written. /dev/stdout
114+
or /dev/stderr can be used to log to STDOUT/STDERR.
113115
```
114116
```sh
115117
hc-install install -version 1.3.7 terraform

cmd/hc-install/cmd_install.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"context"
88
"flag"
99
"fmt"
10+
"io"
11+
"log"
1012
"os"
1113
"runtime"
1214
"strings"
@@ -37,8 +39,10 @@ Usage: hc-install install [options] -version <version> <product>
3739
This command installs a HashiCorp product.
3840
Options:
3941
-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.
4246
`
4347
return strings.TrimSpace(helpText)
4448
}
@@ -47,12 +51,14 @@ func (c *InstallCommand) Run(args []string) int {
4751
var (
4852
version string
4953
installDirPath string
54+
logFilePath string
5055
)
5156

5257
fs := flag.NewFlagSet("install", flag.ExitOnError)
5358
fs.Usage = func() { c.Ui.Output(c.Help()) }
5459
fs.StringVar(&version, "version", "", "version of product to install")
5560
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")
5662

5763
if err := fs.Parse(args); err != nil {
5864
return 1
@@ -83,7 +89,17 @@ Option flags must be provided before the positional argument`)
8389
installDirPath = cwd
8490
}
8591

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)
87103
if err != nil {
88104
msg := fmt.Sprintf("failed to install %s@%s: %v", product, version, err)
89105
c.Ui.Error(msg)
@@ -94,7 +110,7 @@ Option flags must be provided before the positional argument`)
94110
return 0
95111
}
96112

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) {
98114
msg := fmt.Sprintf("hc-install: will install %s@%s", project, tag)
99115
c.Ui.Info(msg)
100116

@@ -103,6 +119,7 @@ func (c *InstallCommand) install(project, tag, installDirPath string) (string, e
103119
return "", fmt.Errorf("invalid version: %w", err)
104120
}
105121
i := hci.NewInstaller()
122+
i.SetLogger(logger)
106123

107124
source := &releases.ExactVersion{
108125
Product: product.Product{

0 commit comments

Comments
 (0)