Skip to content

Commit e6da01d

Browse files
committed
fix: ensure directory exists before rolling backup
fix: don't create backup directory for dry-run docs: only authenticated user will fetch shared repos
1 parent e5ecc73 commit e6da01d

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

config.example.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ log-level: info
1212
output: backup
1313
# GitHub token with read access to the repositories and user
1414
token: ""
15-
# List of usernames to fetch. If neither usernames or in-org is set, it will fetch the token owner's repositories
15+
# List of usernames to fetch. If neither usernames or in-org are set (or an empty string is passed), the authenticated user will (also) be fetched. Fetching the authenticated user also fetches repositories shared with the authenticated user.
1616
usernames: []
1717
# `clone` (clone the repositories), `fetch` (fetch the repositories and write to output if it ends in .json or `repositories.json` in output), `dry-run` (fetch the repositories and print the output)
1818
run-type: clone

pkg/backup/backup.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ func Backup(config BackupConfig) error {
100100
repos = append(repos, fetchedRepos.Starred...)
101101
}
102102
if len(allUsers) == 0 {
103+
// Just to be verbose, set the username to ""
104+
fetchConfig.Username = ""
103105
fetchedRepos, err := GetRepositories(
104106
fetchConfig,
105107
)
@@ -227,7 +229,7 @@ func StartBackup(
227229
var wg sync.WaitGroup
228230

229231
// Run backup on start
230-
backupConfig.Output, err = utils.RollingDir(parentDir, maxBackups)
232+
backupConfig.Output, err = rollingDirIfNotDryRun(backupConfig, maxBackups, parentDir)
231233
if err != nil {
232234
return err
233235
}
@@ -241,8 +243,7 @@ func StartBackup(
241243
// The backup will not be concurrent if the backup process takes longer than the interval
242244
for range ticker.C {
243245
wg.Add(1)
244-
245-
backupConfig.Output, err = utils.RollingDir(parentDir, maxBackups)
246+
backupConfig.Output, err = rollingDirIfNotDryRun(backupConfig, maxBackups, parentDir)
246247
if err != nil {
247248
errChan <- err
248249
return
@@ -272,3 +273,13 @@ func StartBackup(
272273
}
273274
return nil
274275
}
276+
277+
// Only run utils.RollingDir if not in a dry run
278+
func rollingDirIfNotDryRun(config BackupConfig, maxBackups int, parentDir string) (string, error) {
279+
if config.RunType != "dry-run" {
280+
return utils.RollingDir(filepath.Clean(parentDir), maxBackups)
281+
} else {
282+
log.Debug("Dry run - not rolling directories")
283+
}
284+
return config.Output, nil
285+
}

pkg/backup/repositories.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func GetRepositories(config *FetchConfig) (*Repositories, error) {
6262
return nil, err
6363
}
6464
username := user.GetLogin()
65-
log.Info("Fetching repositories for user", "username", username)
6665

6766
// https://github.com/google/go-github?tab=readme-ov-file#pagination
6867
listOptions := github.ListOptions{PerPage: 100}
@@ -71,6 +70,8 @@ func GetRepositories(config *FetchConfig) (*Repositories, error) {
7170
// https://pkg.go.dev/github.com/google/go-github/v63@v63.0.0/github#RepositoriesService.ListByUser
7271
var userRepos []*github.Repository
7372
if config.Username == "" {
73+
log.Info("Fetching repositories for the authenticated user; shared repositories will also be fetched.", "username", username)
74+
7475
opt := &github.RepositoryListByAuthenticatedUserOptions{
7576
ListOptions: listOptions,
7677
}
@@ -86,6 +87,9 @@ func GetRepositories(config *FetchConfig) (*Repositories, error) {
8687
opt.Page = resp.NextPage
8788
}
8889
} else {
90+
91+
log.Info("Fetching repositories for user", "username", username)
92+
8993
opt := &github.RepositoryListByUserOptions{
9094
ListOptions: listOptions,
9195
}

pkg/utils/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ func RollingDir(pathToDir string, maxBackups int) (string, error) {
5252
// Get all directories in the path
5353
dirs := []string{}
5454

55+
// Whilst the directory is created when backing up (clone or fetch), that's later. If the directory doesn't exist, os.ReadDir error. Thus, ensure it, and any parent directories, exist.
56+
err := os.MkdirAll(pathToDir, 0644)
57+
if err != nil {
58+
return "", err
59+
}
60+
5561
filesAndDirs, err := os.ReadDir(pathToDir)
5662
if err != nil {
5763
return "", err

0 commit comments

Comments
 (0)