Skip to content

Commit

Permalink
cmd/hc-install: Enable logging into a file (#193)
Browse files Browse the repository at this point in the history
* cmd/hc-install: Enable logging into a file

* improve documentation around stdout/stderr logging
  • Loading branch information
radeksimko committed Apr 5, 2024
1 parent 8f20715 commit 8093f09
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -108,8 +108,10 @@ Usage: hc-install install [options] -version <version> <product>
This command installs a HashiCorp product.
Options:
-version [REQUIRED] Version of product to install.
-path Path to directory where the product will be installed. Defaults
to current working directory.
-path Path to directory where the product will be installed.
Defaults to current working directory.
-log-file Path to file where logs will be written. /dev/stdout
or /dev/stderr can be used to log to STDOUT/STDERR.
```
```sh
hc-install install -version 1.3.7 terraform
Expand Down
25 changes: 21 additions & 4 deletions cmd/hc-install/cmd_install.go
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -37,8 +39,10 @@ Usage: hc-install install [options] -version <version> <product>
This command installs a HashiCorp product.
Options:
-version [REQUIRED] Version of product to install.
-path Path to directory where the product will be installed. Defaults
to current working directory.
-path Path to directory where the product will be installed.
Defaults to current working directory.
-log-file Path to file where logs will be written. /dev/stdout
or /dev/stderr can be used to log to STDOUT/STDERR.
`
return strings.TrimSpace(helpText)
}
Expand All @@ -47,12 +51,14 @@ func (c *InstallCommand) Run(args []string) int {
var (
version string
installDirPath string
logFilePath string
)

fs := flag.NewFlagSet("install", flag.ExitOnError)
fs.Usage = func() { c.Ui.Output(c.Help()) }
fs.StringVar(&version, "version", "", "version of product to install")
fs.StringVar(&installDirPath, "path", "", "path to directory where production will be installed")
fs.StringVar(&logFilePath, "log-file", "", "path to file where logs will be written")

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

installedPath, err := c.install(product, version, installDirPath)
logger := log.New(io.Discard, "", 0)
if logFilePath != "" {
f, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
c.Ui.Error(fmt.Sprintf("unable to log into %q: %s", logFilePath, err))
return 1
}
logger = log.New(f, "[DEBUG] ", log.LstdFlags|log.Lshortfile|log.Lmicroseconds)
}

installedPath, err := c.install(product, version, installDirPath, logger)
if err != nil {
msg := fmt.Sprintf("failed to install %s@%s: %v", product, version, err)
c.Ui.Error(msg)
Expand All @@ -94,7 +110,7 @@ Option flags must be provided before the positional argument`)
return 0
}

func (c *InstallCommand) install(project, tag, installDirPath string) (string, error) {
func (c *InstallCommand) install(project, tag, installDirPath string, logger *log.Logger) (string, error) {
msg := fmt.Sprintf("hc-install: will install %s@%s", project, tag)
c.Ui.Info(msg)

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

source := &releases.ExactVersion{
Product: product.Product{
Expand Down

0 comments on commit 8093f09

Please sign in to comment.