corso/src/cli/help/env.go
Abin Simon 68256cf59f
Change printing of backup stats to single line (#4526)
Previously we were printing a table for a single item which, for one
was unnecessary, but this also make the output of a backup more
compact.

Before:

```
Logging to file: /home/meain/.cache/corso/logs/2023-10-20T10-21-23Z.log
Connecting to M365                               done
Connecting to repository                      1s done

Backing Up ∙ Teams Testing (meain)
Discovering items to backup                  27s done
    Libraries (TeamsTestingmeain)                  done (found 0 items)
    Libraries (TeamsTestingmeain-Shared0)          done (found 0 items)
    Libraries (TeamsTestingmeain-Private2)         done (found 0 items)
    Messages                                    7s done (found 6 channels)
Backing up data                               7s done
Backup complete ∙ 7b40dd40-f808-4d57-8e39-b4553e48dc5d
    ID                                    Bytes Uploaded  Items Uploaded  Items Skipped  Errors
    7b40dd40-f808-4d57-8e39-b4553e48dc5d  0 B             0               0              0

Completed Backups:
    ID                                    Started At            Duration       Status     Resource Owner
    7b40dd40-f808-4d57-8e39-b4553e48dc5d  2023-10-20T10:21:32Z  36.632747912s  Completed  Teams Testing (meain)
```

After:
```
Connecting to M365                               done
Connecting to repository                      1s done

Backing Up ∙ Teams Testing (meain)
Discovering items to backup                  31s done
    Libraries (TeamsTestingmeain)                  done (found 0 items)
    Libraries (TeamsTestingmeain-Shared0)          done (found 0 items)
    Libraries (TeamsTestingmeain-Private2)         done (found 0 items)
    Messages                                    9s done (found 6 channels)
Backing up data                               7s done
Backup complete ∙ ffb2f619-1cb7-4a11-b3e2-7300aa513c6a
Bytes Uploaded: 0 B | Items Uploaded: 0 | Items Skipped: 0 | Errors: 0

Completed Backups:
    ID                                    ID                    Started At     Duration   Status                 Resource Owner
    ffb2f619-1cb7-4a11-b3e2-7300aa513c6a  2023-10-20T10:23:35Z  40.096203016s  Completed  Teams Testing (meain)
```


---

#### Does this PR need a docs update or release note?

- [x]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [ ]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* #<issue>

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
2023-11-16 12:43:27 +00:00

104 lines
2.7 KiB
Go

package help
import (
"github.com/spf13/cobra"
. "github.com/alcionai/corso/src/cli/print"
)
// AddCommands attaches all `corso env * *` commands to the parent.
func AddCommands(cmd *cobra.Command) {
cmd.AddCommand(envCmd())
}
// The env command: purely a help display.
// `corso env [--help]`
func envCmd() *cobra.Command {
envCmd := &cobra.Command{
Use: "env",
Short: "env var guide",
Long: `A guide to using environment variables in Corso.`,
RunE: handleEnvCmd,
Args: cobra.NoArgs,
}
envCmd.SetHelpFunc(envGuide)
return envCmd
}
// Handler for flat calls to `corso env`.
// Produces the same output as `corso env --help`.
func handleEnvCmd(cmd *cobra.Command, args []string) error {
return cmd.Help()
}
type envVar struct {
category string
name string
description string
}
// interface compliance check
var _ Printable = &envVar{}
// no modifications needed, just passthrough.
func (ev envVar) MinimumPrintable() any {
return ev
}
func (ev envVar) Headers(bool) []string {
// NOTE: skipID does not make sense in this context
return []string{ev.category, " "}
}
func (ev envVar) Values(bool) []string {
return []string{ev.name, ev.description}
}
// headers
const (
corso = "Corso"
azure = "Azure AD App Credentials"
aws = "AWS Credentials"
)
var (
corsoEVs = []envVar{
{corso, "CORSO_PASSPHRASE", "Passphrase to protect encrypted repository contents. " +
"It is impossible to use the repository or recover any backups without this key."},
}
azureEVs = []envVar{
{azure, "AZURE_CLIENT_ID", "Client ID for your Azure AD application used to access your M365 tenant."},
{azure, "AZURE_TENANT_ID", "ID for the M365 tenant where the Azure AD application is registered."},
{azure, "AZURE_CLIENT_SECRET", "Azure secret for your Azure AD application used to access your M365 tenant."},
}
awsEVs = []envVar{
{aws, "AWS_ACCESS_KEY_ID", "Access key for an IAM user or role for accessing an S3 bucket."},
{aws, "AWS_SECRET_ACCESS_KEY", "Secret key associated with the access key."},
{aws, "AWS_SESSION_TOKEN", "Session token required when using temporary credentials."},
}
)
func toPrintable(evs []envVar) []Printable {
ps := []Printable{}
for _, ev := range evs {
ps = append(ps, ev)
}
return ps
}
// envGuide outputs a help menu for setting env vars in Corso.
func envGuide(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
Info(ctx,
"\n--- Environment Variable Guide ---\n",
"As a best practice, Corso retrieves credentials and sensitive information from environment variables.\n ",
"\n")
Table(ctx, toPrintable(corsoEVs))
Info(ctx, "\n")
Table(ctx, toPrintable(azureEVs))
Info(ctx, "\n")
Table(ctx, toPrintable(awsEVs))
}