Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Allow setting repo user and hostname (#3831)
Browse files Browse the repository at this point in the history
Allows sourcing info about repo user/hostname from
both the config file and flags for maintenance.
Since user/hostname mostly matters for maintenance
don't add the flags for all commands.

Do allow config file parameters for them since
that has a lot of repo-level information already.
Currently not setup to persist these values in the
config file

---

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

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

#### Type of change

- [x] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Issue(s)

* #3569

#### Test Plan

- [x] 💪 Manual
- [ ] ⚡ Unit test
- [ ] 💚 E2E
  • Loading branch information
ashmrtn authored Jul 18, 2023
1 parent 88bddf5 commit 140381f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
23 changes: 20 additions & 3 deletions src/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/alcionai/corso/src/cli/flags"
. "github.com/alcionai/corso/src/cli/print"
"github.com/alcionai/corso/src/internal/common/str"
"github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/logger"
"github.com/alcionai/corso/src/pkg/storage"
Expand Down Expand Up @@ -38,6 +40,8 @@ const (

// Corso passphrase in config
CorsoPassphrase = "passphrase"
CorsoUser = "corso_user"
CorsoHost = "corso_host"
)

var (
Expand All @@ -50,9 +54,11 @@ var (
// RepoDetails holds the repository configuration retrieved from
// the .corso.toml configuration file.
type RepoDetails struct {
Storage storage.Storage
Account account.Account
RepoID string
Storage storage.Storage
Account account.Account
RepoID string
RepoUser string
RepoHost string
}

// Attempts to set the default dir and config file path.
Expand Down Expand Up @@ -294,9 +300,20 @@ func getStorageAndAccountWithViper(
return config, clues.Wrap(err, "retrieving storage provider details")
}

config.RepoUser, config.RepoHost = getUserHost(vpr, readConfigFromViper)

return config, nil
}

func getUserHost(vpr *viper.Viper, readConfigFromViper bool) (string, string) {
user := str.First(flags.UserMaintenanceFV, vpr.GetString(CorsoUser))
host := str.First(flags.HostnameMaintenanceFV, vpr.GetString(CorsoHost))

// Fine if these are empty; later code will assign a meaningful default if
// needed.
return user, host
}

// ---------------------------------------------------------------------------
// Helper funcs
// ---------------------------------------------------------------------------
Expand Down
30 changes: 26 additions & 4 deletions src/cli/flags/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
)

const (
MaintenanceModeFN = "mode"
ForceMaintenanceFN = "force"
MaintenanceModeFN = "mode"
ForceMaintenanceFN = "force"
UserMaintenanceFN = "user"
HostnameMaintenanceFN = "host"
)

var (
MaintenanceModeFV string
ForceMaintenanceFV bool
MaintenanceModeFV string
ForceMaintenanceFV bool
UserMaintenanceFV string
HostnameMaintenanceFV string
)

func AddMaintenanceModeFlag(cmd *cobra.Command) {
Expand All @@ -39,3 +43,21 @@ func AddForceMaintenanceFlag(cmd *cobra.Command) {
"Force maintenance. Caution: user must ensure this is not run concurrently on a single repo")
cobra.CheckErr(fs.MarkHidden(ForceMaintenanceFN))
}

func AddMaintenanceUserFlag(cmd *cobra.Command) {
fs := cmd.Flags()
fs.StringVar(
&UserMaintenanceFV,
UserMaintenanceFN,
"",
"Attempt to run maintenance as the specified user for the repo owner user")
}

func AddMaintenanceHostnameFlag(cmd *cobra.Command) {
fs := cmd.Flags()
fs.StringVar(
&HostnameMaintenanceFV,
HostnameMaintenanceFN,
"",
"Attempt to run maintenance with the specified hostname for the repo owner hostname")
}
2 changes: 2 additions & 0 deletions src/cli/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func AddCommands(cmd *cobra.Command) {
utils.MarkPreReleaseCommand())
flags.AddMaintenanceModeFlag(maintenanceCmd)
flags.AddForceMaintenanceFlag(maintenanceCmd)
flags.AddMaintenanceUserFlag(maintenanceCmd)
flags.AddMaintenanceHostnameFlag(maintenanceCmd)

for _, addRepoTo := range repoCommands {
addRepoTo(initCmd)
Expand Down
13 changes: 10 additions & 3 deletions src/cli/repo/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ func initS3Cmd(cmd *cobra.Command, args []string) error {
return Only(ctx, err)
}

opt := utils.ControlWithConfig(cfg)

// SendStartCorsoEvent uses distict ID as tenant ID because repoID is still not generated
utils.SendStartCorsoEvent(
ctx,
cfg.Storage,
cfg.Account.ID(),
map[string]any{"command": "init repo"},
cfg.Account.ID(),
utils.Control())
opt)

s3Cfg, err := cfg.Storage.S3Config()
if err != nil {
Expand All @@ -156,7 +158,7 @@ func initS3Cmd(cmd *cobra.Command, args []string) error {
return Only(ctx, clues.Wrap(err, "Failed to parse m365 account config"))
}

r, err := repository.Initialize(ctx, cfg.Account, cfg.Storage, utils.Control())
r, err := repository.Initialize(ctx, cfg.Account, cfg.Storage, opt)
if err != nil {
if succeedIfExists && errors.Is(err, repository.ErrorRepoAlreadyExists) {
return nil
Expand Down Expand Up @@ -226,7 +228,12 @@ func connectS3Cmd(cmd *cobra.Command, args []string) error {
return Only(ctx, clues.New(invalidEndpointErr))
}

r, err := repository.ConnectAndSendConnectEvent(ctx, cfg.Account, cfg.Storage, repoID, utils.Control())
r, err := repository.ConnectAndSendConnectEvent(
ctx,
cfg.Account,
cfg.Storage,
repoID,
utils.ControlWithConfig(cfg))
if err != nil {
return Only(ctx, clues.Wrap(err, "Failed to connect to the S3 repository"))
}
Expand Down
10 changes: 10 additions & 0 deletions src/cli/utils/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"github.com/alcionai/corso/src/cli/config"
"github.com/alcionai/corso/src/cli/flags"
"github.com/alcionai/corso/src/pkg/control"
)
Expand All @@ -24,3 +25,12 @@ func Control() control.Options {

return opt
}

func ControlWithConfig(cfg config.RepoDetails) control.Options {
opt := Control()

opt.Repo.User = cfg.RepoUser
opt.Repo.Host = cfg.RepoHost

return opt
}
2 changes: 1 addition & 1 deletion src/cli/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetAccountAndConnect(
repoID = events.RepoIDNotFound
}

r, err := repository.Connect(ctx, cfg.Account, cfg.Storage, repoID, Control())
r, err := repository.Connect(ctx, cfg.Account, cfg.Storage, repoID, ControlWithConfig(cfg))
if err != nil {
return nil, nil, nil, clues.Wrap(err, "connecting to the "+cfg.Storage.Provider.String()+" repository")
}
Expand Down

0 comments on commit 140381f

Please sign in to comment.