|
|
|
|
@ -21,15 +21,15 @@ var remoteRepository string
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
RootCmd.AddCommand(syncCommand)
|
|
|
|
|
syncCommand.Flags().StringVarP(
|
|
|
|
|
&remoteRepository,
|
|
|
|
|
"remote",
|
|
|
|
|
"r",
|
|
|
|
|
"",
|
|
|
|
|
"URL of remote repository",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
viper.BindPFlag("dotctl-origin", syncCommand.Flags().Lookup("remote"))
|
|
|
|
|
syncCommand.Flags().StringVarP(
|
|
|
|
|
&remoteRepository,
|
|
|
|
|
"remote",
|
|
|
|
|
"r",
|
|
|
|
|
"",
|
|
|
|
|
"URL of remote repository",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
viper.BindPFlag("dotctl-origin", syncCommand.Flags().Lookup("remote"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var syncCommand = &cobra.Command{
|
|
|
|
|
@ -40,29 +40,29 @@ var syncCommand = &cobra.Command{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateInput(input string) error {
|
|
|
|
|
if input == "" {
|
|
|
|
|
return errors.New("Missing input")
|
|
|
|
|
}
|
|
|
|
|
if input == "" {
|
|
|
|
|
return errors.New("Missing input")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func gitAddFiles(worktree *git.Worktree, fs afero.Fs) error {
|
|
|
|
|
dotfilepath := viper.GetString("dotfile-path")
|
|
|
|
|
entries, err := afero.ReadDir(fs, dotfilepath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, entry := range(entries) {
|
|
|
|
|
if(entry.Name() == "dotctl") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
_, err = worktree.Add(entry.Name())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
dotfilepath := viper.GetString("dotfile-path")
|
|
|
|
|
entries, err := afero.ReadDir(fs, dotfilepath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
|
if entry.Name() == "dotctl" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
_, err = worktree.Add(entry.Name())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runSyncCommand(cmd *cobra.Command, args []string) {
|
|
|
|
|
@ -91,117 +91,116 @@ func runSyncCommand(cmd *cobra.Command, args []string) {
|
|
|
|
|
w, err := r.Worktree()
|
|
|
|
|
CheckIfError(err)
|
|
|
|
|
|
|
|
|
|
username := promptui.Prompt{
|
|
|
|
|
Label: "username",
|
|
|
|
|
Validate: validateInput,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
password := promptui.Prompt{
|
|
|
|
|
Label: "password",
|
|
|
|
|
Validate: validateInput,
|
|
|
|
|
HideEntered: true,
|
|
|
|
|
Mask: '*',
|
|
|
|
|
}
|
|
|
|
|
username := promptui.Prompt{
|
|
|
|
|
Label: "username",
|
|
|
|
|
Validate: validateInput,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usernameVal, err := username.Run()
|
|
|
|
|
CheckIfError(err)
|
|
|
|
|
password := promptui.Prompt{
|
|
|
|
|
Label: "password",
|
|
|
|
|
Validate: validateInput,
|
|
|
|
|
HideEntered: true,
|
|
|
|
|
Mask: '*',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
passwordVal, err := password.Run()
|
|
|
|
|
CheckIfError(err)
|
|
|
|
|
usernameVal, err := username.Run()
|
|
|
|
|
CheckIfError(err)
|
|
|
|
|
|
|
|
|
|
passwordVal, err := password.Run()
|
|
|
|
|
CheckIfError(err)
|
|
|
|
|
|
|
|
|
|
fmt.Println("Pulling from remote")
|
|
|
|
|
fmt.Println("Pulling from remote")
|
|
|
|
|
|
|
|
|
|
err = w.Pull(&git.PullOptions{
|
|
|
|
|
RemoteName: "origin",
|
|
|
|
|
Auth: &http.BasicAuth {
|
|
|
|
|
Username: usernameVal,
|
|
|
|
|
Password: passwordVal,
|
|
|
|
|
},
|
|
|
|
|
Auth: &http.BasicAuth{
|
|
|
|
|
Username: usernameVal,
|
|
|
|
|
Password: passwordVal,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil{
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Fprintf(cmd.OutOrStdout(), "successfully pulled from %s", origin)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status, err := w.Status()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln("Error getting status", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !status.IsClean() {
|
|
|
|
|
fmt.Println("Changes detected, do you want to push them?")
|
|
|
|
|
confirm := promptui.Prompt {
|
|
|
|
|
Label: "commit and push changes",
|
|
|
|
|
IsConfirm: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := confirm.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Will not push changes")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("Pushing changes...")
|
|
|
|
|
|
|
|
|
|
err = gitAddFiles(w, FileSystem)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Could not add files: %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commitMessage := "backup " + time.Now().String()
|
|
|
|
|
|
|
|
|
|
commit, err := w.Commit(commitMessage, &git.CommitOptions{
|
|
|
|
|
Author: &object.Signature{
|
|
|
|
|
Name: "dotctl CLI",
|
|
|
|
|
Email: "example@example.com",
|
|
|
|
|
When: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
obj, err := r.CommitObject(commit)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Cannot commit: %s",err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(obj)
|
|
|
|
|
|
|
|
|
|
err = r.Push(&git.PushOptions{
|
|
|
|
|
RemoteName: "origin",
|
|
|
|
|
Auth: &http.BasicAuth {
|
|
|
|
|
Username: usernameVal,
|
|
|
|
|
Password: passwordVal,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
CheckIfError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// a pull deletes the dotctl config from the filesystem, need to recreate it
|
|
|
|
|
rewriteConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Fprintf(cmd.OutOrStdout(), "successfully pulled from %s", origin)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status, err := w.Status()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln("Error getting status", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !status.IsClean() {
|
|
|
|
|
fmt.Println("Changes detected, do you want to push them?")
|
|
|
|
|
confirm := promptui.Prompt{
|
|
|
|
|
Label: "commit and push changes",
|
|
|
|
|
IsConfirm: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := confirm.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Will not push changes")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("Pushing changes...")
|
|
|
|
|
|
|
|
|
|
err = gitAddFiles(w, FileSystem)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Could not add files: %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commitMessage := "backup " + time.Now().String()
|
|
|
|
|
|
|
|
|
|
commit, err := w.Commit(commitMessage, &git.CommitOptions{
|
|
|
|
|
Author: &object.Signature{
|
|
|
|
|
Name: "dotctl CLI",
|
|
|
|
|
Email: "example@example.com",
|
|
|
|
|
When: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
obj, err := r.CommitObject(commit)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Cannot commit: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(obj)
|
|
|
|
|
|
|
|
|
|
err = r.Push(&git.PushOptions{
|
|
|
|
|
RemoteName: "origin",
|
|
|
|
|
Auth: &http.BasicAuth{
|
|
|
|
|
Username: usernameVal,
|
|
|
|
|
Password: passwordVal,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
CheckIfError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// a pull deletes the dotctl config from the filesystem, need to recreate it
|
|
|
|
|
rewriteConfig()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rewriteConfig() {
|
|
|
|
|
fs := UseFilesystem()
|
|
|
|
|
err := fs.MkdirAll(path.Join(DotfilePath, "dotctl"), 0755)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Unable to create dotfile structure: %s", error.Error(err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = fs.Create(path.Join(DotfilePath, "dotctl/config"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Errorf("Unable to create config file %w", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = viper.WriteConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error: could not write config: ", err)
|
|
|
|
|
}
|
|
|
|
|
fs := UseFilesystem()
|
|
|
|
|
err := fs.MkdirAll(path.Join(DotfilePath, "dotctl"), 0755)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Unable to create dotfile structure: %s", error.Error(err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = fs.Create(path.Join(DotfilePath, "dotctl/config"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Errorf("Unable to create config file %w", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = viper.WriteConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error: could not write config: ", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|