Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
708 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/pokanop/nostromo/task" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var force bool | ||
|
||
// syncCmd represents the sync command | ||
var syncCmd = &cobra.Command{ | ||
Use: "sync [source] [options]", | ||
Short: "Sync nostromo manifests from source locations", | ||
Long: `Sync nostromo manifests from source locations and | ||
makes commands available for execution. | ||
Sync can be used to copy a single manifest to nostromo's config | ||
folder. Or it can be used to update previously synchronized | ||
manifests from respective data sources. | ||
Providing a [source] file will sync only that file. Omitting it | ||
directs nostromo to sync all manifests.`, | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
os.Exit(task.Sync(force, args)) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(syncCmd) | ||
|
||
syncCmd.Flags().BoolVarP(&force, "force", "f", false, "Force sync manifest(s)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"sort" | ||
"time" | ||
|
||
"github.com/pokanop/nostromo/log" | ||
"github.com/pokanop/nostromo/model" | ||
"github.com/pokanop/nostromo/pathutil" | ||
) | ||
|
||
// backupManifest at config path based on timestamp | ||
func backupManifest(m *model.Manifest) error { | ||
// Before saving backup, prune old files | ||
pruneBackups(m) | ||
|
||
// Prevent backups if max count is 0 | ||
if m.Config.BackupCount == 0 { | ||
return nil | ||
} | ||
|
||
// Create backups under base dir in a folder | ||
backupDir, err := ensureBackupDir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Copy existing manifest to backup path | ||
ts := fmt.Sprintf("%d", int64(time.Nanosecond)*time.Now().UnixNano()/int64(time.Millisecond)) | ||
destinationFile := filepath.Join(backupDir, m.Name+"_"+ts+".yaml") | ||
sourceFile := pathutil.Abs(m.Path) | ||
|
||
// Check if manifest exists | ||
if _, err := os.Stat(sourceFile); os.IsNotExist(err) { | ||
return nil | ||
} | ||
|
||
input, err := ioutil.ReadFile(sourceFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = ioutil.WriteFile(destinationFile, input, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func pruneBackups(m *model.Manifest) { | ||
backupDir, err := ensureBackupDir() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Read all files, sort by timestamp, and drop items > max count | ||
files, err := ioutil.ReadDir(backupDir) | ||
if err != nil { | ||
log.Warningf("unable to read backup dir: %s\n", err) | ||
return | ||
} | ||
|
||
// Filter file list for matching names | ||
matches := []fs.FileInfo{} | ||
for _, file := range files { | ||
if isMatch, _ := regexp.MatchString(fmt.Sprintf("%s_\\d+\\.yaml", m.Name), file.Name()); isMatch { | ||
matches = append(matches, file) | ||
} | ||
} | ||
|
||
sort.SliceStable(matches, func(i, j int) bool { | ||
return matches[i].ModTime().After(matches[j].ModTime()) | ||
}) | ||
|
||
// Add one more to backup count since a new backup will be created | ||
maxCount := m.Config.BackupCount - 1 | ||
if maxCount < 0 { | ||
maxCount = 0 | ||
} | ||
if len(matches) > maxCount { | ||
for _, file := range matches[maxCount:] { | ||
filename := filepath.Join(backupDir, file.Name()) | ||
if err := os.Remove(filename); err != nil { | ||
log.Warningf("failed to prune backup file %s: %s\n", filename, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func ensureBackupDir() (string, error) { | ||
backupDir := backupsPath() | ||
if err := pathutil.EnsurePath(backupDir); err != nil { | ||
return "", err | ||
} | ||
return pathutil.Abs(backupDir), nil | ||
} |
Oops, something went wrong.