Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup profile registry implementation + improve testability #622

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/assume/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func GetCliApp() *cli.App {
browser.GrantedIntroduction()
}
// Sync granted profile registries if enabled
autosync.Run(false)
autosync.Run(c.Context, true)

// Setup the shell alias
if os.Getenv("FORCE_NO_ALIAS") != "true" {
Expand Down
9 changes: 5 additions & 4 deletions pkg/autosync/autosync.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package autosync

import (
"context"
"time"

"github.com/common-fate/clio"
"github.com/common-fate/granted/pkg/granted/registry"
)

// shouldFailForRequiredKeys when true will fail the profile registry sync
// interactive when false will fail the profile registry sync
// in case where user specific values that are defined in granted.yml's `templateValues` are not available.
// this is done so that users are aware of required keys when granted's credential-process is used through the AWS CLI.
func Run(shouldFailForRequiredKeys bool) {
func Run(ctx context.Context, interactive bool) {
if registry.IsOutdatedConfig() {
clio.Warn("Outdated Profile Registry Configuration. Use `granted registry migrate` to update your configuration.")

Expand All @@ -19,7 +20,7 @@ func Run(shouldFailForRequiredKeys bool) {
return
}

registries, err := registry.GetProfileRegistries()
registries, err := registry.GetProfileRegistries(interactive)
if err != nil {
clio.Debugf("unable to load granted config file with err %s", err.Error())
return
Expand All @@ -40,7 +41,7 @@ func Run(shouldFailForRequiredKeys bool) {
return
}

err = runSync(rc, shouldFailForRequiredKeys)
err = runSync(ctx, rc, interactive)
if err != nil {
clio.Debugw("failed to sync profile registries", "error", err)
clio.Warn("Failed to sync Profile Registries")
Expand Down
6 changes: 3 additions & 3 deletions pkg/autosync/registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package autosync

import (
"context"
"fmt"
"time"

Expand All @@ -16,10 +17,9 @@ func (e *RegistrySyncError) Error() string {
return fmt.Sprintf("error syncing profile registry with err: %s", e.err.Error())
}

func runSync(rc RegistrySyncConfig, shouldFailForRequiredKeys bool) error {
func runSync(ctx context.Context, rc RegistrySyncConfig, interactive bool) error {
clio.Info("Syncing Profile Registries")
shouldSilentLog := true
err := registry.SyncProfileRegistries(shouldSilentLog, false, shouldFailForRequiredKeys)
err := registry.SyncProfileRegistries(ctx, interactive)
if err != nil {
return &RegistrySyncError{err: err}
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ type KeyringConfig struct {
}

type Registry struct {
Name string `toml:"name"`
URL string `toml:"url"`
Path *string `toml:"path,omitempty"`
Filename *string `toml:"filename,omitempty"`
Ref *string `toml:"ref,omitempty"`
Priority *int `toml:"priority, omitempty"`
PrefixDuplicateProfiles bool `toml:"prefixDuplicateProfiles,omitempty"`
PrefixAllProfiles bool `toml:"prefixAllProfiles,omitempty"`
WriteOnSyncFailure bool `toml:"writeOnSyncFailure,omitempty"`
Name string `toml:"name"`
URL string `toml:"url"`
Path string `toml:"path,omitempty"`
Filename string `toml:"filename,omitempty"`
Ref string `toml:"ref,omitempty"`
Priority int `toml:"priority,omitempty"`
PrefixDuplicateProfiles bool `toml:"prefixDuplicateProfiles,omitempty"`
PrefixAllProfiles bool `toml:"prefixAllProfiles,omitempty"`
}

// NewDefaultConfig returns a config with OS specific defaults populated
Expand Down
34 changes: 34 additions & 0 deletions pkg/git/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package git

import (
"bufio"
"fmt"
"os/exec"
"strings"

"github.com/common-fate/clio"
)

// Clone wraps the command 'git clone'.
func Clone(repoURL string, repoDirPath string) error {
clio.Debugf("git clone %s\n", repoURL)

cmd := exec.Command("git", "clone", repoURL, repoDirPath)

stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
return err
}

scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
if strings.Contains(strings.ToLower(scanner.Text()), "error") || strings.Contains(strings.ToLower(scanner.Text()), "fatal") {
return fmt.Errorf(scanner.Text())
}

clio.Info(scanner.Text())
}
clio.Debugf("Successfully cloned %s", repoURL)

return nil
}
20 changes: 20 additions & 0 deletions pkg/git/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package git

import (
"os/exec"

"github.com/common-fate/clio"
)

func Init(repoDirPath string) error {
clio.Debugf("git init %s\n", repoDirPath)

cmd := exec.Command("git", "init", repoDirPath)

err := cmd.Run()
if err != nil {
return err
}

return nil
}
41 changes: 41 additions & 0 deletions pkg/git/pull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package git

import (
"bufio"
"fmt"
"os/exec"
"strings"

"github.com/common-fate/clio"
)

// Pull wraps the command 'git pull'.
func Pull(repoDirPath string, shouldSilentLogs bool) error {
// pull the repo here.
clio.Debugf("git -C %s pull %s %s\n", repoDirPath, "origin", "HEAD")
cmd := exec.Command("git", "-C", repoDirPath, "pull", "origin", "HEAD")

// StderrPipe returns a pipe that will be connected to the command's
// standard error when the command starts.
stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
return err
}

scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "error") || strings.Contains(scanner.Text(), "fatal") {
return fmt.Errorf(scanner.Text())
}

if shouldSilentLogs {
clio.Debug(scanner.Text())
} else {
clio.Info(scanner.Text())
}
}

clio.Debugf("Successfully pulled the repo")

return nil
}