* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
178 lines
4.8 KiB
Go
178 lines
4.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package sessionrecordingscmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/boundary/api"
|
|
"github.com/hashicorp/boundary/api/sessionrecordings"
|
|
"github.com/hashicorp/boundary/internal/cmd/base"
|
|
"github.com/mitchellh/cli"
|
|
"github.com/mitchellh/go-wordwrap"
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
var (
|
|
_ cli.Command = (*DownloadCommand)(nil)
|
|
_ cli.CommandAutocomplete = (*DownloadCommand)(nil)
|
|
)
|
|
|
|
const (
|
|
castExt = ".cast" // default download file extension (is overridden when an output file is specified)
|
|
)
|
|
|
|
type DownloadCommand struct {
|
|
*base.Command
|
|
}
|
|
|
|
func (c *DownloadCommand) Synopsis() string {
|
|
return wordwrap.WrapString("Download a session recording", base.TermWidth)
|
|
}
|
|
|
|
func (c *DownloadCommand) Help() string {
|
|
return base.WrapForHelpText([]string{
|
|
"Usage: boundary session-recordings download [args]",
|
|
"",
|
|
" Download a session recording resource. Example:",
|
|
"",
|
|
` $ boundary session-recordings download -id chr_u6e9wJ8B8H`,
|
|
"",
|
|
"",
|
|
}) + c.Flags().Help()
|
|
}
|
|
|
|
func (c *DownloadCommand) Flags() *base.FlagSets {
|
|
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetClient | base.FlagSetOutputFormat)
|
|
f := set.NewFlagSet("Command Options")
|
|
|
|
f.StringVar(&base.StringVar{
|
|
Name: "id",
|
|
Target: &c.FlagId,
|
|
Usage: "The id of the session recording resource to download.",
|
|
})
|
|
f.StringVar(&base.StringVar{
|
|
Name: "output",
|
|
Target: &c.FlagOutputFile,
|
|
Usage: "An optional output file for the download. If not provided the recording id will be used with a \".cast\" extension. Use \"-\" for stdout.",
|
|
Aliases: []string{"o"},
|
|
})
|
|
f.BoolVar(&base.BoolVar{
|
|
Name: "no-clobber",
|
|
Target: &c.FlagNoClobber,
|
|
Usage: "An option to stop downloads that would overwrite existing files.",
|
|
Aliases: []string{"nc"},
|
|
})
|
|
return set
|
|
}
|
|
|
|
func (c *DownloadCommand) AutocompleteArgs() complete.Predictor {
|
|
return complete.PredictAnything
|
|
}
|
|
|
|
func (c *DownloadCommand) AutocompleteFlags() complete.Flags {
|
|
return c.Flags().Completions()
|
|
}
|
|
|
|
func (c *DownloadCommand) Run(args []string) int {
|
|
f := c.Flags()
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
c.PrintCliError(err)
|
|
return base.CommandUserError
|
|
}
|
|
|
|
switch {
|
|
case c.FlagId == "":
|
|
c.PrintCliError(errors.New("ID must be provided via -id"))
|
|
return base.CommandUserError
|
|
}
|
|
|
|
client, err := c.Client()
|
|
if c.WrapperCleanupFunc != nil {
|
|
defer func() {
|
|
if err := c.WrapperCleanupFunc(); err != nil {
|
|
c.PrintCliError(fmt.Errorf("Error cleaning kms wrapper: %w", err))
|
|
}
|
|
}()
|
|
}
|
|
if err != nil {
|
|
c.PrintCliError(fmt.Errorf("Error creating API client: %w", err))
|
|
return base.CommandCliError
|
|
}
|
|
|
|
sClient := sessionrecordings.NewClient(client)
|
|
result, err := sClient.Download(c.Context, c.FlagId)
|
|
if err != nil {
|
|
if apiErr := api.AsServerError(err); apiErr != nil {
|
|
c.PrintApiError(apiErr, "Error from controller when downloading session recording")
|
|
return base.CommandApiError
|
|
}
|
|
c.PrintCliError(fmt.Errorf("Download error: %w", err))
|
|
return base.CommandCliError
|
|
}
|
|
|
|
var outFile *os.File
|
|
switch {
|
|
case c.FlagOutputFile == "-":
|
|
outFile = os.Stdout
|
|
case c.FlagOutputFile != "" && c.FlagNoClobber:
|
|
_, err := os.Stat(c.FlagOutputFile)
|
|
switch {
|
|
case os.IsNotExist(err):
|
|
outFile, err = os.Create(c.FlagOutputFile)
|
|
if err != nil {
|
|
c.PrintCliError(fmt.Errorf("Unable to create download file %q when \"-nc\" was provided: %w", c.FlagOutputFile, err))
|
|
return base.CommandCliError
|
|
}
|
|
defer outFile.Close()
|
|
case err != nil:
|
|
c.PrintCliError(fmt.Errorf("Error trying to check if file %q exists when \"-nc\" was provided: %w", c.FlagOutputFile, err))
|
|
return base.CommandCliError
|
|
default:
|
|
c.PrintCliError(fmt.Errorf("Error trying to overwrite to an existing file %q when \"-nc\" was provided", c.FlagOutputFile))
|
|
return base.CommandCliError
|
|
}
|
|
case c.FlagOutputFile != "":
|
|
outFile, err = os.Create(c.FlagOutputFile)
|
|
if err != nil {
|
|
c.PrintCliError(fmt.Errorf("Unable to create requested download file %q: %w", c.FlagOutputFile, err))
|
|
return base.CommandCliError
|
|
}
|
|
defer outFile.Close()
|
|
default:
|
|
fileName := getNextFileName(c.FlagId)
|
|
outFile, err = os.Create(fileName)
|
|
if err != nil {
|
|
c.PrintCliError(fmt.Errorf("Unable to create download file %q: %w", fileName, err))
|
|
return base.CommandCliError
|
|
}
|
|
defer outFile.Close()
|
|
}
|
|
|
|
if _, err := io.Copy(outFile, result); err != nil {
|
|
c.PrintCliError(fmt.Errorf("Error reading download stream: %w", err))
|
|
return base.CommandCliError
|
|
}
|
|
return base.CommandSuccess
|
|
}
|
|
|
|
func getNextFileName(baseName string) string {
|
|
if _, err := os.Stat(baseName + castExt); os.IsNotExist(err) {
|
|
return baseName + castExt
|
|
}
|
|
startIndex := 1
|
|
for {
|
|
fileName := baseName + castExt + "." + strconv.Itoa(startIndex)
|
|
if _, err := os.Stat(fileName); os.IsNotExist(err) {
|
|
return fileName
|
|
}
|
|
startIndex++
|
|
}
|
|
}
|