First steps -pref-dir

This commit is contained in:
Etienne Charlier
2017-06-05 23:16:11 +02:00
parent 36044e13c0
commit c88e148d59
10 changed files with 194 additions and 31 deletions

View File

@@ -18,6 +18,8 @@ import (
"github.com/gilbertchen/cli"
"github.com/gilbertchen/duplicacy"
"io/ioutil"
)
const (
@@ -26,6 +28,7 @@ const (
var ScriptEnabled bool
func getRepositoryPreference(context *cli.Context, storageName string) (repository string,
preference *duplicacy.Preference) {
@@ -34,16 +37,15 @@ func getRepositoryPreference(context *cli.Context, storageName string) (reposito
duplicacy.LOG_ERROR("REPOSITORY_PATH", "Failed to retrieve the current working directory: %v", err)
return "", nil
}
for {
stat, err := os.Stat(path.Join(repository, duplicacy.DUPLICACY_DIRECTORY))
stat, err := os.Stat(path.Join(repository, duplicacy.DUPLICACY_DIRECTORY)) //TOKEEP
if err != nil && !os.IsNotExist(err) {
duplicacy.LOG_ERROR("REPOSITORY_PATH", "Failed to retrieve the information about the directory %s: %v",
repository, err)
return "", nil
}
if stat != nil && stat.IsDir() {
if stat != nil && (stat.IsDir() || stat.Mode().IsRegular()) {
break
}
@@ -54,10 +56,10 @@ func getRepositoryPreference(context *cli.Context, storageName string) (reposito
}
repository = parent
}
duplicacy.LoadPreferences(repository)
duplicacy.SetKeyringFile(path.Join(repository, duplicacy.DUPLICACY_DIRECTORY, "keyring"))
duplicacyDirectory := duplicacy.GetDotDuplicacyPathName(repository)
duplicacy.SetKeyringFile(path.Join(duplicacyDirectory, "keyring"))
if storageName == "" {
storageName = context.String("storage")
@@ -142,8 +144,9 @@ func runScript(context *cli.Context, repository string, storageName string, phas
if !ScriptEnabled {
return false
}
scriptDir, _ := filepath.Abs(path.Join(repository, duplicacy.DUPLICACY_DIRECTORY, "scripts"))
duplicacyDirectory := duplicacy.GetDotDuplicacyPathName(repository)
scriptDir, _ := filepath.Abs(path.Join(duplicacyDirectory, "scripts"))
scriptName := phase + "-" + context.Command.Name
script := path.Join(scriptDir, scriptName)
@@ -174,14 +177,14 @@ func runScript(context *cli.Context, repository string, storageName string, phas
}
func initRepository(context *cli.Context) {
configRespository(context, true)
configRepository(context, true)
}
func addStorage(context *cli.Context) {
configRespository(context, false)
configRepository(context, false)
}
func configRespository(context *cli.Context, init bool) {
func configRepository(context *cli.Context, init bool) {
setGlobalOptions(context)
defer duplicacy.CatchLogException()
@@ -220,8 +223,15 @@ func configRespository(context *cli.Context, init bool) {
duplicacy.LOG_ERROR("REPOSITORY_PATH", "Failed to retrieve the current working directory: %v", err)
return
}
duplicacyDirectory := path.Join(repository, duplicacy.DUPLICACY_DIRECTORY)
duplicacyDirectory := context.String("pref-dir")
if duplicacyDirectory == "" {
duplicacyDirectory = path.Join(repository, duplicacy.DUPLICACY_DIRECTORY) // TOKEEP
}
duplicacy.LOG_INFO("PREF_PATH", "-pref-dir value: --|%s|-- ", duplicacyDirectory)
if stat, _ := os.Stat(path.Join(duplicacyDirectory, "preferences")); stat != nil {
duplicacy.LOG_ERROR("REPOSITORY_INIT", "The repository %s has already been initialized", repository)
return
@@ -230,10 +240,20 @@ func configRespository(context *cli.Context, init bool) {
err = os.Mkdir(duplicacyDirectory, 0744)
if err != nil && !os.IsExist(err) {
duplicacy.LOG_ERROR("REPOSITORY_INIT", "Failed to create the directory %s: %v",
duplicacy.DUPLICACY_DIRECTORY, err)
duplicacyDirectory, err)
return
}
if context.String("pref-dir") != "" {
// out of tree preference file
// write real path into .duplicacy file inside repository
duplicacyFileName := path.Join(repository, duplicacy.DUPLICACY_FILE)
d1 := []byte(duplicacyDirectory)
err = ioutil.WriteFile(duplicacyFileName, d1, 0644)
if err != nil {
duplicacy.LOG_ERROR("REPOSITORY_PATH", "Failed to write %s file inside repository %v", duplicacyFileName, err)
return
}
}
duplicacy.SetKeyringFile(path.Join(duplicacyDirectory, "keyring"))
} else {
@@ -547,7 +567,6 @@ func changePassword(context *cli.Context) {
duplicacy.LOG_INFO("STORAGE_SET", "The password for storage %s has been changed", preference.StorageURL)
}
func backupRepository(context *cli.Context) {
setGlobalOptions(context)
defer duplicacy.CatchLogException()
@@ -1071,7 +1090,8 @@ func infoStorage(context *cli.Context) {
repository := context.String("repository")
if repository != "" {
duplicacy.SetKeyringFile(path.Join(repository, duplicacy.DUPLICACY_DIRECTORY, "keyring"))
duplicacyDirectory := duplicacy.GetDotDuplicacyPathName(repository)
duplicacy.SetKeyringFile(path.Join(duplicacyDirectory, "keyring"))
}
isEncrypted := context.Bool("e")
@@ -1132,6 +1152,11 @@ func main() {
Usage: "the minimum size of chunks (defaults to chunk-size / 4)",
Argument: "1M",
},
cli.StringFlag{
Name: "pref-dir",
Usage: "Specify alternate location for .duplicacy preferences directory (absolute or relative to current directory)",
Argument: "<preference directory path>",
},
},
Usage: "Initialize the storage if necessary and the current directory as the repository",
ArgsUsage: "<snapshot id> <storage url>",