Don't use global variables (flags)

'
This commit is contained in:
Tim Hockin 2021-06-22 09:52:09 -07:00
parent 476fba3d3e
commit 16c89cec6f
1 changed files with 36 additions and 38 deletions

View File

@ -369,6 +369,7 @@ type repoSync struct {
link string // the name of the symlink to publish under `root` link string // the name of the symlink to publish under `root`
authURL string // a URL to re-fetch credentials, or "" authURL string // a URL to re-fetch credentials, or ""
sparseFile string // path to a sparse-checkout file sparseFile string // path to a sparse-checkout file
syncHookCmd string // command to run after each sync
} }
func main() { func main() {
@ -537,6 +538,7 @@ func main() {
link: *flLink, link: *flLink,
authURL: *flAskPassURL, authURL: *flAskPassURL,
sparseFile: *flSparseCheckoutFile, sparseFile: *flSparseCheckoutFile,
syncHookCmd: *flSyncHookCommand,
} }
// This context is used only for git credentials initialization. There are no long-running operations like // This context is used only for git credentials initialization. There are no long-running operations like
@ -551,7 +553,7 @@ func main() {
} }
if *flSSH { if *flSSH {
if err := setupGitSSH(*flSSHKnownHosts); err != nil { if err := setupGitSSH(*flSSHKnownHosts, *flSSHKeyFile, *flSSHKnownHostsFile); err != nil {
log.Error(err, "ERROR: can't set up git SSH") log.Error(err, "ERROR: can't set up git SSH")
os.Exit(1) os.Exit(1)
} }
@ -566,7 +568,7 @@ func main() {
// This needs to be after all other git-related config flags. // This needs to be after all other git-related config flags.
if *flGitConfig != "" { if *flGitConfig != "" {
if err := setupExtraGitConfigs(ctx, *flGitConfig); err != nil { if err := git.setupExtraGitConfigs(ctx, *flGitConfig); err != nil {
log.Error(err, "ERROR: can't set additional git configs") log.Error(err, "ERROR: can't set additional git configs")
os.Exit(1) os.Exit(1)
} }
@ -925,7 +927,7 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error
// This is required due to the undocumented behavior outlined here: // This is required due to the undocumented behavior outlined here:
// https://public-inbox.org/git/CAPig+cSP0UiEBXSCi7Ua099eOdpMk8R=JtAjPuUavRF4z0R0Vg@mail.gmail.com/t/ // https://public-inbox.org/git/CAPig+cSP0UiEBXSCi7Ua099eOdpMk8R=JtAjPuUavRF4z0R0Vg@mail.gmail.com/t/
log.V(0).Info("configuring worktree sparse checkout") log.V(0).Info("configuring worktree sparse checkout")
checkoutFile := *flSparseCheckoutFile checkoutFile := git.sparseFile
gitInfoPath := filepath.Join(git.root, fmt.Sprintf(".git/worktrees/%s/info", hash)) gitInfoPath := filepath.Join(git.root, fmt.Sprintf(".git/worktrees/%s/info", hash))
gitSparseConfigPath := filepath.Join(gitInfoPath, "sparse-checkout") gitSparseConfigPath := filepath.Join(gitInfoPath, "sparse-checkout")
@ -956,7 +958,7 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error
} }
args := []string{"sparse-checkout", "init"} args := []string{"sparse-checkout", "init"}
_, err = runCommand(ctx, worktreePath, *flGitCmd, args...) _, err = runCommand(ctx, worktreePath, git.cmd, args...)
if err != nil { if err != nil {
return err return err
} }
@ -1014,11 +1016,10 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error
// Execute the hook command, if requested. // Execute the hook command, if requested.
var execErr error var execErr error
if *flSyncHookCommand != "" { if git.syncHookCmd != "" {
log.V(0).Info("executing command for git sync hooks", "command", *flSyncHookCommand) log.V(0).Info("executing command for git sync hooks", "command", git.syncHookCmd)
// TODO: move this to be async like webhook? // TODO: move this to be async like webhook?
// TODO: don't use global flags way down here if _, err := runCommand(ctx, worktreePath, git.syncHookCmd); err != nil {
if _, err := runCommand(ctx, worktreePath, *flSyncHookCommand); err != nil {
// Save it until after cleanup runs. // Save it until after cleanup runs.
execErr = err execErr = err
} }
@ -1074,7 +1075,7 @@ func (git *repoSync) CloneRepo(ctx context.Context) error {
// If sparse checkout is requested, configure git for it. // If sparse checkout is requested, configure git for it.
if git.sparseFile != "" { if git.sparseFile != "" {
log.V(0).Info("configuring sparse checkout") log.V(0).Info("configuring sparse checkout")
checkoutFile := *flSparseCheckoutFile checkoutFile := git.sparseFile
// TODO: capture this as a function (mostly duplicated above) // TODO: capture this as a function (mostly duplicated above)
gitRepoPath := filepath.Join(git.root, ".git") gitRepoPath := filepath.Join(git.root, ".git")
@ -1107,7 +1108,7 @@ func (git *repoSync) CloneRepo(ctx context.Context) error {
} }
args := []string{"sparse-checkout", "init"} args := []string{"sparse-checkout", "init"}
_, err = runCommand(ctx, git.root, *flGitCmd, args...) _, err = runCommand(ctx, git.root, git.cmd, args...)
if err != nil { if err != nil {
return err return err
} }
@ -1295,12 +1296,9 @@ func (git *repoSync) SetupAuth(ctx context.Context, username, password string) e
return nil return nil
} }
func setupGitSSH(setupKnownHosts bool) error { func setupGitSSH(setupKnownHosts bool, pathToSSHSecret, pathToSSHKnownHosts string) error {
log.V(1).Info("setting up git SSH credentials") log.V(1).Info("setting up git SSH credentials")
var pathToSSHSecret = *flSSHKeyFile
var pathToSSHKnownHosts = *flSSHKnownHostsFile
_, err := os.Stat(pathToSSHSecret) _, err := os.Stat(pathToSSHSecret)
if err != nil { if err != nil {
return fmt.Errorf("can't access SSH key: %w", err) return fmt.Errorf("can't access SSH key: %w", err)
@ -1402,7 +1400,7 @@ func (git *repoSync) CallAskPassURL(ctx context.Context) error {
return nil return nil
} }
func setupExtraGitConfigs(ctx context.Context, configsFlag string) error { func (git *repoSync) setupExtraGitConfigs(ctx context.Context, configsFlag string) error {
log.V(1).Info("setting additional git configs") log.V(1).Info("setting additional git configs")
configs, err := parseGitConfigs(configsFlag) configs, err := parseGitConfigs(configsFlag)
@ -1410,7 +1408,7 @@ func setupExtraGitConfigs(ctx context.Context, configsFlag string) error {
return fmt.Errorf("can't parse --git-config flag: %v", err) return fmt.Errorf("can't parse --git-config flag: %v", err)
} }
for _, kv := range configs { for _, kv := range configs {
if _, err := runCommand(ctx, "", *flGitCmd, "config", "--global", kv.key, kv.val); err != nil { if _, err := runCommand(ctx, "", git.cmd, "config", "--global", kv.key, kv.val); err != nil {
return fmt.Errorf("error configuring additional git configs %q %q: %v", kv.key, kv.val, err) return fmt.Errorf("error configuring additional git configs %q %q: %v", kv.key, kv.val, err)
} }
} }